4
4
#include < Wire.h>
5
5
#include " MS5837.h"
6
6
#include < ros.h>
7
+ #include < auv_msgs/ThrusterMicroseconds.h>
7
8
#include < std_msgs/Float32.h>
8
9
#include < std_msgs/Float64.h>
9
10
#include < std_msgs/Int32.h>
@@ -46,14 +47,19 @@ MS5837 sensor;
46
47
// Initialize ROS node handle
47
48
ros::NodeHandle nh;
48
49
50
+ // Define global variables for ros handling
51
+ // Maintains newest version of thruster pwm values and status
52
+ uint16_t microseconds[] = {1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 };
53
+ int Sthrusters[8 ];
54
+
49
55
// Define global variables for ros handling
50
56
// Maintains newest version of all variables
51
57
float batt_voltage_1_new = 0 ;
52
58
float batt_voltage_2_new = 0 ;
53
59
int thrusters_new[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
54
60
int devices_new[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 };
55
61
String status_new = " I am Douglas!" ;
56
- float quaternions_new [] = { 0 , 0 , 0 , 0 };
62
+ float positions_new [] = { 0 , 0 , 0 , 0 };
57
63
58
64
// Define global variables for display functions
59
65
// Maintains version of variables on screen
@@ -63,7 +69,7 @@ uint16_t batt_colours[] = { WHITE, WHITE };
63
69
float thrusters_old[] = { -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 };
64
70
float devices_old[] = { -1 , -1 , -1 , -1 , -1 , -1 , -1 };
65
71
String status_old = " " ;
66
- String quaternions_old [] = { " " , " " , " " , " " };
72
+ String positions_old [] = { " " , " " , " " , " " };
67
73
68
74
// Status of batteries
69
75
boolean BATT1_EMPTY = false ;
@@ -142,14 +148,14 @@ void initMainScreen() {
142
148
tft.drawLine (0 , HEIGHT / 3 + 40 + 45 , WIDTH, HEIGHT / 3 + 40 + 45 , BLACK);
143
149
tft.drawLine (0 , HEIGHT - 41 , WIDTH, HEIGHT - 41 , BLACK);
144
150
145
- // Quaternions section
151
+ // Positions section
146
152
for (int i = 0 ; i < 4 ; i++) {
147
153
int x = i * WIDTH / 4 ;
148
154
tft.drawRect (x, HEIGHT - 40 , WIDTH / 4 , 40 , BLACK);
149
155
tft.setCursor (x + 2 , HEIGHT - 38 );
150
156
tft.setTextSize (1 );
151
157
if (i == 0 ) {
152
- tft.print (" W " );
158
+ tft.print (" YAW " );
153
159
} else if (i == 1 ) {
154
160
tft.print (" X" );
155
161
} else if (i == 2 ) {
@@ -339,16 +345,16 @@ void status(String status) {
339
345
status_old = temp_status;
340
346
}
341
347
342
- // Function to update quaternions display
343
- void quaternions (float W , float X, float Y, float Z) {
344
- char bufferW [5 ], bufferX[5 ], bufferY[5 ], bufferZ[5 ];
348
+ // Function to update positions display
349
+ void positions (float YAW , float X, float Y, float Z) {
350
+ char bufferYAW [5 ], bufferX[5 ], bufferY[5 ], bufferZ[5 ];
345
351
346
- if (W == 1.0 ) {
347
- strcpy (bufferW , " 1.0" );
352
+ if (YAW == 1.0 ) {
353
+ strcpy (bufferYAW , " 1.0" );
348
354
} else {
349
- dtostrf (W , 3 , 2 , bufferW );
350
- if (bufferW [0 ] == ' 0' ) {
351
- memmove (bufferW, bufferW + 1 , strlen (bufferW ));
355
+ dtostrf (YAW , 3 , 2 , bufferYAW );
356
+ if (bufferYAW [0 ] == ' 0' ) {
357
+ memmove (bufferYAW, bufferYAW + 1 , strlen (bufferYAW ));
352
358
}
353
359
}
354
360
@@ -379,26 +385,43 @@ void quaternions(float W, float X, float Y, float Z) {
379
385
}
380
386
}
381
387
382
- String temp_quaternions [] = { bufferW , bufferX, bufferY, bufferZ };
388
+ String temp_positions [] = { bufferYAW , bufferX, bufferY, bufferZ };
383
389
384
390
for (int i = 0 ; i < 4 ; i++) {
385
391
int x = i * WIDTH / 4 ;
386
392
387
- if (temp_quaternions [i] != quaternions_old [i]) {
393
+ if (temp_positions [i] != positions_old [i]) {
388
394
tft.fillRect (x + 1 + 8 , HEIGHT - 40 + 1 + 6 , WIDTH / 4 - 2 - 8 , 38 - 6 , WHITE);
389
395
390
396
tft.setCursor (x + 5 , HEIGHT - 33 );
391
397
tft.setTextColor (BLACK);
392
398
tft.setTextSize (4 );
393
- tft.println (temp_quaternions [i]);
399
+ tft.println (temp_positions [i]);
394
400
}
395
401
}
396
402
397
403
for (int i = 0 ; i < 4 ; i++) {
398
- quaternions_old [i] = temp_quaternions [i];
404
+ positions_old [i] = temp_positions [i];
399
405
}
400
406
}
401
407
408
+ // function to update thruster statuses
409
+ void thrusterStatus (int Sthrusters[]) {
410
+ for (int i = 0 ; i < 8 ; i++) {
411
+ if (microseconds[i] == 1500 ) {
412
+ Sthrusters[i] = 0 ;
413
+ } else {
414
+ Sthrusters[i] = 1 ;
415
+ }
416
+ }
417
+ }
418
+
419
+ // Callback function that updates microseconds array with values from ros
420
+ void commandCb (const auv_msgs::ThrusterMicroseconds& tc){
421
+ memcpy (microseconds, tc.microseconds , 8 *sizeof (uint16_t ));
422
+ thrusterStatus (Sthrusters);
423
+ }
424
+
402
425
// Callback function for battery 1 message
403
426
void batt1MessageCallback (const std_msgs::Float32& msg) {
404
427
batt_voltage_1_new = msg.data ;
@@ -409,45 +432,6 @@ void batt2MessageCallback(const std_msgs::Float32& msg) {
409
432
batt_voltage_2_new = msg.data ;
410
433
}
411
434
412
- // Callback function for thruster 1 message
413
- void thruster1MessageCallback (const std_msgs::Int32& msg) {
414
- thrusters_new[0 ] = msg.data ;
415
- }
416
-
417
- // Callback function for thruster 2 message
418
- void thruster2MessageCallback (const std_msgs::Int32& msg) {
419
- thrusters_new[1 ] = msg.data ;
420
- }
421
-
422
- // Callback function for thruster 3 message
423
- void thruster3MessageCallback (const std_msgs::Int32& msg) {
424
- thrusters_new[2 ] = msg.data ;
425
- }
426
- // Callback function for thruster 4 message
427
- void thruster4MessageCallback (const std_msgs::Int32& msg) {
428
- thrusters_new[3 ] = msg.data ;
429
- }
430
-
431
- // Callback function for thruster 5 message
432
- void thruster5MessageCallback (const std_msgs::Int32& msg) {
433
- thrusters_new[4 ] = msg.data ;
434
- }
435
-
436
- // Callback function for thruster 6 message
437
- void thruster6MessageCallback (const std_msgs::Int32& msg) {
438
- thrusters_new[5 ] = msg.data ;
439
- }
440
-
441
- // Callback function for thruster 7 message
442
- void thruster7MessageCallback (const std_msgs::Int32& msg) {
443
- thrusters_new[6 ] = msg.data ;
444
- }
445
-
446
- // Callback function for thruster 8 message
447
- void thruster8MessageCallback (const std_msgs::Int32& msg) {
448
- thrusters_new[7 ] = msg.data ;
449
- }
450
-
451
435
// Callback function for IMU message
452
436
void devicesIMUMessageCallback (const std_msgs::Int32& msg) {
453
437
devices_new[0 ] = msg.data ;
@@ -488,43 +472,36 @@ void statusMessageCallback(const std_msgs::String& msg) {
488
472
status_new = msg.data ;
489
473
}
490
474
491
- // Callback function for quaternion W message
492
- void quaternionWMessageCallback (const std_msgs::Float32& msg) {
493
- quaternions_new [0 ] = msg.data ;
475
+ // Callback function for position W message
476
+ void positionYAWMessageCallback (const std_msgs::Float32& msg) {
477
+ positions_new [0 ] = msg.data ;
494
478
}
495
479
496
- // Callback function for quaternion X message
497
- void quaternionXMessageCallback (const std_msgs::Float32& msg) {
498
- quaternions_new [1 ] = msg.data ;
480
+ // Callback function for position X message
481
+ void positionXMessageCallback (const std_msgs::Float32& msg) {
482
+ positions_new [1 ] = msg.data ;
499
483
}
500
484
501
- // Callback function for quaternion Y message
502
- void quaternionYMessageCallback (const std_msgs::Float32& msg) {
503
- quaternions_new [2 ] = msg.data ;
485
+ // Callback function for position Y message
486
+ void positionYMessageCallback (const std_msgs::Float32& msg) {
487
+ positions_new [2 ] = msg.data ;
504
488
}
505
489
506
- // Callback function for quaternion Z message
507
- void quaternionZMessageCallback (const std_msgs::Float32& msg) {
508
- quaternions_new [3 ] = msg.data ;
490
+ // Callback function for position Z message
491
+ void positionZMessageCallback (const std_msgs::Float32& msg) {
492
+ positions_new [3 ] = msg.data ;
509
493
}
510
494
511
495
// Define publisher message variable
512
496
std_msgs::Float64 depth_msg;
513
497
514
498
// Define publishers and subscribers
515
499
// Publishes depth
516
- // Subscribes to battery voltages, thruster statuses , device statuses, status message, and quaternions
500
+ // Subscribes to battery voltages, thruster microseconds , device statuses, status message, and positions
517
501
ros::Publisher DEPTH (" /sensors/depth/z" , &depth_msg);
518
- ros::Subscriber<std_msgs::Float32> BATT1 (" /batteries/voltage/1" , &batt1MessageCallback);
519
- ros::Subscriber<std_msgs::Float32> BATT2 (" /batteries/voltage/2" , &batt2MessageCallback);
520
- ros::Subscriber<std_msgs::Int32> THRUSTER1 (" /thrusters/status/1" , &thruster1MessageCallback);
521
- ros::Subscriber<std_msgs::Int32> THRUSTER2 (" /thrusters/status/2" , &thruster2MessageCallback);
522
- ros::Subscriber<std_msgs::Int32> THRUSTER3 (" /thrusters/status/3" , &thruster3MessageCallback);
523
- ros::Subscriber<std_msgs::Int32> THRUSTER4 (" /thrusters/status/4" , &thruster4MessageCallback);
524
- ros::Subscriber<std_msgs::Int32> THRUSTER5 (" /thrusters/status/5" , &thruster5MessageCallback);
525
- ros::Subscriber<std_msgs::Int32> THRUSTER6 (" /thrusters/status/6" , &thruster6MessageCallback);
526
- ros::Subscriber<std_msgs::Int32> THRUSTER7 (" /thrusters/status/7" , &thruster7MessageCallback);
527
- ros::Subscriber<std_msgs::Int32> THRUSTER8 (" /thrusters/status/8" , &thruster8MessageCallback);
502
+ ros::Subscriber<auv_msgs::ThrusterMicroseconds> sub (" /propulsion/microseconds" , &commandCb);
503
+ ros::Subscriber<std_msgs::Float32> BATT1 (" /display/batteries/voltage/1" , &batt1MessageCallback);
504
+ ros::Subscriber<std_msgs::Float32> BATT2 (" /display/batteries/voltage/2" , &batt2MessageCallback);
528
505
ros::Subscriber<std_msgs::Int32> DEVICEIMU (" /sensors/imu/status" , &devicesIMUMessageCallback);
529
506
ros::Subscriber<std_msgs::Int32> DEVICEDVL (" /sensors/dvl/status" , &devicesDVLMessageCallback);
530
507
ros::Subscriber<std_msgs::Int32> DEVICEPS (" /sensors/depth/status" , &devicesPSMessageCallback);
@@ -533,10 +510,10 @@ ros::Subscriber<std_msgs::Int32> DEVICEACT("/sensors/actuator/status", &devicesA
533
510
ros::Subscriber<std_msgs::Int32> DEVICEFC (" /sensors/front_camera/status" , &devicesFCMessageCallback);
534
511
ros::Subscriber<std_msgs::Int32> DEVICEDC (" /sensors/down_camera/status" , &devicesDCMessageCallback);
535
512
ros::Subscriber<std_msgs::String> STATUSMESSAGE (" /mission_display" , &statusMessageCallback);
536
- ros::Subscriber<std_msgs::Float32> QUATERNIONW (" /quaternions/W " , &quaternionWMessageCallback );
537
- ros::Subscriber<std_msgs::Float32> QUATERNIONX (" /quaternions/X " , &quaternionXMessageCallback );
538
- ros::Subscriber<std_msgs::Float32> QUATERNIONY (" /quaternions/Y " , &quaternionYMessageCallback );
539
- ros::Subscriber<std_msgs::Float32> QUATERNIONZ (" /quaternions/Z " , &quaternionZMessageCallback );
513
+ ros::Subscriber<std_msgs::Float32> POSITIONYAW (" /state/theta/z " , &positionYAWMessageCallback );
514
+ ros::Subscriber<std_msgs::Float32> POSITIONX (" /state/x " , &positionXMessageCallback );
515
+ ros::Subscriber<std_msgs::Float32> POSITIONY (" /state/y " , &positionYMessageCallback );
516
+ ros::Subscriber<std_msgs::Float32> POSITIONZ (" /state/z " , &positionZMessageCallback );
540
517
541
518
// Function to calculate and publish depth
542
519
void publish_depth () {
@@ -547,9 +524,9 @@ void publish_depth() {
547
524
void setup () {
548
525
// Initialize I2C communication with sensor
549
526
Wire.begin ();
550
- while (! sensor.init ()) {
551
- delay (5000 );
552
- }
527
+ sensor.init ();
528
+ delay (1000 );
529
+
553
530
sensor.setModel (MS5837::MS5837_30BA);
554
531
sensor.setFluidDensity (997 );
555
532
@@ -568,16 +545,9 @@ void setup() {
568
545
nh.initNode ();
569
546
570
547
// Subscribe to ROS topics
548
+ nh.subscribe (sub);
571
549
nh.subscribe (BATT1);
572
550
nh.subscribe (BATT2);
573
- nh.subscribe (THRUSTER1);
574
- nh.subscribe (THRUSTER2);
575
- nh.subscribe (THRUSTER3);
576
- nh.subscribe (THRUSTER4);
577
- nh.subscribe (THRUSTER5);
578
- nh.subscribe (THRUSTER6);
579
- nh.subscribe (THRUSTER7);
580
- nh.subscribe (THRUSTER8);
581
551
nh.subscribe (DEVICEIMU);
582
552
nh.subscribe (DEVICEDVL);
583
553
nh.subscribe (DEVICEPS);
@@ -586,10 +556,10 @@ void setup() {
586
556
nh.subscribe (DEVICEFC);
587
557
nh.subscribe (DEVICEDC);
588
558
nh.subscribe (STATUSMESSAGE);
589
- nh.subscribe (QUATERNIONW );
590
- nh.subscribe (QUATERNIONX );
591
- nh.subscribe (QUATERNIONY );
592
- nh.subscribe (QUATERNIONZ );
559
+ // nh.subscribe(POSITIONYAW );
560
+ // nh.subscribe(POSITIONX );
561
+ // nh.subscribe(POSITIONY );
562
+ // nh.subscribe(POSITIONZ );
593
563
594
564
// Advertise ROS publisher
595
565
nh.advertise (DEPTH);
@@ -612,10 +582,10 @@ void loop() {
612
582
// Update display with new data
613
583
batt1 (batt_voltage_1_new);
614
584
batt2 (batt_voltage_2_new);
615
- thrusters (thrusters_new [0 ], thrusters_new [1 ], thrusters_new [2 ], thrusters_new [3 ], thrusters_new [4 ], thrusters_new [5 ], thrusters_new [6 ], thrusters_new [7 ]);
585
+ thrusters (Sthrusters [0 ], Sthrusters [1 ], Sthrusters [2 ], Sthrusters [3 ], Sthrusters [4 ], Sthrusters [5 ], Sthrusters [6 ], Sthrusters [7 ]);
616
586
devices (devices_new[0 ], devices_new[1 ], devices_new[2 ], devices_new[3 ], devices_new[4 ], devices_new[5 ], devices_new[6 ]);
617
587
status (status_new);
618
- quaternions (quaternions_new [0 ], quaternions_new [1 ], quaternions_new [2 ], quaternions_new [3 ]);
588
+ // positions(positions_new [0], positions_new [1], positions_new [2], positions_new [3]);
619
589
620
590
// Delay for stability
621
591
delay (10 );
0 commit comments