Here is a link to video of project in action - https://www.mathworks.com/videos/how-to-control-a-robotic-arm-from-a-remote-location-using-iot-1538564659095.html .
Something I really wanted to try was designing a robotic arm completely from scratch. There are many open-source designs available, but I thought it would be gratifying if I could design, build, and program one from nothing (and it was).
I will talk about both the design and programming phases. But I will focus more on the programming side since it is not just controlling servo angles. I used Simulink to program an Arduino MKR1000 which is the MCU controlling the robotic arm. I then added the ability to control the robot arm from practically anywhere through ThingSpeak, an open IoT platform with MATLAB analytics.
15” x 20” 1/8-inch plywood
5x GS9025MG servo
MATLAB Support package for Arduino hardware
Simulink Support package for Arduino hardware
6 V DC barrel jack power supply
MATLAB Support package for ThingSpeak
3x 2n2222a Transistors
I wanted to keep the cost of parts low, so I designed this for hobby servos, specifically the GS-9025MG model. The main restrictions that this imposed was the final weight and size. This was because micro servos could not exert much torque, and as a result, cannot lift heavy lever arms.I tried prototyping with 1/4 inch plywood, but it proved to be too heavy for the servos. I reduced the length of the arms and used 1/8 inch plywood, and that fixed the servo control issue. I drew up the design using Fusion 360, and downloaded the design as a .dxf file, which I then laser cut.
Also, supplying continuous DC voltage from a wall outlet proved necessary because the current drawn from battery packs was very high and drained them very quickly.
Regarding controlling the motors, the Arduino MKR1000 has 4 PWM pins, but I needed 5 to control the 5 servos, so I used one PWM pin to control 2 servos and a digital pin to toggle between the two servos and control one at a time.
Here is the circuit describing that:
Here is the wiring of the servos to the Arduino:
There were two parts of the programming, the prototyping phase where I assembled it and got the servos to command to a certain location, and the precise control phase where I was able to command a location to the servo using ThingSpeak.
Using sliders and constant blocks I was able to command the servos on the Arduino to move to certain angles. I used this model to find out the biases between the commanded angle and the actual angles of the arm, as well as the range of allowable angles for each servo.
The blocks with the gear on them are the PWM write blocks for commanding the serve to a certain angle between 0 and 180.
I used inverse kinematics for an arm in 2D planar motion to solve for the angles necessary to move the arm to an XY coordinate. I found that this link explains the kinematics rather well: https://appliedgo.net/roboticarm/
Although this looks more complicated than the other one, the logic is still the same. The output blocks are still the servo write blocks with the gears. The input blocks are now not the slider/constant block inputs but are being read from the ThingSpeak channel. I will elaborate later on using ThingSpeak. The inputs are the X and Y position of the arm, which using the loc2angles matlab function block are converted to commanded angles for the arm. The angles2loc block at the top of the model is just a way of monitoring the system to make sure the servo angles translated to the correct input position using forward kinematics. One of the cool tools from Simulink that I used in this project is External Mode. This allowed me to monitor the system while it was running on the Arduino itself.
The blocks with the gray cloud on them are the ThingSpeak read block built into the Simulink support package for Arduino. I input the channel ID, the data field number I want to retrieve, and the API read key, and it outputs the data from that channel and field. Don’t worry, if you are unfamiliar with any of those terms, I will elaborate soon.
Using ThingSpeak: Creating and using a channel
- Go to https://thingspeak.com/
- Click ‘sign in’ in the top right corner, and log in using your MathWorks account.
- On the dashboard page click ‘New Channel’
- Fill in the data like below. The data shown is all you need to fill in.
- Click ‘Save channel’ at the bottom
- You have now created a ThingSpeak channel
- In the MATLAB command window or using the MATLAB mobile app, you can enter commands that look like:
- thingSpeakWrite(chID,[0.696 3.94 -12 0 0],'WriteKey',APIKEY,'Fields',[1 2 3 4 5]);
This writes new data to the specified fields in the ThingSpeak channel. You can change the values in the second argument of the function call to change the value of the corresponding field. After changing the values, the channel data should look like this:
Running the Model
When you finish with setting up the ThingSpeak channel, you can either run the model on the Arduino in external mode, or by deploying to the hardware. In the Simulink model, go to ‘Model Configuration Parameters’ by clicking on the small gear in the top menu panel, select ‘Hardware implementation’ and make sure the ‘Hardware Board’ is the Arduino MKR 1000. Under ‘Target Hardware Resources’ in the same tab, click on wifi properties and enter the wifi credentials to access the network.
Click the green run button to run the model in external mode or the blue deploy button to deploy it to hardware.
Once the model is running, you can write new data to the fields in the ThingSpeak channel from MATLAB or MATLAB mobile on your phone and watch how it moves your robot arm!