-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand_Rx_code.ino
198 lines (90 loc) · 2.4 KB
/
hand_Rx_code.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*Wireless Hand Gesture Robotic Arm
* Receiver Code
*
* Control A Robotic Arm Wirelessly Using a Glove With Flex and Motion Sensors
*
* Robotic Arm Wiring:
* All VCC - 5V (separate from the Arduino power)
* All GND - GND (power source GND and Arduino GND should be connected)
* Base Servo Control - D3
* Arm Extend Control - D6 and D9 (had to combine because of DOF of controller and wireless data constraints)
* Grip Control - D5
*
* NRF24 Wiring:
* VCC - 3.3V (or use an adapter for 5V, like I did)
* GND - GND
* CE - D8
* CSN - D10
* SCK - D13
* MOSI - D11
* MISO - D12
* IRQ - N/C
*/
#include <SPI.h>
#include <RH_NRF24.h>
int servoBase = 3;
int servoGrip = 5;
int servoExt1 = 6;
int servoExt2 = 9;
RH_NRF24 nrf24;
int yaw, roll, grip;
double posBase, posExt, posGrip;
String receivedString;
void setup()
{
pinMode(servoBase, OUTPUT);
pinMode(servoGrip, OUTPUT);
pinMode(servoExt1, OUTPUT);
pinMode(servoExt2, OUTPUT);
servoWrite(1500, servoBase);
servoWrite(1000, servoGrip);
servoWrite(1500, servoExt1);
servoWrite(1500, servoExt2);
Serial.begin(9600);
if (!nrf24.init())
Serial.println("init failed");
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
}
void loop()
{
if (nrf24.available())
{
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.recv(buf, &len)){
receivedString = (char*)buf;
receivedString.remove(6);
//Serial.println(receivedString);
yaw = receivedString.substring(0,2).toInt();
roll = receivedString.substring(2,4).toInt();
grip = receivedString.substring(4,5).toInt();
Serial.print(yaw);
Serial.print(" ");
Serial.print(roll);
Serial.print(" ");
Serial.println(grip);
posBase = roll*8.33333*2;
posExt = yaw*8.333333*2;
servoWrite(posBase, servoBase);
servoWrite(posExt, servoExt2);
servoWrite(posExt, servoExt1);
if(grip == 1){
posGrip = 1000;
}else{
posGrip = 2000;
}
servoWrite(posGrip, servoGrip);
}
else{
Serial.println("recv failed");
}
}
}
void servoWrite(double pulseWidth, int servo){
digitalWrite(servo, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(servo, LOW);
}