Skip to content

Commit cd76ac9

Browse files
committed
more efficient method for kill switches and water detection
1 parent 64b66c2 commit cd76ac9

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

Power Board/thrusters.ino

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ DO NOT FLASH TO TEENSY OR TO ARDUINO
44
55
*/
66

7+
// test MCU kill switch
8+
// test water interrupt
9+
// test delay needed for thruster initialization
10+
// test if ESCs arm after kill switches
11+
// arm all thrusters
12+
713
#include <Servo.h>
814

915
// boolean for interrupts
@@ -70,9 +76,6 @@ void updateThrusters(const uint16_t microseconds[8]) {
7076

7177
// attaches and arms thrusters
7278
void initThrusters() {
73-
interrupts();
74-
interruptFlag = false;
75-
7679
thrusters[SRG_P].attach(SRG_P_PIN);
7780
thrusters[SRG_S].attach(SRG_S_PIN);
7881
thrusters[SWY_BW].attach(SWY_BW_PIN);
@@ -84,6 +87,8 @@ void initThrusters() {
8487

8588
updateThrusters(offCommand);
8689
delay(7000);
90+
91+
interruptFlag = false;
8792
}
8893

8994
void killSystem() {
@@ -96,25 +101,38 @@ void powerSystem() {
96101
delay(100);
97102
}
98103

104+
void interruptRaised() {
105+
interruptFlag = true;
106+
}
107+
108+
void waterInterrupt() {
109+
if (digitalRead(WATER_DETECTED) == HIGH) {
110+
killSystem();
111+
} else {
112+
powerSystem();
113+
}
114+
}
115+
99116
void setup() {
100117
pinMode(SYSTEM_KILLED, INPUT_PULLUP);
101118
pinMode(THURSTERS_KILLED, INPUT_PULLUP);
102119
pinMode(WATER_DETECTED, INPUT_PULLUP);
103120

104121
pinMode(MCU_KS, OUTPUT);
105122

106-
attachInterrupt(digitalPinToInterrupt(SYSTEM_KILLED), initThrusters, RISING);
107-
attachInterrupt(digitalPinToInterrupt(THURSTERS_KILLED), initThrusters, RISING);
108-
attachInterrupt(digitalPinToInterrupt(WATER_DETECTED), killSystem, RISING);
109-
attachInterrupt(digitalPinToInterrupt(WATER_DETECTED), powerSystem, FALLING);
123+
attachInterrupt(digitalPinToInterrupt(SYSTEM_KILLED), interruptRaised, RISING);
124+
attachInterrupt(digitalPinToInterrupt(THURSTERS_KILLED), interruptRaised, RISING);
125+
attachInterrupt(digitalPinToInterrupt(WATER_DETECTED), waterInterrupt, CHANGE);
110126

111127
initThrusters();
112128
}
113129

114130
void loop() {
115131
if (interruptFlag) {
116-
interruptFlag = false;
132+
initThrusters();
117133
}
118134

119-
updateThrusters(microseconds);
135+
if (digitalRead(WATER_DETECTED) == LOW) {
136+
updateThrusters(microseconds);
137+
}
120138
}

0 commit comments

Comments
 (0)