Disclaimer: I am neither an EE major or electrician. What follows is based upon my understanding of the task which may or may not be correct. This applies to wiring “design”/implementation as well as how the software interfaces. By following my examples for wiring and by downloading and using the software you assume all responsibility.
Assembly of robotic arm purchased from Aliexpress. This arm consists of 6 servo motors featuring metal gears. The motors are: Tower Pro – MG996R. This kit provides more parts then are required. I suppose this is because the base is multifunctional and depending upon how you assemble it those additional parts might be employed.
Pictures may be double clicked for expanded view.
The following table will attempt to show/explain the wiring configuration for the above arm (6 servos) and the Adafruit motorHAT. Because of the attributes of the motors employed it was not possible to connect the motor’s servo cable to the 3 output pins on the HAT. These motors would overload the HATs capability so the motor’s signal pin was connected to the HAT’s signal pin and the motor’s +/- wires are connected to a common +/- busbar (in this case a beefed up Adafruit 1/2 siezed Perma Proto board. The wiring for a single motor would look like this:
For six servo motors the 6 V batter pack’s + lead would run to a + bus bar and then to each motor. There are some photos later of this wiring setup.
The video below is based upon the Adafruit 16-Channel PWM / Servo HAT for Raspberry Pi. I have written a short python app to interface to the HAT and control 6 servo motors between their min/max servo positions. For those of us new to servos I feel that this app will help understand how to control the servo in real time. This should help also with defining your servo’s “servoMin and servoMax” parameters. Just be sure to move slowly. If your are not careful you can push the servo beyond in min and max offsets and possibly strip your servo’s gears or cause other damage.
If your servos have a total swing of 180o then you can move the servo using degrees once you have determined servoMin/servoMax. You need to convert degrees to a value between servoMin and servoMax. Here is the formula
rawrange = servoMax – servoMin
pulseLength = (degrees / 180) * rawrange + servoMin
so if you want to move your servo to 30o:
servoMin = 150 (this would be 0o)
servoMax=450 (this would be 180)
rawrange = 450 –150
pulseLength = (30/180)*450 + 150 = 225 (moving your servo to this value would be 30o from the servoMin position
channel# will be the 3pin connector you have your servo connected to on the Adafruit HAT
issue the command: pwm.setPWM(channel#, 0, pulseLength)
Regarding the videos and software. I have removed the video for single servo control and replaced it with a newer video showing the control of all 6 motors and two leds. I have removed the software for single motor control and replaced it with the new software for all 6 motors and leds. You can use the 6 motor software to test a single motor. In fact I would recommend this until you figure out your servoMin/servoMax values and frequency setting. If I had to to this again I would have had the servos free standing (no arm) to check out their functionality.
Two downloads follow (they require the use of an Adafruit servoHat):
- the first will help you determine your servo motors minimum/maximum displacement. It sets the following values: servoMin and ServoMax to 100 and 600 and calculates the middle offset, ServoMiddle. With the gui you will be able to control the servo and determine the values of servoMin and servoMax for your servos.
- NOTE: Regarding the led control portion. You cannot plug a led or two directly into the servo. You must design your own small pcb with resister. ALSO: the program set channel 15 for the led control. Be sure to use this channel only for your leds. If you plug a servo into this channel then you will mostly likely overdrive your servo and damage it.
- the second will control a robotic arm with 6 servos and brighten/dim two leds. This second download set values for my setup. You will need to look at servoMin, servoMax. frequency, the initialization values, etc. and adjust as necessary for your setup.
servo_min_max.py_.gz (1097 downloads)
servo_control.py_.gz (900 downloads)
The following table attempts to show current loading on the arm for:
- all servos as measured on the common + bus
- a single servo. I chose #2 because it has to support the entire arm and I wanted to know the current required for this effort. The other servos with the exception of #6 would have less work to do.
The following table shows voltages across the Servo5’s lines – Orange, Red, Brown at various slider settings
Servo5 slider values | 120 | 200 | 300 | 400 | 500 | 530 | after “quit” |
RB | 4.99 | 5.03 | 5.03 | 4.98 | 4.71 | 4.53 | 5.17 |
OR | 4.89 | 4.83 | 4.80 | 4.74 | 4.45 | 4.36 | 5.17 |
OB | 0.92 | 0.156 | 0.235 | 0.233 | 0.231 | 0.233 | 0 |
+/- busbars | 4.99 | 5.04 | 5.03 | 4.98 | 4.68 | 4.51 | 5.17 |
2 LEDS – slider values | 0 | 200 | 300 | 1400 | 3100 | 4095 | after “quit” |
OR | 2.72 | 2.70 | 2.70 | 2.62 | 2.51 | 2.435 | 2.72 |
Note: OR + OB = RB seems to be true. Some variations occur. Could be interpretation of meter or not waiting for meter to settle.
LED values are coming strictly from the ServoHAT, i.e., the signal pin (O) and 5V+ (R) pin at various slider settings.
Hello. I have download both the codes and have attempted to run them on Python 2.7. However, most of the lines get error messages that there are indent errors or names like self are not defined. What am I doing wrong?
This code was written and tested on Python 5, but I don’t believe it would have a problem on Python 7. Python code sets up it programming blocks like “if” using indentation. If the block of code is not correct it will complain about indentation problems. I downloaded the code and do not get any indentation problems. It is possible that your editor is reformatting the code resulting in “indent errors”. The code contains one “self” definition and many references. That failure could be due to an improper “indent” on that “self” definition. Which editor are you using?
Try downloading the code and running: ./servo_min_max.py without viewing in an editor. Note that you need to make it executable: chmod +x servo_min_max.py.
Thanks – I’ll give it a go….Michael
Let me know. I no longer have a test bed set up for servos at this time so it would be hard for me to test, but I suppose I could come up with something to test with should you continue to run into trouble. It will take a few days though.
I am getting a syntax error in line 72 with ” servo1_rotation_angle_label” being highlighted. I can’t for the life of me seem to find anything wrong with it.
I am sorry to keep worrying you but I would love to have this operating properly as I wish to control about 8 servos to operate some turnouts on my s scale layout.
So are you saying that “indent and self” errors are now fixed? and that you are having just this syntax error?
Line 72? Do you mean line 171: servo1_rotation_angle_label = Gtk.Label(label=”Servo 1 Rotation Angle”) Make sure the line looks exactly like what I have printed here. If you are a perl person you may have added a trailing “;”.
This line will Place a slider header title (label) “Servo 1 Rotation Angle” at the top of the display. It is part of the gui build. You say you are getting a syntax error but do not tell me the error. Cut and paste the error so I can see it. Look at line 59: “from gi.repository import Gtk” . Make sure this line is not causing a problem, i.e., proper indentation, no typos, or extra characters.
Thanks once again. I will be away from my pc for a week or so, I will get back to you then.
Michael
Just one more thing. see below:
Traceback (most recent call last):
File “/home/pi/Downloads/servo_control.py_-1”, line 129, in
class ServoWindow(Gtk.Window):
File “/home/pi/Downloads/servo_control.py_-1”, line 139, in ServoWindow
self.connect(“delete_event”, self.delete_event)
NameError: name ‘self’ is not defined
The check module does not show any areas but run module shows the above.
Michael
Good detail, Thank you for sharing. I came across this voice controlled 6DOF Robotics if anyone interested. https://leenabot.com/en/Robotic-arm/
my myy.. thanks sooo much its working perfectly
I just had to get the files for Adafruit_PWM_Servo_Driver and Adafruit_I2C from the link (https://github.com/born2net/raspberry/tree/master/Adafruit-Raspberry-Pi-Python-Code/Adafruit_PWM_Servo_Driver) all in one folder as the downloaded files.
Bless up man!!
Thanks and I apologize for the late reply but sometimes life gets in the way.