Skip to content
Snippets Groups Projects

Working 24 controls + attribution

Merged Franklin Kaifeng Zheng requested to merge fkzheng-24-sensor-attribution into main
Files
2
+ 16
17
@@ -553,8 +553,9 @@ void track_admin() {
_print_reservation_table(track_node_reservations, curtime, cur_res_screen_state);
break;
case TRACK_GET_SWITCH:
reply[0] = switch_states[(uint8_t)req->switch_num];
reply[0] = switch_states[(int)req->switch_num];
Reply(sender, reply, 1);
break;
case TRACK_RESERVE_SEGMENTS:
reply[0] = 1;
@@ -581,8 +582,9 @@ void track_admin() {
case TRACK_UNPARK:
ASSERT(req->node_count == 1);
reply[0] = 1;
_unpark_train(track_node_reservations, req->track_nodes_encoded[0]);
Reply(sender, reply, 1);
_unpark_train(track_node_reservations, req->track_nodes_encoded[0]);
break;
case TRACK_PARK:
ASSERT(req->node_count == 1);
@@ -774,13 +776,14 @@ void flip_next_switches(struct Ring_Buffer* path) {
}
sw_count = turn_switches(switches, directions, sw_count);
// printi("Flipping ", sw_count, " switches\r\n");
Delay(get_tid_by_name(CLOCK_SERVER), 100);
// Delay(get_tid_by_name(CLOCK_SERVER), 100);
}
char get_switch_state(uint8_t switch_num) {
int track_admin_tid = get_tid_by_name(TRACK_ADMIN);
// prints("DEBUG\r\n");
debug_printi("switch_num: ", switch_num, "\r\n");
struct Track_Request track_req = { .request_type = TRACK_GET_SWITCH, .switch_num = switch_num };
char reply[1];
Send(track_admin_tid, (char*)&track_req, sizeof(struct Track_Request), reply, 1);
@@ -798,11 +801,9 @@ void switch_worker() {
struct Track_Request track_req = { .request_type = TRACK_UPDATE_SWITCH, .sw_count = 0 };
int uart_train_tx_tid = get_tid_by_name(UART_SERVER_TRAIN_SEND);
char cmd[] = " ";
char switch_flipped = 0;
for (int i = 0; i < req->sw_count; i++) {
// check if we need to flip it
if (req->directions[i] != get_switch_state(req->switch_nums[i])) {
switch_flipped = 1;
cmd[0] = req->directions[i] == 'S' ? SWITCH_STRAIGHT : SWITCH_CURVED;
cmd[1] = req->switch_nums[i];
@@ -843,19 +844,16 @@ void switch_worker() {
// send solenoid off
cmd[0] = SWITCH_SOLENOID_OFF;
cmd[1] = '\0';
Delay(get_tid_by_name(CLOCK_SERVER), SOLENOID_OFF_DELAY);
if (switch_flipped) {
if (track_req.sw_count) {
Delay(get_tid_by_name(CLOCK_SERVER), SOLENOID_OFF_DELAY);
send_switch_command(uart_train_tx_tid, cmd);
char reply[1];
Send(get_tid_by_name(TRACK_ADMIN), (char*)&track_req, sizeof(struct Track_Request), reply, 1);
}
int track_admin_tid = WhoIs(TRACK_ADMIN);
char reply[1];
Send(track_admin_tid, (char*)&track_req, sizeof(struct Track_Request), reply, 1);
debug_prints("Switch worker exiting\r\n");
Exit();
}
// TODO: Have this ask the track admin which switches actually need to be flipped because switch flipping is very slow
int turn_switches(char* switch_nums, char* directions, uint8_t sw_count) {
ASSERT(sw_count < 23);
struct Switch_Request req = { .sw_count = 0 };
@@ -872,10 +870,11 @@ int turn_switches(char* switch_nums, char* directions, uint8_t sw_count) {
printi("invalid switch direction on switch: ", switch_num, ", must be one of {'S', 'C'}\r\n");
return 0;
}
req.switch_nums[i] = switch_num;
req.directions[i] = direction;
req.sw_count++;
if (get_switch_state(switch_num) != direction) {
req.switch_nums[i] = switch_num;
req.directions[i] = direction;
req.sw_count++;
}
}
if (req.sw_count == 0) {
Loading