-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArm_code.ino
277 lines (225 loc) · 6.94 KB
/
Arm_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* Libraries*/
#include <Stepper.h>
#include <Servo.h>
#include <math.h>
//#include <MatrixMath.h>
#include <ros.h>
#include <std_msgs/Int8.h>
#define encoderPinA 2
#define encoderPinB 3
#define motor 11
#define limitswitch 10
/*Variables*/
//Stepper
int L = 138;
const int stepsperrev = 200;
int in1Pin = 4;
int in2Pin = 5;
int in3Pin = 6;
int in4Pin = 7;
//DC motor
volatile int encoderPos = 1;
int A = digitalRead(encoderPinA);
int B = digitalRead(encoderPinB);
int istate;
int laststate;
//int LS;
double lastpos = 1;
int desangle;
int stepf = 0;
int steps = 0;
int start = 0;
int reset = 0;
int started;
int stoparm;
Servo myservo;
Stepper myStepper(200, in1Pin, in2Pin, in3Pin, in4Pin);
//double xstart,ystart;
//double xfinish,yfinish;
//double desanglefinish,theta2finish;
//double xdelta,ydelta;
//double desangle,theta2;
//double desangledelta,Ddelta;
//double Jac[2][2];
//double D;
ros::NodeHandle nh;
void rosreset(const std_msgs::Int8& cmd_msg)
{
reset = cmd_msg.data;
}
void rosstop(const std_msgs::Int8& cmd_msg)
{
stoparm = cmd_msg.data;
}
void rosangle(const std_msgs::Int8& cmd_msg)
{
desangle = cmd_msg.data;
}
void rosstart(const std_msgs::Int8& cmd_msg)
{
start = cmd_msg.data;
}
void rosextension(const std_msgs::Int8& cmd_msg)
{
steps = cmd_msg.data*454.545455;
}
void doEncoderA() //when doencoder is called, increase position
{
encoderPos++;
}
void doEncoderB() //when doencoder is called, increase position
{
encoderPos++;
}
ros::Subscriber<std_msgs::Int8> stop_sub("armstop", rosstop);
ros::Subscriber<std_msgs::Int8> reset_sub("armreset", rosreset);
ros::Subscriber<std_msgs::Int8> start_sub("armstart", rosstart);
ros::Subscriber<std_msgs::Int8> angle_sub("armangle", rosangle);
ros::Subscriber<std_msgs::Int8> extension_sub("armextension", rosextension);
void setup() {
nh.initNode();
nh.subscribe(angle_sub);
nh.subscribe(extension_sub);
nh.subscribe(start_sub);
nh.subscribe(reset_sub);
// Stepper motor
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
//DC motor
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
pinMode(motor, OUTPUT);
pinMode(limitswitch, INPUT);
attachInterrupt(digitalPinToInterrupt(encoderPinA), doEncoderA, RISING);
attachInterrupt(digitalPinToInterrupt(encoderPinB), doEncoderB, RISING);
Serial.begin (9600);
Serial.println("Setup successful...");
myStepper.setSpeed(200);
istate = A;
myservo.attach (9);
}
void loop() {
if(start == 1 )
{
started = 1;
encoderPos = 0;
myservo.write (150);
nh.spinOnce();
delay(1);
analogWrite(motor, 220);
// LS = digitalRead(limitswitch);
// if (LS == LOW){
// Serial.println("ktk");
// Serial.println(lastpos);
// Serial.println(desangle);
// delay(100);
// }
// if (LS == HIGH){
// analogWrite(motor, 180);
// encoderPos = 0;
// Serial.println ("LS Pressed");
// delay(100);
// }
if (encoderPos == 0)
{
while (((desangle/.1536)*3.15) > encoderPos)
{ //desiredPos*3.19 converts angles into counts
Serial.println("Ch A"); // see state of ch A
Serial.println(A);
Serial.println("Ch B"); // see state of ch B
Serial.println(B);
Serial.println("Encoder Pos");
Serial.println(encoderPos);
// Serial.println("theta2");
// Serial.println(theta2);
Serial.println("lastpos");
Serial.println(lastpos);
analogWrite(motor, 200); //moves arm down
laststate = digitalRead(encoderPinA);
if (istate != laststate)
{ //if the motor is moving, it will try to change encoderposition accordingly
if (A != B)
{
Serial.println("clockwise");
encoderPos++;
}
if (B > A)
{
Serial.println("counterclockwise");
encoderPos++;
}
Serial.println(encoderPos);
Serial.println("-----------------------------"); //for neatness sake, seperate loops to easily identify changes
}
lastpos = desangle;
}
delay(10);
analogWrite(motor, 180);
nh.spinOnce();
delay(1);
Serial.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
myStepper.step((-steps*454.545455));
delay (2000);
stepf = steps;
myservo.write (150); // sets the servo at 0 degree position
delay(2000); // waits for the servo to get there
myservo.write (90); // sets the servo at 90 degree position
delay (4000); // waits for the servo to get there
myservo.write (150);
delay (1500);
// myStepper.step(steps*454.545455);
// delay (2000);
}
}
while (reset != 1 && started == 1)
{
nh.spinOnce();
delay(1);
if( reset == 1)
{
myStepper.step((stepf*454.545455));
delay (2000);
started = 0;
while ( encoderPos > 100)
{ //desiredPos*3.19 converts angles into counts
if( stoparm == 1)
{
encoderPos = 0;
analogWrite(motor, 180);
}
Serial.println("Ch A"); // see state of ch A
Serial.println(A);
Serial.println("Ch B"); // see state of ch B
Serial.println(B);
Serial.println("Encoder Pos");
Serial.println(encoderPos);
// Serial.println("theta2");
// Serial.println(theta2);
Serial.println("lastpos");
Serial.println(lastpos);
analogWrite(motor, 130); //moves arm down
laststate = digitalRead(encoderPinA);
if (istate != laststate)
{ //if the motor is moving, it will try to change encoderposition accordingly
if (A != B)
{
Serial.println("clockwise");
encoderPos--;
}
if (A > B)
{
Serial.println("counterclockwise");
encoderPos--;
}
Serial.println(encoderPos);
Serial.println("-----------------------------"); //for neatness sake, seperate loops to easily identify changes
}
}
analogWrite(motor, 180);
}
}
nh.spinOnce();
delay(1);
}