Skip to content

Commit c1026c8

Browse files
NPIPHIzyunlam
authored andcommitted
midas reset handling
1 parent 8ecd714 commit c1026c8

File tree

6 files changed

+77
-10
lines changed

6 files changed

+77
-10
lines changed

MIDAS/src/gnc/yessir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Yessir : public KalmanFilter<NUM_STATES, NUM_SENSOR_INPUTS>
2525

2626
void tick(float dt, float sd, Barometer &barometer, Acceleration acceleration, Orientation &orientation, FSMState state);
2727

28-
28+
bool should_reinit = false;
2929
private:
3030
float s_dt = 0.05f;
3131
float spectral_density_ = 13.0f;

MIDAS/src/hardware/telemetry_backend.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,30 @@ class TelemetryBackend {
6060
* @return bool indicating a successful read and write to buffer
6161
*/
6262
template<typename T>
63-
bool read(T* write) {
63+
bool read(T* write, int wait_milliseconds) {
6464
static_assert(sizeof(T) <= RH_RF95_MAX_MESSAGE_LEN, "The data type to receive is too large");
6565
uint8_t len = sizeof(T);
66+
67+
// set receive mode
68+
rf95.setModeRx();
69+
70+
// busy wait for interrupt signalling
71+
for(int i = 1; i < wait_milliseconds; i++){
72+
THREAD_SLEEP(1);
73+
if(digitalRead(rf95._interruptPin)){
74+
rf95.handleInterrupt();
75+
break;
76+
}
77+
}
78+
6679
if (rf95.available() && rf95.recv((uint8_t*) write, &len)) {
67-
return len == sizeof(T);
68-
} else {
69-
return false;
80+
if (sizeof(T) == len) {
81+
return true;
82+
} else {
83+
return false;
84+
}
7085
}
86+
return false;
7187
}
7288

7389
private:

MIDAS/src/systems.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ DECLARE_THREAD(kalman, RocketSystems* arg) {
133133
TickType_t last = xTaskGetTickCount();
134134

135135
while (true) {
136+
if(yessir.should_reinit){
137+
yessir.initialize(arg);
138+
TickType_t last = xTaskGetTickCount();
139+
yessir.should_reinit = false;
140+
}
136141
// add the tick update function
137142
Barometer current_barom_buf = arg->rocket_data.barometer.getRecent();
138143
Orientation current_orientation = arg->rocket_data.orientation.getRecent();
@@ -159,8 +164,26 @@ DECLARE_THREAD(kalman, RocketSystems* arg) {
159164
DECLARE_THREAD(telemetry, RocketSystems* arg) {
160165
while (true) {
161166
arg->tlm.transmit(arg->rocket_data, arg->led);
162-
163-
THREAD_SLEEP(1);
167+
168+
FSMState current_state = arg->rocket_data.fsm_state.getRecentUnsync();
169+
if (current_state == FSMState(STATE_IDLE)) {
170+
TelemetryCommand command;
171+
if (arg->tlm.receive(&command, 2000)) {
172+
if(command.valid()) {
173+
arg->tlm.acknowledgeReceived();
174+
switch(command.command) {
175+
case CommandType::RESET_KF:
176+
yessir.should_reinit = true;
177+
break;
178+
default:
179+
break;
180+
}
181+
}
182+
183+
}
184+
} else {
185+
THREAD_SLEEP(1);
186+
}
164187
}
165188
}
166189

MIDAS/src/telemetry.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ void Telemetry::transmit(RocketData& rocket_data, LEDController& led) {
5959
backend.send(packet);
6060
}
6161

62+
bool Telemetry::receive(TelemetryCommand* command, int wait_milliseconds) {
63+
return backend.read(command, wait_milliseconds);
64+
}
65+
66+
void Telemetry::acknowledgeReceived() {
67+
received_count ++;
68+
}
69+
6270
/**
6371
* @brief creates the packet to send through the telemetry system
6472
*
@@ -78,8 +86,7 @@ TelemetryPacket Telemetry::makePacket(RocketData& data) {
7886

7987
packet.lat = gps.latitude;
8088
packet.lon = gps.longitude;
81-
packet.alt = (int16_t) gps.altitude;
82-
// Convert range of value so that we can also account for negative altitudes
89+
packet.alt = (((int16_t) gps.altitude) & 0xfffe) | (received_count & 0x0001); // Convert range of value so that we can also account for negative altitudes
8390
packet.baro_alt = inv_convert_range<int16_t>(barometer.altitude, 1 << 17);
8491

8592
auto [ax,ay,az] = pack_highg_tilt(highg, map(static_cast<long>(orientation.tilt * 100),0, 314, 0, 63));

MIDAS/src/telemetry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class Telemetry {
2626
ErrorCode __attribute__((warn_unused_result)) init();
2727

2828
void transmit(RocketData& rocket_data, LEDController& led);
29+
bool receive(TelemetryCommand* command, int wait_milliseconds);
30+
void acknowledgeReceived();
2931
private:
32+
int received_count;
3033
TelemetryPacket makePacket(RocketData& data);
3134

3235
TelemetryBackend backend;

MIDAS/src/telemetry_packet.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
struct TelemetryPacket {
1212
int32_t lat;
1313
int32_t lon;
14-
uint16_t alt;
14+
uint16_t alt; //15 bit meters, 1 bit command ack
1515
uint16_t baro_alt;
1616
uint16_t highg_ax; //14 bit signed ax [-16,16) 2 bit tilt angle
1717
uint16_t highg_ay; //14 bit signed ax [-16,16) 2 bit tilt angle
@@ -25,3 +25,21 @@ struct TelemetryPacket {
2525

2626
uint8_t fsm_callsign_satcount; //4 bit fsm state, 1 bit is_sustainer_callsign, 3 bits sat count
2727
};
28+
29+
30+
// Commands transmitted from ground station to rocket
31+
enum class CommandType: uint8_t { RESET_KF };
32+
33+
/**
34+
* @struct TelemetryCommand
35+
*
36+
* @brief format of the packet that telemetry receives
37+
*/
38+
struct TelemetryCommand {
39+
CommandType command;
40+
std::array<char, 3> verify = {{'B', 'R', 'K'}};
41+
42+
bool valid() {
43+
return verify == std::array<char, 3>{{'B','R','K'}};
44+
}
45+
};

0 commit comments

Comments
 (0)