10
10
11
11
#define LED_PIN 13
12
12
#define BUTTON 2
13
- #define YELLOW_LED 3
14
- #define GREEN_LED 4
15
13
16
- std_msgs::Float32 thruster_force_msg;
17
14
float thrusterForce = -1 ;
18
15
16
+ #define YELLOW_LED 6
17
+ #define GREEN_LED 7
18
+
19
+ // sensor code
20
+ #include < HX711_ADC.h>
21
+
22
+ const int HX711_dout = 4 ; // mcu > HX711 dout pin
23
+ const int HX711_sck = 5 ; // mcu > HX711 sck pin
24
+
25
+ HX711_ADC LoadCell (HX711_dout, HX711_sck);
26
+
27
+ const int calVal_eepromAdress = 0 ;
28
+ unsigned long t = 0 ;
29
+
30
+ void sensor_setup () {
31
+ LoadCell.begin ();
32
+ float calibrationValue;
33
+ calibrationValue = 696.0 ;
34
+ unsigned long stabilizingtime = 2000 ;
35
+ boolean _tare = true ;
36
+ LoadCell.start (stabilizingtime, _tare);
37
+ if (LoadCell.getTareTimeoutFlag ()) {
38
+ while (1 );
39
+ } else {
40
+ LoadCell.setCalFactor (calibrationValue);
41
+ }
42
+ }
43
+
44
+ void sensor_loop () {
45
+ static boolean newDataReady = 0 ;
46
+ const int serialPrintInterval = 0 ;
47
+
48
+ if (LoadCell.update ()) newDataReady = true ;
49
+
50
+ if (newDataReady) {
51
+ if (millis () > t + serialPrintInterval) {
52
+ float i = LoadCell.getData ();
53
+ thrusterForce = i;
54
+ newDataReady = 0 ;
55
+ t = millis ();
56
+ }
57
+ }
58
+
59
+ if (digitalRead (BUTTON) == HIGH) {
60
+ LoadCell.tareNoDelay ();
61
+ digitalWrite (YELLOW_LED, HIGH);
62
+ }
63
+
64
+ if (LoadCell.getTareStatus () == true ) {
65
+ digitalWrite (GREEN_LED, HIGH);
66
+ delay (500 );
67
+ digitalWrite (YELLOW_LED, LOW);
68
+ digitalWrite (GREEN_LED, LOW);
69
+ }
70
+ }
71
+ // end of sensor code
72
+
73
+ std_msgs::Float32 thruster_force_msg;
74
+
19
75
uint16_t microseconds[] = {1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 };
20
76
void commandCb (const auv_msgs::ThrusterMicroseconds &tc) {
21
77
memcpy (microseconds, tc.microseconds , 8 * sizeof (uint16_t ));
@@ -25,12 +81,7 @@ ros::NodeHandle nh;
25
81
ros::Subscriber<auv_msgs::ThrusterMicroseconds> sub (" /propulsion/microseconds" , &commandCb);
26
82
ros::Publisher thruster_force (" /thruster/force" , &thruster_force_msg);
27
83
28
- void senseData () {
29
- thrusterForce++;
30
- }
31
-
32
84
void publishData () {
33
- senseData ();
34
85
thruster_force_msg.data = thrusterForce;
35
86
thruster_force.publish (&thruster_force_msg);
36
87
}
@@ -42,21 +93,18 @@ void thruster_tests_setup() {
42
93
pinMode (LED_PIN, OUTPUT);
43
94
digitalWrite (LED_PIN, HIGH);
44
95
96
+ sensor_setup ();
97
+
45
98
nh.initNode ();
46
99
nh.subscribe (sub);
47
100
nh.advertise (thruster_force);
48
101
}
49
102
50
103
void thruster_tests_loop () {
51
104
digitalWrite (LED_PIN, !digitalRead (LED_PIN));
52
- if (digitalRead (BUTTON) == HIGH) {
53
- digitalWrite (YELLOW_LED, HIGH);
54
- digitalWrite (GREEN_LED, LOW);
55
- } else {
56
- digitalWrite (YELLOW_LED, LOW);
57
- digitalWrite (GREEN_LED, HIGH);
58
- }
59
-
105
+
106
+ sensor_loop ();
107
+
60
108
publishData ();
61
109
nh.spinOnce ();
62
110
delay (10 );
0 commit comments