Components and supplies
5V Power Source
MG996R Digital Servo
SG90 Micro Servo
Breadboard - 400 contacts
Arduino Uno Rev3
Tools and machines
Hot glue gun (generic)
Apps and platforms
Arduino IDE
python
Project description
Code
Robot Arm Arduino Code
cpp
Code for the Arduino Arm part of the project
1#include <Servo.h> 2 3Servo servo1; 4Servo servo2; 5Servo servo3; 6Servo servo4; 7Servo servo5; 8 9int servoPositions[5]; 10 11void setup() { 12 Serial.begin(9600); 13 servo1.attach(3); 14 servo2.attach(5); 15 servo3.attach(6); 16 servo4.attach(9); 17 servo5.attach(10); 18} 19 20void loop() { 21 while(Serial.available()){ 22 String input = Serial.readStringUntil('\n'); 23 servoPositions[0] = input.substring(0,3).toInt(); 24 servoPositions[1] = input.substring(3,6).toInt(); 25 servoPositions[2] = input.substring(6,9).toInt(); 26 servoPositions[3] = input.substring(9,12).toInt(); 27 servoPositions[4] = input.substring(12,15).toInt(); 28 } 29 30 servo1.write(servoPositions[0]); 31 servo2.write(servoPositions[1]); 32 servo3.write(servoPositions[2]); 33 servo4.write(servoPositions[3]); 34 servo5.write(servoPositions[4]); 35 36 delay(500); 37}
Python GUI Code
python
Code for the computer GUI that controls the arm
1from tkinter import * 2from tkinter import filedialog 3import serial 4import time 5 6port_opened=False 7 8def set_port(): 9 global port_opened,arduino 10 com_port= port_input.get() 11 arduino=serial.Serial(com_port,9600) 12 port_opened=True 13 print ("COM port set to: "+com_port) 14 15def send_positions(position): 16 message = "{0:0=3d}".format(position[0])+"{0:0=3d}".format(position[1])+"{0:0=3d}".format(position[2])+"{0:0=3d}".format(position[3])+"{0:0=3d}".format(position[4])+"\n" 17 arduino.write(str.encode(message)) 18 print(message, end='') 19 time.sleep(0.2) 20 21saved_positions = [] 22 23def save_positions(): 24 saved_positions.append([servo1_slider.get(), servo2_slider.get(), servo3_slider.get(), servo4_slider.get(), servo5_slider.get()]); 25 print("saved positions: "+str(saved_positions)) 26 27def play_positions(): 28 for position in saved_positions: 29 print("playing: "+str(position)) 30 send_positions(position); 31 time.sleep(1) 32 33def clear_all_positions(): 34 global saved_positions 35 saved_positions = [] 36 print("cleared all positions") 37 38def clear_last_positions(): 39 global saved_positions 40 removed = saved_positions.pop() 41 print("removed: "+str(removed)) 42 print("saved positions: "+str(saved_positions)) 43 44def open_file(): 45 global saved_positions 46 filename = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes = (("Text files","*.txt*"),("all files","*.*"))) 47 file = open(filename, "r") 48 data=file.read() 49 saved_positions=eval(data) 50 file.close() 51 print("opened: "+filename) 52 53def save_file(): 54 save_file = filedialog.asksaveasfile(mode='w', defaultextension=".txt") 55 save_file.write(str(saved_positions)) 56 save_file.close() 57 print("saved file") 58 59def instructions(): 60 print("1.) Set the Arduino's COM port and click Enter. This can be found in Device Manager in Windows") 61 print("2.) Move the arm's servos using the sliders") 62 print("3.) To record a position, click on Record Position") 63 print("4.) To replay the recorded positions, click on Replay Positions") 64 print("\nTo save what you've recorded, got to File > Save File.") 65 print("To open a previously saved file, got to File > Open File.") 66 67window = Tk() 68window.title("Robot Arm Controller 2") 69window.minsize(355,300) 70 71port_label=Label(window,text="Set Port:"); 72port_label.place(x=10,y=10); 73port_input=Entry(window) 74port_input.place(x=10,y=35) 75port_button=Button(window, text="Enter", command=set_port) 76port_button.place(x=135,y=32) 77 78servo1_slider = Scale(window, from_=180, to=0) 79servo1_slider.place(x=0, y=100) 80servo1_label=Label(window,text="Servo 1") 81servo1_label.place(x=10, y=80) 82 83servo2_slider = Scale(window, from_=180, to=0) 84servo2_slider.place(x=70, y=100) 85servo2_label=Label(window,text="Servo 2") 86servo2_label.place(x=80, y=80) 87 88servo3_slider = Scale(window, from_=180, to=0) 89servo3_slider.place(x=140, y=100) 90servo3_label=Label(window,text="Servo 3") 91servo3_label.place(x=150, y=80) 92 93servo4_slider = Scale(window, from_=180, to=0) 94servo4_slider.place(x=210, y=100) 95servo4_label=Label(window,text="Servo 4") 96servo4_label.place(x=220, y=80) 97 98servo5_slider = Scale(window, from_=180, to=0) 99servo5_slider.place(x=280, y=100) 100servo5_label=Label(window,text="Servo 5") 101servo5_label.place(x=290, y=80) 102 103save_button=Button(window, text="Record Position", command=save_positions) 104save_button.place(x=10,y=220) 105 106clear_button=Button(window, text="Clear Last Position", command=clear_last_positions) 107clear_button.place(x=120,y=220) 108 109clear_button=Button(window, text="Clear All Positions", command=clear_all_positions) 110clear_button.place(x=120,y=255) 111 112play_button=Button(window, text="Replay Positions", command=play_positions, height=3) 113play_button.place(x=250,y=220) 114 115menubar = Menu(window) 116filemenu = Menu(menubar, tearoff=0) 117filemenu.add_command(label="Open File", command=open_file) 118filemenu.add_command(label="Save File", command=save_file) 119menubar.add_cascade(label="File", menu=filemenu) 120 121editmenu = Menu(menubar, tearoff=0) 122editmenu.add_command(label="Clear last position", command=clear_last_positions) 123editmenu.add_command(label="Clear all positions", command=clear_all_positions) 124menubar.add_cascade(label="Edit", menu=editmenu) 125 126helpmenu = Menu(menubar, tearoff=0) 127helpmenu.add_command(label="How to use (printed in console)", command=instructions) 128menubar.add_cascade(label="Help", menu=helpmenu) 129 130window.config(menu=menubar) 131 132while True: 133 window.update() 134 if(port_opened): 135 send_positions([servo1_slider.get(), servo2_slider.get(), servo3_slider.get(), servo4_slider.get(), servo5_slider.get()])
Downloadable files
Cardboard Cutout Template
cardboard_arm_template.pdf
Documentation
Schematic
Wiring schematic
robot_arm_2_wiring_1_bb_O0neqoD6Ym.png
Comments
Only logged in users can leave comments