-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robotic_Arm.ino
176 lines (157 loc) · 5.28 KB
/
Robotic_Arm.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <Servo.h>
Servo ThumbRot, ThumbContract, Index, Mid, PinkyRing, UDwrist, LRwrist; //servos
const int Midpot=A0; //middle finger potentiometer is connected to A0
const int ThumbButton=6;
int pos, Mpot, angleMid, buttonState;
int MidExtd, MidCont; //the readings of the potentiometer when the middle finger is contracted and extended
void setup() {
Serial.begin(9600);
Index.attach(7); //attaches the servo to respective pin
ThumbContract.attach(8);
Mid.attach(9);
ThumbRot.attach(10);
UDwrist.attach(11);
LRwrist.attach(12);
PinkyRing.attach(6);
Serial.println("Need to calibrate range of middle finger potentiometer");
Serial.println("Please rotate finger so it is at rest, fully extended");
delay(2000);
MidExtd=(analogRead(Midpot));
Serial.println("Please rotate finger so it is bent over, fully contracted");
delay(2000);
MidCont=(analogRead(Midpot));
Serial.println("The range of the potentiometer is calibrated");
}
void loop() {
wave(); //cycle through each of the hand positions, spending 10 seconds on each one
delay(10000);
relax();
keygrab();
delay(10000);
relax();
fist();
delay(10000);
relax();
}
void wave(){
for (pos = 70; pos <= 110; pos += 1) {
moveLRwrist(pos);
delay(15);
}
for (pos = 110; pos >= 70; pos -= 1) {
moveLRwrist(pos);
delay(15);
}
}
void keygrab(){ //works for holding objects like keys, credit cards, sd cards etc.
moveIndex(180);
delay(1000); //1 second delay so you can place a key or card in position for the thumb to grasp
moveThumb(130, -1); //you might need to change the value of 130, see comment in instructable
//don't forget to press the momentary button to stop the thumb contracting when it has a firm grip
}
void fist(){
moveIndex(180); //contract each of the fingers individually
moveMiddle(180);
movePinkyRing(180);
moveThumb(130, -1); //you might need to change the value of 130, see comment in instructable
//don't forget to press the momentary button to stop the thumb contracting
}
void relax(){
moveIndex(90); //contract each of the fingers individually
moveMiddle(90);
movePinkyRing(90);
moveThumb(130, 1);
moveLRwrist(90);
moveUDwrist(90);
//don't forget to press the momentary button to stop the thumb relaxing
}
void moveThumb(int rotAngle, int contract){
if (rotAngle<60 or rotAngle>140){ //you might need to change this range (see comment in instructable)
Serial.println("Error, thumb rotation angle out of range");
}
else if (contract<-1 or contract>1 ){
Serial.println("Error, thumb contraction out of range");
}
else{
ThumbRot.write(pos);
delay(25);
if (contract==-1){
buttonState=digitalRead(ThumbButton);
while (buttonState== LOW){ //keep rotating pulley until button is pushed
ThumbContract.write(0); //contract the finger
buttonState=digitalRead(ThumbButton); //make sure you push the button otherwise the pulley will keep rotating until either the servo or the thumb breaks
delay(25);
}
}
else if(contract==1){
buttonState=digitalRead(ThumbButton);
while (buttonState== LOW){ //keep rotating pulley until button is pushed
ThumbContract.write(0); //extend the finger
buttonState=digitalRead(ThumbButton);
delay(25);
}
}
//do nothing if contract=0
}
}
void moveMiddle(int angle){
if (angle<90 or angle>180){
Serial.println("Error, middle finger angle out of range");
}
else{
Mpot=analogRead(Midpot);
angleMid=map(Mpot, MidExtd, MidCont, 90, 180); //convert the raw analog value into a understandable range with the units of degrees
if (angleMid<angle){ //needs to contract finger to reach the angle
while (angleMid<angle){
Mid.write(180); //goes to the far extreme, since the potentiometer provides no meaningful reading
Mpot=analogRead(Midpot);
angleMid=map(Mpot, MidExtd, MidCont, 90, 180);
delay(25);
}
}
else if (angleMid>angle){ //needs to relax finger to reach the angle
while (angleMid>angle){
Mid.write(0);
Mpot=analogRead(Midpot);
angleMid=map(Mpot, MidExtd, MidCont, 90, 180);
delay(25);
}
}
}
}
void moveIndex(int angle){
if (angle<90 or angle>180){
Serial.println("Error, index finger angle out of range");
}
else{
Index.write(angle);
delay(25);
}
}
void movePinkyRing(int angle){
if (angle<90 or angle>180){
Serial.println("Error, pinky/ring finger angle out of range");
}
else{
PinkyRing.write(angle);
delay(25);
}
}
void moveUDwrist(int angle){
if (angle<70 or angle>110){
Serial.println("Error, angle out of range for wrist motion");
}
else{
UDwrist.write(angle);
delay(25);
}
}
void moveLRwrist(int angle){
if (angle<70 or angle>110){
Serial.println("Error, angle out of range for wrist motion");
}
else{
LRwrist.write(angle);
delay(25);
}
}