Skip to content

Commit 4bb4223

Browse files
committed
added thruster sensor code
1 parent 49cd6e4 commit 4bb4223

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

pio_workspace/src/thruster_tests_main.cpp

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,68 @@
1010

1111
#define LED_PIN 13
1212
#define BUTTON 2
13-
#define YELLOW_LED 3
14-
#define GREEN_LED 4
1513

16-
std_msgs::Float32 thruster_force_msg;
1714
float thrusterForce = -1;
1815

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+
1975
uint16_t microseconds[] = {1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
2076
void commandCb(const auv_msgs::ThrusterMicroseconds &tc) {
2177
memcpy(microseconds, tc.microseconds, 8 * sizeof(uint16_t));
@@ -25,12 +81,7 @@ ros::NodeHandle nh;
2581
ros::Subscriber<auv_msgs::ThrusterMicroseconds> sub("/propulsion/microseconds", &commandCb);
2682
ros::Publisher thruster_force("/thruster/force", &thruster_force_msg);
2783

28-
void senseData() {
29-
thrusterForce++;
30-
}
31-
3284
void publishData() {
33-
senseData();
3485
thruster_force_msg.data = thrusterForce;
3586
thruster_force.publish(&thruster_force_msg);
3687
}
@@ -42,21 +93,18 @@ void thruster_tests_setup() {
4293
pinMode(LED_PIN, OUTPUT);
4394
digitalWrite(LED_PIN, HIGH);
4495

96+
sensor_setup();
97+
4598
nh.initNode();
4699
nh.subscribe(sub);
47100
nh.advertise(thruster_force);
48101
}
49102

50103
void thruster_tests_loop() {
51104
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+
60108
publishData();
61109
nh.spinOnce();
62110
delay(10);

0 commit comments

Comments
 (0)