-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodom_publisher_subscriber.ino
418 lines (282 loc) · 8.25 KB
/
odom_publisher_subscriber.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <ros.h>
#include <geometry_msgs/Vector3Stamped.h>
#include <geometry_msgs/Twist.h>
#include <ros/time.h>
#include "robot_specs.h"
#define LeftMotorA 5
#define LeftMotorB 9
#define RightMotorA 10
#define RightMotorB 11
#define LOOPTIME 100 // PID loop time(ms)
#define SMOOTH 10
#define sign(x) (x > 0) - (x < 0)
#define FORWARD 1
#define BACKWARD 2
#define STOP 0
unsigned long lastMilli = 0; // loop timing
unsigned long lastMilliPub = 0;
long rpm_req1 = 0;
long rpm_req2 = 0;
long long rpm_act1 = 0;
long long rpm_act2 = 0;
long long t1_l=0;
double rpm_req1_smoothed = 0;
double rpm_req2_smoothed = 0;
int directionLeft = FORWARD;
int directionRight = FORWARD;
int PWM_val1 = 0;
int PWM_val2 = 0;
volatile long count1 = 0; // rev counter
volatile long count2 = 0;
long countAnt1 = 0;
long countAnt2 = 0;
float Kp = 0.5;
float Kd = 0;
float Ki = 0;
int flagL=0 , flagR=0 , fl , fr;
ros::NodeHandle nh;
void handle_cmd( const geometry_msgs::Twist& cmd_msg)
{
double x = cmd_msg.linear.x;
double z = cmd_msg.angular.z;
if (z == 0)
{ // go straight
// convert m/s to rpm
rpm_req1 = x*60/(pi*wheel_diameter);
rpm_req2 = rpm_req1;
}
else if (x == 0)
{
// convert rad/s to rpm
rpm_req2 = z*track_width*60/(wheel_diameter*pi*2);
rpm_req1 = -rpm_req2;
}
else
{
rpm_req1 = x*60/(pi*wheel_diameter)-z*track_width*60/(wheel_diameter*pi*2);
rpm_req2 = x*60/(pi*wheel_diameter)+z*track_width*60/(wheel_diameter*pi*2);
}
}
ros::Subscriber<geometry_msgs::Twist> sub("cmd_vel_mux/input/teleop", handle_cmd);
geometry_msgs::Vector3Stamped rpm_msg;
ros::Publisher rpm_pub("rpm", &rpm_msg);
ros::Time current_time;
ros::Time last_time;
char c;
void setup()
{
Serial1.begin(9600);
count1 = 0;
count2 = 0;
countAnt1 = 0;
countAnt2 = 0;
rpm_req1 = 0;
rpm_req2 = 0;
rpm_act1 = 0;
rpm_act2 = 0;
PWM_val1 = 0;
PWM_val2 = 0;
nh.initNode();
nh.getHardware()->setBaud(57600);
nh.subscribe(sub);
nh.advertise(rpm_pub);
pinMode(A5,INPUT);
pinMode(A4,INPUT);
pinMode(LeftMotorA , OUTPUT);
pinMode(LeftMotorB , OUTPUT);
pinMode(RightMotorA , OUTPUT);
pinMode(RightMotorB , OUTPUT);
digitalWrite(LeftMotorA , LOW);
digitalWrite(LeftMotorB , LOW);
digitalWrite(RightMotorA , LOW);
digitalWrite(RightMotorB , LOW);
}
void loop()
{
if(Serial1.available())
{
c = Serial1.read();
switch(c)
{
case 'f' :
PWM_val1 = 150;
PWM_val2 = 150;
motorRun(1,FORWARD,PWM_val1);
motorRun(2,FORWARD,PWM_val2);
break;
case 'b' : motorRun(1,BACKWARD,180);
motorRun(2,BACKWARD,180);
break;
case 'l' : motorRun(1,STOP,0);
motorRun(2,FORWARD,180);
break;
case 'r' : motorRun(1,FORWARD,180);
motorRun(2,STOP,0);
break;
case 's' :
PWM_val1 = 0;
PWM_val2 = 0;
motorRun(1,STOP,0);
motorRun(2,STOP,0);
break;
default : PWM_val1 = 0;
PWM_val2 = 0;
break;
}
}
fl = analogRead(A5);
if((fl>400) && flagL==0)
{
//Serial1.print("L");
flagL=1;
encoderLeft();
}
else if(fl<100)
flagL=0;
fr=analogRead(A4);
if((fr>400) && flagR==0)
{
//Serial1.print("R");
flagR=1;
encoderRight();
}
else if(fr<100)
flagR=0;
unsigned long time = millis();
if(time-lastMilli>= 500)
{ // enter tmed loop
getMotorData(time-lastMilli);
Serial1.print("RPM of Left motor :");
Serial1.print(long(rpm_act1));
Serial1.print("\tRPM of Right motor :");
Serial1.println(long(rpm_act2));
Serial1.print("rpm_req1: ");
Serial1.print(rpm_req1);
Serial1.print("\trpm_req2: ");
Serial1.println(rpm_req2);
PWM_val1 = abs(updatePid(1, PWM_val1, rpm_req1, rpm_act1));
PWM_val2 = abs(updatePid(2, PWM_val2, rpm_req2, rpm_act2));
//PWM_val1 = 150.0*rpm_req1/MAX_RPM;
//PWM_val2 = 150.0*rpm_req2/MAX_RPM;
Serial1.print("PWM1 : ");
Serial1.print(PWM_val1);
Serial1.print("\tPWM2 : ");
Serial1.println(PWM_val2);
Serial1.println();
// if(PWM_val1 > 0) directionLeft = FORWARD;
//else if(PWM_val1 < 0) directionLeft = BACKWARD;
//if (rpm_req1 == 0) directionLeft = STOP;
//if(PWM_val2 > 0) directionRight = FORWARD;
//else if(PWM_val2 < 0) directionRight = BACKWARD;
//if (rpm_req2 == 0) directionRight = STOP;
directionLeft = FORWARD;
directionRight = FORWARD;
motorRun(1,directionLeft,PWM_val1);
motorRun(2,directionRight,PWM_val2);
publishRPM(time-lastMilli);
lastMilli = time;
}
nh.spinOnce();
}
void getMotorData(unsigned long time)
{
unsigned long dt = time*8;
long long dcount1 = count1-countAnt1;
long long dcount2 = count2-countAnt2;
rpm_act1 = (dcount1*60000)/dt;
rpm_act2 = (dcount2*60000)/dt;
countAnt1 = count1;
countAnt2 = count2;
}
int updatePid(int id, int command, double targetValue, double currentValue) {
double pidTerm = 0; // PID correction
double error = 0;
double new_pwm = 0;
double new_cmd = 0;
static double last_error1 = 0;
static double last_error2 = 0;
static double int_error1 = 0;
static double int_error2 = 0;
error = targetValue-currentValue;
if (id == 1) {
int_error1 += error;
pidTerm = Kp*error + Kd*(error-last_error1) + Ki*int_error1;
last_error1 = error;
}
else {
int_error2 += error;
pidTerm = Kp*error + Kd*(error-last_error2) + Ki*int_error2;
last_error2 = error;
}
new_pwm = constrain(abs(command)*MAX_RPM/150.0 + pidTerm, -MAX_RPM, MAX_RPM);
//Serial1.print("new_pwm: ");
//Serial1.println(new_pwm);
new_cmd = 150.0*new_pwm/MAX_RPM;
return int(new_cmd);
}
void motorRun(int Id , int Direction , int Speed)
{
switch(Id)
{
// left motor
case 1 : if(Direction == FORWARD)
{
analogWrite(LeftMotorA , Speed);
analogWrite(LeftMotorB , 0);
break;
}
else if(Direction == BACKWARD)
{
analogWrite(LeftMotorA , 0);
analogWrite(LeftMotorB , Speed);
break;
}
else if(Direction == STOP)
{
digitalWrite(LeftMotorA , HIGH);
digitalWrite(LeftMotorB , HIGH);
break;
}
// Right motor
case 2 : if(Direction == FORWARD)
{
analogWrite(RightMotorA , Speed);
analogWrite(RightMotorB , 0);
break;
}
else if(Direction == BACKWARD)
{
analogWrite(RightMotorA , 0);
analogWrite(RightMotorB , Speed);
break;
}
else if(Direction == STOP)
{
digitalWrite(RightMotorA , HIGH);
digitalWrite(RightMotorB , HIGH);
break;
}
default : break;
}
}
void publishRPM(unsigned long time) {
rpm_msg.header.stamp = nh.now();
rpm_msg.vector.x = rpm_act1;
rpm_msg.vector.y = rpm_act2;
rpm_msg.vector.z = double(time)/1000;
rpm_pub.publish(&rpm_msg);
nh.spinOnce();
}
void encoderLeft()
{
count1++;
}
void encoderRight()
{
count2++;
}