-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenCR_NEO.ino
executable file
·1307 lines (1104 loc) · 35.7 KB
/
OpenCR_NEO.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright 2016 ROBOTIS CO., LTD.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
/* Authors: Yoonseok Pyo, Leon Jung, Darby Lim, HanCheol Cho */
#include "turtlebot3_core_config.h"
/*******************************************************************************
* ROS NodeHandle
*******************************************************************************/
ros::NodeHandle nh;
HardwareTimer Timer(TIMER_CH1);
/*******************************************************************************
* Subscriber
*******************************************************************************/
ros::Subscriber<geometry_msgs::Twist> cmd_vel_sub("cmd_vel", commandVelocityCallback);
ros::Subscriber<geometry_msgs::PoseWithCovarianceStamped> init_pose_sub("initialpose", InitPoseCallback);
/*******************************************************************************
* Publisher
*******************************************************************************/
// Bumpers, cliffs, buttons, encoders, battery of Turtlebot3
turtlebot3_msgs::SensorState sensor_state_msg;
ros::Publisher sensor_state_pub("sensor_state", &sensor_state_msg);
// IMU of Turtlebot3
sensor_msgs::Imu imu_msg;
ros::Publisher imu_pub("imu", &imu_msg);
// Command velocity of Turtlebot3 using RC100 remote controller
geometry_msgs::Twist cmd_vel_rc100_msg;
ros::Publisher cmd_vel_rc100_pub("cmd_vel_rc100", &cmd_vel_rc100_msg);
nav_msgs::Odometry odom;
ros::Publisher odom_pub("odom", &odom);
sensor_msgs::JointState joint_states;
ros::Publisher joint_states_pub("joint_states", &joint_states);
std_msgs::Float64MultiArray sonar1_msg;
ros::Publisher sonar_pub("sonar1_msg", &sonar1_msg);
/*******************************************************************************
* Transform Broadcaster
*******************************************************************************/
// TF of Turtlebot3
geometry_msgs::TransformStamped tfs_msg;
geometry_msgs::TransformStamped odom_tf;
tf::TransformBroadcaster tfbroadcaster;
/*******************************************************************************
* SoftwareTimer of Turtlebot3
*******************************************************************************/
static uint32_t tTime[4];
/*******************************************************************************
* Declaration for motor
*******************************************************************************/
Turtlebot3MotorDriver motor_driver;
bool init_encoder_[2] = {false, false};
int32_t last_diff_tick_[2];
int32_t last_tick_[2];
double last_rad_[2];
double last_velocity_[2];
double goal_linear_velocity = 0.0;
double goal_angular_velocity = 0.0;
/*******************************************************************************
* Declaration for IMU
*******************************************************************************/
cIMU imu;
/*******************************************************************************
* Declaration for RC100 remote controller
*******************************************************************************/
RC100 remote_controller;
double const_cmd_vel = 0.6;
/*******************************************************************************
* Declaration for test drive
*******************************************************************************/
bool start_move = false;
bool start_rotate = false;
int32_t last_left_encoder = 0;
int32_t last_right_encoder = 0;
/*******************************************************************************
* Declaration for SLAM and navigation
*******************************************************************************/
unsigned long prev_update_time;
float odom_pose[3];
char *joint_states_name[] = {"wheel_left_joint", "wheel_right_joint"};
float joint_states_pos[2] = {0.0, 0.0};
float joint_states_vel[2] = {0.0, 0.0};
float joint_states_eff[2] = {0.0, 0.0};
/*******************************************************************************
* Declaration for LED
*******************************************************************************/
#define LED_TXD 0
#define LED_RXD 1
#define LED_LOW_BATTERY 2
#define LED_ROS_CONNECT 3
/*******************************************************************************
* Declaration for Battery
*******************************************************************************/
#define BATTERY_POWER_OFF 0
#define BATTERY_POWER_STARTUP 1
#define BATTERY_POWER_NORMAL 2
#define BATTERY_POWER_CHECK 3
#define BATTERY_POWER_WARNNING 4
static bool setup_end = false;
static uint8_t battery_voltage = 0;
static float battery_valtage_raw = 0;
static uint8_t battery_state = BATTERY_POWER_OFF;
/*******************************************************************************
* Declaration for PID, for speedcontrol
*******************************************************************************/
int timer_timer;
long count_timer1= 0 ; // number of timer2() called
long count_timer2= 0 ; // number of timer2() called
unsigned long watch_dog = 0, last_time = 0;
bool pidflag=false;
double test_speed = 0.2;
double test_d = 0.6;
int64_t end_tick = 0;
/*******************************************************************************
* Setup function
*******************************************************************************/
void setup()
{
// add by huang
drv_pwm_release(MOTOR1_A);
drv_pwm_set_freq(MOTOR1_A,4000);
drv_pwm_release(MOTOR1_B);
drv_pwm_set_freq(MOTOR1_B,4000);
drv_pwm_release(MOTOR2_A);
drv_pwm_set_freq(MOTOR2_A,4000);
drv_pwm_release(MOTOR2_B);
drv_pwm_set_freq(MOTOR2_B,4000);
pidflag=false;watch_dog=0;last_time=0;
motor_driver.speed_motor.v_motor1 = 0;
motor_driver.speed_motor.v_motor2 = 0;
motor_driver.omni_wheel[0].encoder_a = ENCODER1_A;
motor_driver.omni_wheel[0].encoder_b = ENCODER1_B;
motor_driver.omni_wheel[0].counter = 0;
motor_driver.omni_wheel[0].count = 0;
motor_driver.omni_wheel[0].dir = 0;
motor_driver.omni_wheel[1].encoder_a = ENCODER2_A;
motor_driver.omni_wheel[1].encoder_b = ENCODER2_B;
motor_driver.omni_wheel[1].counter = 0;
motor_driver.omni_wheel[1].count = 0;
motor_driver.omni_wheel[1].dir = 0;
Sonar_Init();
//****************************************************************************************
//** Motor Initialization
//****************************************************************************************
// set motor Pin
//****************************************************************************************
//** Encoder Initialization
//****************************************************************************************
// set encoder Pin
pinMode(ENCODER1_B, INPUT_PULLUP);
// pinMode(3, INPUT_PULLDOWN);
pinMode(ENCODER1_A, INPUT_PULLUP);
pinMode(ENCODER2_A, INPUT_PULLUP);
// pinMode(8, INPUT_PULLDOWN);
pinMode(ENCODER2_B, INPUT_PULLUP);
// set encoder irp falling
attachInterrupt(0, EnCoder1_Handle, FALLING);
attachInterrupt(3, EnCoder2_Handle, FALLING);
// set timer8
Timer.stop();
Timer.setPeriod(60000);//intervalTime_timer8); // in microseconds 15ms
Timer.attachInterrupt(Timer8_Handle);
Timer.start();
//****************************************************************************************
//** ROS Initialization
//****************************************************************************************
// Initialize ROS node handle, advertise and subscribe the topics
nh.initNode();
nh.getHardware()->setBaud(115200);
nh.subscribe(cmd_vel_sub);
nh.subscribe(init_pose_sub);
nh.advertise(sensor_state_pub);
nh.advertise(imu_pub);
nh.advertise(cmd_vel_rc100_pub);
nh.advertise(odom_pub);
nh.advertise(joint_states_pub);
nh.advertise(sonar_pub);
tfbroadcaster.init(nh);
nh.loginfo("Connected to OpenCR board!");
// Setting for Dynamixel motors
motor_driver.init();
// Setting for IMU
imu.begin();
// Setting for ROBOTIS RC100 remote controller and cmd_vel
remote_controller.begin(1); // 57600bps baudrate for RC100 control
cmd_vel_rc100_msg.linear.x = 0.0;
cmd_vel_rc100_msg.angular.z = 0.0;
// Setting for SLAM and navigation (odometry, joint states, TF)
odom_pose[0] = 0.0;
odom_pose[1] = 0.0;
odom_pose[2] = 0.0;
joint_states.header.frame_id = "base_footprint";
joint_states.name = joint_states_name;
joint_states.name_length = 2;
joint_states.position_length = 2;
joint_states.velocity_length = 2;
joint_states.effort_length = 2;
prev_update_time = millis();
pinMode(13, OUTPUT);
SerialBT2.begin(57600);
setup_end = true;
}
/*******************************************************************************
* Loop function
*******************************************************************************/
void loop()
{
test_Serial_Control();
//receiveRemoteControlData();
// add by huang
last_time = millis() - watch_dog;
if ( watch_dog != 0 && last_time > 1300 ) {
/* analogWrite(MOTOR1_A, HIGH);
analogWrite(MOTOR1_B, HIGH);
analogWrite(MOTOR2_A, HIGH);
analogWrite(MOTOR2_B, HIGH);*/
goal_linear_velocity = 0;
goal_angular_velocity = 0;
watch_dog = 0;
}
if ((millis()-tTime[0]) >= (1000 / CONTROL_MOTOR_SPEED_PERIOD))
{
controlMotorSpeed();
tTime[0] = millis();
}
if ((millis()-tTime[1]) >= (1000 / CMD_VEL_PUBLISH_PERIOD))
{
cmd_vel_rc100_pub.publish(&cmd_vel_rc100_msg);
//Sonar_Update(sonar1_msg);
//sonar_pub.publish(&sonar1_msg);
tTime[1] = millis();
}
if ((millis()-tTime[2]) >= (1000 / DRIVE_INFORMATION_PUBLISH_PERIOD))
{
publishSensorStateMsg();
publishDriveInformation();
tTime[2] = millis();
}
if ((millis()-tTime[3]) >= (1000 / IMU_PUBLISH_PERIOD))
{
publishImuMsg();
tTime[3] = millis();
}
// Check push button pressed for simple test drive
checkPushButtonState();
// Update the IMU unit
imu.update();
// Start Gyro Calibration after ROS connection
updateGyroCali();
// Show LED status
showLedStatus();
// Update Voltage
updateVoltageCheck();
// Call all the callbacks waiting to be called at that point in time
nh.spinOnce();
}
float velocity_calculate(Wheel *omni) {
float ang;
ang = float(omni->count*ROTATE_SPEED);
/* if (omni->dir == 1) {
ang = -ang;
}*/
omni->count = 0;
return ang;
}
void Timer8_Handle() {
motor_driver.ang[0] = velocity_calculate(&motor_driver.omni_wheel[0]);
if (motor_driver.omni_wheel[0].dir == 0) {
motor_driver.ang[0] = -motor_driver.ang[0];
}
motor_driver.ang[1] = velocity_calculate(&motor_driver.omni_wheel[1]);
if (motor_driver.omni_wheel[1].dir == 1) {
motor_driver.ang[1] = -motor_driver.ang[1];
}
}
void EnCoder1_Handle(){
motor_driver.Count_and_Direction(&motor_driver.omni_wheel[0]);
}
void EnCoder2_Handle(){
motor_driver.Count_and_Direction(&motor_driver.omni_wheel[1]);
}
void test_Serial_Control()
{
if(Serial2.available() > 0 && start_move == false)
{
delay(100);
String comdata = "";
int mode = 0;
int index = 0;
double recv_num[3] = {0};
char tbuffer[100] = {0};
char *pos = tbuffer;
double tkp,tki,tkd;
while (Serial2.available() > 0)
{
unsigned char ch = Serial2.read();
if(ch != ';')
{
*pos = ch;
pos++;
}
else
{
*pos = 0;
if(index == 0)
{
mode = atoi(tbuffer);
}
else if(index == 1)
{
tkp = atof(tbuffer);
}
else if(index == 2)
{
tki = atof(tbuffer);
}
else if(index == 3)
{
tkd = atof(tbuffer);
}
index++;
pos = tbuffer;
}
}
if(mode == 0)
{
motor_driver.m_pid_l.Kp = tkp;
motor_driver.m_pid_l.Ki = tki;
motor_driver.m_pid_l.Kd = tkd;
motor_driver.m_pid_r.Kp = tkp;
motor_driver.m_pid_r.Ki = tki;
motor_driver.m_pid_r.Kd = tkd;
// test_speed = -test_speed;
/* motor_driver.m_pid_r.feedback = 0;
motor_driver.m_pid_r.Integral = 0;
motor_driver.m_pid_r.output = 0;
motor_driver.m_pid_r.last_feedback = 0;
motor_driver.m_pid_l.feedback = 0;
motor_driver.m_pid_l.Integral = 0;
motor_driver.m_pid_l.output = 0;
motor_driver.m_pid_l.last_feedback = 0;*/
}
else if(mode == 2)
{
test_speed = -test_speed;
test_d = tki;
start_move = true;
}
else if(mode == 3)
{
test_speed = tkp;
test_d = tki;
start_move = true;
}
else if(mode == 4)
{
test_speed = -tkp;
test_d = tki;
}
else if(mode == 5)
{
test_speed = tkp;
test_d = tki;
motor_driver.m_pid_r.feedback = 0;
motor_driver.m_pid_r.Integral = 0;
motor_driver.m_pid_r.output = 0;
motor_driver.m_pid_r.last_feedback = 0;
motor_driver.m_pid_l.feedback = 0;
motor_driver.m_pid_l.Integral = 0;
motor_driver.m_pid_l.output = 0;
motor_driver.m_pid_l.last_feedback = 0;
}
else if(mode == 1)
{
motor_driver.m_pid_r.Kp = tkp;
motor_driver.m_pid_r.Ki = tki;
motor_driver.m_pid_r.Kd = tkd;
}
else if(mode == 9)
{
start_move = false;
return;
}
Serial2.print(tkp,5);
Serial2.print(",");
Serial2.print(tki,5);
Serial2.print(",");
Serial2.print(tkd,5);
Serial2.println("");
Serial2.flush();
motor_driver.omni_wheel[0].counter = 0;
motor_driver.omni_wheel[1].counter = 0;
start_move = true;
end_tick = millis();
sensor_state_msg.left_encoder = 0;
sensor_state_msg.right_encoder = 0;
last_left_encoder = sensor_state_msg.left_encoder;
last_right_encoder = sensor_state_msg.right_encoder;
}
}
/*******************************************************************************
* Callback function for cmd_vel msg
*******************************************************************************/
void commandVelocityCallback(const geometry_msgs::Twist& cmd_vel_msg)
{
goal_linear_velocity = cmd_vel_msg.linear.x;
goal_angular_velocity = cmd_vel_msg.angular.z;
pidflag = true;
watch_dog = millis();
}
static double last_theta = 0.0;
void InitPoseCallback(const geometry_msgs::PoseWithCovarianceStamped &msg)
{
odom_pose[0] = msg.pose.pose.position.x;
odom_pose[1] = msg.pose.pose.position.y;
odom_pose[2] = atan2f(msg.pose.pose.orientation.y*msg.pose.pose.orientation.z + msg.pose.pose.orientation.x*msg.pose.pose.orientation.w,
0.5f - msg.pose.pose.orientation.z*msg.pose.pose.orientation.z - msg.pose.pose.orientation.w*msg.pose.pose.orientation.w);
last_theta = odom_pose[2];
}
/*******************************************************************************
* Publish msgs (IMU data: angular velocity, linear acceleration, orientation)
*******************************************************************************/
void publishImuMsg(void)
{
imu_msg.header.stamp = nh.now();
imu_msg.header.frame_id = "imu_link";
imu_msg.angular_velocity.x = imu.SEN.gyroADC[0];
imu_msg.angular_velocity.y = imu.SEN.gyroADC[1];
imu_msg.angular_velocity.z = imu.SEN.gyroADC[2];
imu_msg.angular_velocity_covariance[0] = 0.02;
imu_msg.angular_velocity_covariance[1] = 0;
imu_msg.angular_velocity_covariance[2] = 0;
imu_msg.angular_velocity_covariance[3] = 0;
imu_msg.angular_velocity_covariance[4] = 0.02;
imu_msg.angular_velocity_covariance[5] = 0;
imu_msg.angular_velocity_covariance[6] = 0;
imu_msg.angular_velocity_covariance[7] = 0;
imu_msg.angular_velocity_covariance[8] = 0.02;
imu_msg.linear_acceleration.x = imu.SEN.accADC[0];
imu_msg.linear_acceleration.y = imu.SEN.accADC[1];
imu_msg.linear_acceleration.z = imu.SEN.accADC[2];
imu_msg.linear_acceleration_covariance[0] = 0.04;
imu_msg.linear_acceleration_covariance[1] = 0;
imu_msg.linear_acceleration_covariance[2] = 0;
imu_msg.linear_acceleration_covariance[3] = 0;
imu_msg.linear_acceleration_covariance[4] = 0.04;
imu_msg.linear_acceleration_covariance[5] = 0;
imu_msg.linear_acceleration_covariance[6] = 0;
imu_msg.linear_acceleration_covariance[7] = 0;
imu_msg.linear_acceleration_covariance[8] = 0.04;
imu_msg.orientation.w = imu.quat[0];
imu_msg.orientation.x = imu.quat[1];
imu_msg.orientation.y = imu.quat[2];
imu_msg.orientation.z = imu.quat[3];
imu_msg.orientation_covariance[0] = 0.0025;
imu_msg.orientation_covariance[1] = 0;
imu_msg.orientation_covariance[2] = 0;
imu_msg.orientation_covariance[3] = 0;
imu_msg.orientation_covariance[4] = 0.0025;
imu_msg.orientation_covariance[5] = 0;
imu_msg.orientation_covariance[6] = 0;
imu_msg.orientation_covariance[7] = 0;
imu_msg.orientation_covariance[8] = 0.0025;
imu_pub.publish(&imu_msg);
tfs_msg.header.stamp = nh.now();
tfs_msg.header.frame_id = "base_link";
tfs_msg.child_frame_id = "imu_link";
tfs_msg.transform.rotation.w = imu.quat[0];
tfs_msg.transform.rotation.x = imu.quat[1];
tfs_msg.transform.rotation.y = imu.quat[2];
tfs_msg.transform.rotation.z = imu.quat[3];
tfs_msg.transform.translation.x = 0.1;
tfs_msg.transform.translation.y = 0.0;
tfs_msg.transform.translation.z = 0.068;
tfbroadcaster.sendTransform(tfs_msg);
}
/*******************************************************************************
* Publish msgs (sensor_state: bumpers, cliffs, buttons, encoders, battery)
*******************************************************************************/
void publishSensorStateMsg(void)
{
bool dxl_comm_result = false;
int32_t current_tick;
sensor_state_msg.stamp = nh.now();
sensor_state_msg.battery = checkVoltage();
dxl_comm_result = motor_driver.readEncoder(sensor_state_msg.left_encoder, sensor_state_msg.right_encoder);
if (dxl_comm_result == true)
{
sensor_state_pub.publish(&sensor_state_msg);
}
else
{
return;
}
current_tick = sensor_state_msg.left_encoder;
if (!init_encoder_[LEFT])
{
last_tick_[LEFT] = current_tick;
init_encoder_[LEFT] = true;
}
last_diff_tick_[LEFT] = current_tick - last_tick_[LEFT];
last_tick_[LEFT] = current_tick;
last_rad_[LEFT] += TICK2RAD * (double)last_diff_tick_[LEFT];
current_tick = sensor_state_msg.right_encoder;
if (!init_encoder_[RIGHT])
{
last_tick_[RIGHT] = current_tick;
init_encoder_[RIGHT] = true;
}
last_diff_tick_[RIGHT] = current_tick - last_tick_[RIGHT];
last_tick_[RIGHT] = current_tick;
last_rad_[RIGHT] += TICK2RAD * (double)last_diff_tick_[RIGHT];
}
/*******************************************************************************
* Publish msgs (odometry, joint states, tf)
*******************************************************************************/
void publishDriveInformation(void)
{
unsigned long time_now = millis();
unsigned long step_time = time_now - prev_update_time;
prev_update_time = time_now;
ros::Time stamp_now = nh.now();
// odom
updateOdometry((double)(step_time * 0.001));
odom.header.stamp = stamp_now;
odom_pub.publish(&odom);
// joint_states
updateJoint();
joint_states.header.stamp = stamp_now;
joint_states_pub.publish(&joint_states);
// tf
updateTF(odom_tf);
tfbroadcaster.sendTransform(odom_tf);
}
/*******************************************************************************
* Calculate the odometry
*******************************************************************************/
bool updateOdometry(double diff_time)
{
double odom_vel[3];
double wheel_l, wheel_r; // rotation value of wheel [rad]
double delta_s, delta_theta;
double v, w; // v = translational velocity [m/s], w = rotational velocity [rad/s]
double step_time;
wheel_l = wheel_r = 0.0;
delta_s = delta_theta = 0.0;
v = w = 0.0;
step_time = 0.0;
step_time = diff_time;
if (step_time == 0)
return false;
wheel_l = TICK2RAD * (double)last_diff_tick_[LEFT];
wheel_r = TICK2RAD * (double)last_diff_tick_[RIGHT];
if (isnan(wheel_l))
wheel_l = 0.0;
if (isnan(wheel_r))
wheel_r = 0.0;
delta_s = WHEEL_RADIUS * (wheel_r + wheel_l) / 2.0;
//delta_theta = WHEEL_RADIUS * (wheel_r - wheel_l) / WHEEL_SEPARATION;
delta_theta = atan2f(imu.quat[1]*imu.quat[2] + imu.quat[0]*imu.quat[3],
0.5f - imu.quat[2]*imu.quat[2] - imu.quat[3]*imu.quat[3]) - last_theta;
v = delta_s / step_time;
w = delta_theta / step_time;
last_velocity_[LEFT] = wheel_l / step_time;
last_velocity_[RIGHT] = wheel_r / step_time;
// compute odometric pose
odom_pose[0] += delta_s * cos(odom_pose[2] + (delta_theta / 2.0));
odom_pose[1] += delta_s * sin(odom_pose[2] + (delta_theta / 2.0));
odom_pose[2] += delta_theta;
// compute odometric instantaneouse velocity
odom_vel[0] = v;
odom_vel[1] = 0.0;
odom_vel[2] = w;
odom.pose.pose.position.x = odom_pose[0];
odom.pose.pose.position.y = odom_pose[1];
odom.pose.pose.position.z = 0;
odom.pose.pose.orientation = tf::createQuaternionFromYaw(odom_pose[2]);
// We should update the twist of the odometry
odom.twist.twist.linear.x = odom_vel[0];
odom.twist.twist.angular.z = odom_vel[2];
last_theta = atan2f(imu.quat[1]*imu.quat[2] + imu.quat[0]*imu.quat[3],
0.5f - imu.quat[2]*imu.quat[2] - imu.quat[3]*imu.quat[3]);
return true;
}
/*******************************************************************************
* Calculate the joint states
*******************************************************************************/
void updateJoint(void)
{
joint_states_pos[LEFT] = last_rad_[LEFT];
joint_states_pos[RIGHT] = last_rad_[RIGHT];
joint_states_vel[LEFT] = last_velocity_[LEFT];
joint_states_vel[RIGHT] = last_velocity_[RIGHT];
joint_states.position = joint_states_pos;
joint_states.velocity = joint_states_vel;
}
/*******************************************************************************
* Calculate the TF
*******************************************************************************/
void updateTF(geometry_msgs::TransformStamped& odom_tf)
{
odom.header.frame_id = "odom";
odom_tf.header = odom.header;
odom_tf.child_frame_id = "base_footprint";
odom_tf.transform.translation.x = odom.pose.pose.position.x;
odom_tf.transform.translation.y = odom.pose.pose.position.y;
odom_tf.transform.translation.z = odom.pose.pose.position.z;
odom_tf.transform.rotation = odom.pose.pose.orientation;
}
/*******************************************************************************
* Receive remocon (RC100) data
*******************************************************************************/
void receiveRemoteControlData(void)
{
int received_data = 0;
if (remote_controller.available())
{
received_data = remote_controller.readData();
if (received_data & RC100_BTN_U)
{
cmd_vel_rc100_msg.linear.x += VELOCITY_LINEAR_X * SCALE_VELOCITY_LINEAR_X;
}
else if (received_data & RC100_BTN_D)
{
cmd_vel_rc100_msg.linear.x -= VELOCITY_LINEAR_X * SCALE_VELOCITY_LINEAR_X;
}
if (received_data & RC100_BTN_L)
{
cmd_vel_rc100_msg.angular.z += VELOCITY_ANGULAR_Z * SCALE_VELOCITY_ANGULAR_Z;
}
else if (received_data & RC100_BTN_R)
{
cmd_vel_rc100_msg.angular.z -= VELOCITY_ANGULAR_Z * SCALE_VELOCITY_ANGULAR_Z;
}
if (received_data & RC100_BTN_6)
{
cmd_vel_rc100_msg.linear.x = const_cmd_vel;
cmd_vel_rc100_msg.angular.z = 0.0;
}
else if (received_data & RC100_BTN_5)
{
cmd_vel_rc100_msg.linear.x = 0.0;
cmd_vel_rc100_msg.angular.z = 0.0;
}
if (cmd_vel_rc100_msg.linear.x > MAX_LINEAR_VELOCITY)
{
cmd_vel_rc100_msg.linear.x = MAX_LINEAR_VELOCITY;
}
if (cmd_vel_rc100_msg.angular.z > MAX_ANGULAR_VELOCITY)
{
cmd_vel_rc100_msg.angular.z = MAX_ANGULAR_VELOCITY;
}
goal_linear_velocity = cmd_vel_rc100_msg.linear.x;
goal_angular_velocity = cmd_vel_rc100_msg.angular.z;
pidflag=true;
}
}
/*******************************************************************************
* Control motor speed
*******************************************************************************/
void controlMotorSpeed(void)
{
bool dxl_comm_result = false;
double wheel_speed_cmd[2];
double lin_vel1;
double lin_vel2;
wheel_speed_cmd[LEFT] = goal_linear_velocity*LINEAR_SCALE - (goal_angular_velocity * WHEEL_SEPARATION / 2)*ANGULAR_SCALE;
wheel_speed_cmd[RIGHT] = goal_linear_velocity*LINEAR_SCALE + (goal_angular_velocity * WHEEL_SEPARATION / 2)*ANGULAR_SCALE;
lin_vel1 = wheel_speed_cmd[LEFT] * VELOCITY_CONSTANT_VALUE;
if (lin_vel1 > LIMIT_X_MAX_VELOCITY)
{
lin_vel1 = LIMIT_X_MAX_VELOCITY;
}
else if (lin_vel1 < -LIMIT_X_MAX_VELOCITY)
{
lin_vel1 = -LIMIT_X_MAX_VELOCITY;
}
lin_vel2 = wheel_speed_cmd[RIGHT] * VELOCITY_CONSTANT_VALUE;
if (lin_vel2 > LIMIT_X_MAX_VELOCITY)
{
lin_vel2 = LIMIT_X_MAX_VELOCITY;
}
else if (lin_vel2 < -LIMIT_X_MAX_VELOCITY)
{
lin_vel2 = -LIMIT_X_MAX_VELOCITY;
}
motor_driver.speed_motor.v_motor1 = (int)lin_vel1;
motor_driver.speed_motor.v_motor2 = (int)lin_vel2;
if(pidflag)
{
#if 0
debug_print(abs(motor_driver.speed_motor.v_motor1));
debug_print(",");
debug_print(abs(-motor_driver.speed_motor.v_motor2));
debug_print(",");
debug_print(abs(motor_driver.ang[0]));
debug_print(",");
debug_print(abs(motor_driver.ang[1]));
debug_print(",");
debug_print(motor_driver.omni_wheel[0].counter);
debug_print(",");
debug_print(motor_driver.omni_wheel[1].counter);
debug_print(",");
debug_print(motor_driver.m_pid_l.output);
debug_print(",");
debug_print(motor_driver.m_pid_r.output);
debug_print(",");
debug_print(motor_driver.m_pid_l.Integral);
debug_print(",");
debug_print(motor_driver.m_pid_r.Integral);
debug_println(",0");
#endif
dxl_comm_result = motor_driver.speedControl((int64_t)lin_vel1, (int64_t)lin_vel2,motor_driver.ang[0],motor_driver.ang[1]);
}
else
{
dxl_comm_result = motor_driver.speedControl(0, 0,motor_driver.ang[0],motor_driver.ang[1]);
}
if (dxl_comm_result == false)
return;
}
/*******************************************************************************
* Get Button Press (Push button 1, Push button 2)
*******************************************************************************/
uint8_t getButtonPress(void)
{
uint8_t button_state = 0;
static uint32_t t_time[2];
static uint8_t button_state_num[2] = {0, };
for (int button_num = 0; button_num < 2; button_num++)
{
switch (button_state_num[button_num])
{
case WAIT_FOR_BUTTON_PRESS:
if (getPushButton() & (1 << button_num))
{
t_time[button_num] = millis();
button_state_num[button_num] = WAIT_SECOND;
}
break;
case WAIT_SECOND:
if ((millis()-t_time[button_num]) >= 1000)
{
if (getPushButton() & (1 << button_num))
{
button_state_num[button_num] = CHECK_BUTTON_RELEASED;
button_state |= (1 << button_num);
}
else
{
button_state_num[button_num] = WAIT_FOR_BUTTON_PRESS;
}
}
break;
case CHECK_BUTTON_RELEASED:
if (!(getPushButton() & (1 << button_num)))
button_state_num[button_num] = WAIT_FOR_BUTTON_PRESS;
break;
default :
button_state_num[button_num] = WAIT_FOR_BUTTON_PRESS;
break;
}
}
return button_state;
}
/*******************************************************************************
* Turtlebot3 test drive using RC100 remote controller
*******************************************************************************/
void testDrive(void)
{
int32_t current_tick = millis();
double diff_encoder = 0.0;
if (start_move)
{
diff_encoder = test_d / (0.207 / 330); // (Circumference) / (The number of tick per revolution)
if (abs(end_tick - current_tick) <= 4000)
{
cmd_vel_rc100_msg.linear.x = test_speed * SCALE_VELOCITY_LINEAR_X;
goal_linear_velocity = cmd_vel_rc100_msg.linear.x;
pidflag = true;
}
else
{
cmd_vel_rc100_msg.linear.x = 0.0;
goal_linear_velocity = cmd_vel_rc100_msg.linear.x;
pidflag = false;
start_move = false;
}
}
else if (start_rotate)
{
diff_encoder = test_d / (0.207 / 330);
if (abs(end_tick - current_tick) <= 4000)
{
cmd_vel_rc100_msg.linear.x = -test_speed * SCALE_VELOCITY_LINEAR_X;
goal_linear_velocity = cmd_vel_rc100_msg.linear.x;
pidflag = true;
}
else
{
cmd_vel_rc100_msg.linear.x = 0.0;
goal_linear_velocity = cmd_vel_rc100_msg.linear.x;
pidflag = false;
start_rotate = false;
}
}
}
/*******************************************************************************
* Check Push Button State
*******************************************************************************/
void checkPushButtonState()
{
uint8_t button_state = getButtonPress();
if (button_state & (1<<0))
{
start_move = true;
last_left_encoder = sensor_state_msg.left_encoder;
last_right_encoder = sensor_state_msg.right_encoder;
}
if (button_state & (1<<1))
{
end_tick = millis();
start_rotate = true;
last_left_encoder = sensor_state_msg.left_encoder;
last_right_encoder = sensor_state_msg.right_encoder;
}
testDrive();
}
/*******************************************************************************
* Check voltage
*******************************************************************************/
float checkVoltage(void)
{
float vol_value;