-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththeta_claw.ino
164 lines (148 loc) · 4.17 KB
/
theta_claw.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
#include <Servo.h>
//define servos
Servo right;
Servo left;
Servo middle;
Servo claw;
//define joystick pins
const int R_SW_pin = 7; // Left joystick switch output connected to digital pin
const int R_X_pin = 1; // Left joystick X output connected to analog pin
const int R_Y_pin = 0; // Right joystick y output connected to analog pin
const int L_SW_pin = 6; // Left joystick switch output connected to digital pin
const int L_X_pin = 3; // Left joystick X output connected to analog pin
const int L_Y_pin = 2; // Right joystick y output connected to analog pin
//variable to store and read values from analog pins
int currRX = 0;
int currRY = 0;
int initJoyValRX = 0;
int initJoyValRY = 0;
int currLX = 0;
int currLY = 0;
int initJoyValLX = 0;
int initJoyValLY = 0;
void setup() {
right.attach(10);
middle.attach(11);
left.attach(12);
claw.attach(9);
pinMode(L_SW_pin, INPUT);
digitalWrite(L_SW_pin,HIGH);
pinMode(R_SW_pin, INPUT);
digitalWrite(R_SW_pin,HIGH);
Serial.begin(115200);
initJoyValLX = analogRead(L_X_pin);
initJoyValLY = analogRead(L_Y_pin);
initJoyValRX = analogRead(R_X_pin);
initJoyValRY = analogRead(R_Y_pin);
// get joystick values with no motion to remap later
//Serial.println(servo_test.read());
currRX = map(initJoyValRX, 0, 1023, 0, 180);
currRY = map(initJoyValRY, 0, 1023, 0, 180);
currLX = map(initJoyValLX, 0, 1023, 0, 180);
currLY = map(initJoyValLY, 0, 1023, 0, 180);
// slow movement to inital position for servo 1
//resetServoPosition(currRY, right);
// slow movement to initial position for servo 2
//resetServoPosition(currRX, middle);
//resetServoPosition(currLX, claw);
//resetServoPosition(currLY, left);
}
int joyValRX_analog = 0;
int joyValRY_analog = 0;
int joyValRX = 0;
int joyValRY = 0;
int joyValLX_analog = 0;
int joyValLY_analog = 0;
int joyValLX = 0;
int joyValLY = 0;
int timeClawGrip = 0;
void loop() {
while(true){
joyValRX_analog = analogRead(R_X_pin);
delay(8);
joyValRY_analog = analogRead(R_Y_pin);
delay(8);
joyValLX_analog = analogRead(L_X_pin);
delay(8);
joyValLY_analog = analogRead(L_Y_pin);
delay(8);
// remapping values so the initial joystick analog read is the default
joyValRX = remap(joyValRX_analog, initJoyValRX);
joyValRY = remap(joyValRY_analog, initJoyValRY);
joyValLX = remap(joyValLX_analog, initJoyValLX);
joyValLY = remap(joyValLY_analog, initJoyValLY);
currRX -= joyValRX;
currRY += joyValRY;
currRX = servoWrite(currRX, 90, 180); //right
currRY = servoWrite(currRY, 0, 180); //middle
currLX -= joyValLX;
currLY += joyValLY;
currLX = servoWrite(currLX, 35, 70); //left
currLY = servoWrite(currLY, 10, 125); //claw
middle.write(currRX);
right.write(currRY);
left.write(currLX);
claw.write(currLY);
/*if (joyValLY == 0) {
timeClawGrip = 0;
if (!claw.attached()) {
claw.attach(9);
Serial.print("ATTACHED");
}
} else {
timeClawGrip++;
}
if (timeClawGrip >= 5) {
if (claw.attached()) {
claw.detach();
Serial.print("DETACHED");
}
}*/
Serial.println("CurrRX: " + String(currRX));
Serial.println("CurrRY: " + String(currRY));
Serial.println("CurrLX: " + String(currLX));
Serial.println("CurrLY: " + String(currLY));
//can't move servo out of range by pushing joystick
delay(20);
}
}
int servoWrite(int curr, int minVal, int maxVal){
if (curr > maxVal) {
curr = maxVal;
}
else if (curr < minVal) {
curr = minVal;
}
return curr;
}
int remap(int analog, int initJoyVal){
int joyVal = 0;
if (analog < 212) {
joyVal = -1;
}
else if (analog > 812) {
joyVal = 1;
} else {
joyVal = 0;
}
//Serial.print(joyVal);
return joyVal;
}
void resetServoPosition(int curr, Servo servo) {
int new_val = 0;
if (servo.read() < curr) {
while (servo.read() < curr) {
new_val = servo.read() + 3;
servo.write(new_val);
Serial.println(new_val);
delay(50);
}
} else {
while (servo.read() >= curr) {
new_val = servo.read() - 3;
servo.write(new_val);
Serial.println(new_val);
delay(50);
}
}
}