There are many devices that can be connected to a microcontroller to provide an action, or sense the environment. Here are a few items that can be connected to a microcontroller:
- Accelerometers
- Servos
- Other motors (like stepping motors)
- Mosfets
- Other ICs
- Range Finders
- GPS
- and Much More.
Servos are motors that use feedback to control the motor's position. This type of control is called closed loop control. Servos use a special electromechanical device to provide this feedback. This electromechanical device can be a potentiometer or encoder. Encoders come in various shapes, sizes and resolutions.
Encoders provide a pulse (usually caused by an LED and sensor) that determines the position of the shaft of the motor. When the controller counts to the commanded number of pulses, the motor is stopped, or put into reverse if the motor over ran the position. The motor and controller will constantly provide torque in the direction that is needed to keep the position and is always correcting.
If a potentiometer is used, the motor shaft is connected to the potentiometer in some way (hobby servos connect the potentiometer with gears).
The Hitec HS-322HD is a hobby servo. This servo uses a potentiometer to determine the position of the horn. The potentiometer is connected directly to the rotation of the horn. The horn and potentiometer is connected to the motor shaft by a series of gears giving the motor a very high mechanical advantage.
The potentiometer is connected to the motor via a circuit that is used to control the motor.
This disassembly is not advised. The motor housing is glued to the inside housing of the servo at a location that is very difficult to reach.
The hobby servo requires a PWM to turn the horn (shaft) of the servo. The particular hobby servo that is being used is a Hitec and this servo requires the PWM period to be at 20ms (milliseconds). The actual pulse is only 0.9ms to 2.1ms in width. That means that the pulse width of .9ms will turn the servo to one extreme and the 2.1ms will turn the servo to the other extreme (180 degrees).
This video specifically applies to hobby servo control, but is a good discussion on using the 16-bit timer to create a PWM.
The period can be created using the 16-bit timer overflow at 65535, or the ICR (Input Capture Register) can be used as the top of the timer count so the timer overflows at the value given to the ICR. This allows for a very precise PWM period.
The OCR (Output Compare Register) is used to start the pulse. When the overflow, or the ICR is hit by the timer/counter, the pulse ends and the signal goes to 0v.
The hobby servo is connected to the microcontroller using three wires, the VCC, GND and signal wire. The signal wire is connected to the OCR1A pin of the AVR microcontroller which will output the PWM (Pulse Width Modulation).
The Hobby Servo is connected to PD5 or OC1 pin and tested for a 2 ms pulse width.
The servo horn did not rotate to its expected position. Trial and error is required to determine the actual value that is needed to get to the desired position. Caution is advised not to exceed the servo's limits, or the servo may be damaged during this process.
The OCR1A is adjusted to 900 to create a .9 ms Pulse width.
The OCR1A is adjusted to 900 to create a .9 ms Pulse width. The horn did not turn to the expected extreme, so the number will need to be adjusted with trial and error. Caution is advised not to exceed the servo's physical limits.
The OCR1A for the first position is moved into the while(1) never ending loop. A delay is introduced because it takes time for the servo to get to the intended position. If the delay was not used, the servo would just shake in a very small position. Also, since there is no way for the microcontroller to know the position of the servo (all of the closed loop positioning is internal within the servo) the delay allows the servo to reach the position.
Another OCR1A for the other extreme position is added after the delay. Another delay is added after this statement to give the servo time to get to this new position.
The process is repeated until the microcontroller is unplugged.
The values are 800 (.8 ms) and 2200 (2.2 ms). These numbers can be further tweaked to turn the horn more towards its limits.
The OCR1A for the first position is moved into the while(1) never ending loop. A delay is introduced because it takes time for the servo to get to the intended position. If the delay was not used, the servo would just shake in a very small position. Also, since there is no way for the microcontroller to know the position of the servo (all of the closed loop positioning is internal within the servo) the delay allows the servo to reach the position.
Another OCR1A for the other extreme position is added after the delay. Another delay is added after this statement to give the servo time to get to this new position.
The process is repeated until the microcontroller is unplugged.
In non-inverted mode, the pulse is stared at the beginning of the period. The Period to match 50 Hz (20 ms period time frame), a timer/counter is used and the top value ICR1 is set at 19,999.
To set non-inverted mode, the COM1A1 is set and COM1A0 is not set.
The OCR value is set to correspond to the non-inverted mode so it is set at the low end of the timer/counter count. 2 ms would be equal to 2000. In inverted mode, the 2 ms would be equal to 19,999 - 2000 since the pulse is at the end.
In non-inverted mode, the pulse is stared at the beginning of the period. The Period to match 50 Hz (20 ms period time frame), a timer/counter is used and the top value ICR1 is set at 19,999.
To set non-inverted mode, the COM1A1 is set and COM1A0 is not set.
The OCR value is set to correspond to the non-inverted mode so it is set at the low end of the timer/counter count. 2 ms would be equal to 2000. In inverted mode, the 2 ms would be equal to 19,999 - 2000 since the pulse is at the end.
In non-inverted mode, the pulse is stared at the beginning of the period. The Period to match 50 Hz (20 ms period time frame), a timer/counter is used and the top value ICR1 is set at 19,999.
To set non-inverted mode, the COM1A1 is set and COM1A0 is not set.
The OCR value is set to correspond to the non-inverted mode so it is set at the low end of the timer/counter count. 2 ms would be equal to 2000. In inverted mode, the 2 ms would be equal to 19,999 - 2000 since the pulse is at the end.
In this case, a Hitec hobby servo is controlled using the PWM signal, but any device could be controlled using this method with a change to the ICR value to conform to the device's PWM frequency requirement. The 19,999 (20,000 indexed from 0) is used because this translates to a 50 Hz frequency, or 20 ms period that the Hitec servo requires.
The TCNT1 will be used to bring the pin low within the period for the specific pin. The TCNT1 is a timer/counter that holds the value of the clock.
Important aspects of outputting to more than one pin are as follows:
- Make sure the pins that will output the PWM are set for output data direction (i.e. DDRB = 0xFF for all PORT B pins output)
- Make sure that the pins will be set high in the interrupt service routine (ISR)
- Add another line within code block of the if (TCNT1>=800 && TCNT1<=2400) and make sure the pin reflect the correct output pin and the number after the TCNT1 >= value is correctly set for the position for that servo on that pin.
Important aspects of outputting to more than one pin are as follows:
- Make sure the pins that will output the PWM are set for output data direction (i.e. DDRB = 0xFF for all PORT B pins output)
- Make sure that the pins will be set high in the interrupt service routine (ISR)
- Add another line within code block of the if (TCNT1>=800 && TCNT1<=2400) and make sure the pin reflect the correct output pin and the number after the TCNT1 >= value is correctly set for the position for that servo on that pin.
Important aspects of outputting to more than one pin are as follows:
- Make sure the pins that will output the PWM are set for output data direction (i.e. DDRB = 0xFF for all PORT B pins output)
- Make sure that the pins will be set high in the interrupt service routine (ISR)
- Add another line within code block of the if (TCNT1>=800 && TCNT1<=2400) and make sure the pin reflect the correct output pin and the number after the TCNT1 >= value is correctly set for the position for that servo on that pin.
Only three servos were available, so the true output will not be shown.
Important aspects of outputting to more than one pin are as follows:
- Make sure the pins that will output the PWM are set for output data direction (i.e. DDRB = 0xFF for all PORT B pins output)
- Make sure that the pins will be set high in the interrupt service routine (ISR)
- Add another line within code block of the if (TCNT1>=800 && TCNT1<=2400) and make sure the pin reflect the correct output pin and the number after the TCNT1 >= value is correctly set for the position for that servo on that pin.
The Dynamixel line of servos are digital servos that use the UART form of communication. The mode of communication is Half Duplex and Asynchronous sinc ethe full communication happens only on one line.
There can be as many as 256 servos on the communication line. Each servo has a unique ID, so when there is a transmission, the data that is sent must contain the ID of the servo that is to be controlled.
The horn mount is removed by a center screw and the mount is pryed off of the servo. The case is disassembled with 4 long screws at each corner. The top cover is removed exposing the gears. The bottom cover is removed.
The gears are removed and the bushings are removed from the larger gear.
The horn mount is removed by a center screw and the mount is pried off of the servo. The case is disassembled with 4 long screws at each corner. The top cover is removed exposing the gears. The bottom cover is removed.
The gears are removed and the bushings are removed from the larger gear.
Bushing are slid onto each side of the large gear. The large gear is installed on the servo. Grease is applied to the medium compound gear and installed on the pin as shown where the smaller pinion is mated with the large gear. The metallic gear can be used in place of this gear. The remaining gears are installed to complete the gear assembly using grease. The gears are spun to insure correct assembly.
The top cover and bottom cover is installed using 4 long screws. Finally, the horn mount is installed using a single screw that is fastened to the center of the mount.