Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PD power seq implementation #249

Merged
merged 4 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion can/src/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TASK(CAN_TX, TASK_STACK_256)
LOG_DEBUG("can_tx called: %d!\n", counter);
counter++;

check_can_watchdogs();
check_can_watchdogs(); // TODO: handle STATUS_CODE_TIMEOUT here

can_tx_all();

Expand Down
15 changes: 5 additions & 10 deletions libraries/codegen/boards/bms_carrier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
target:
centre_console:
watchdog: 0
power_distribution:
watchdog: 3
critical: true
signals:
batt_perc:
Expand Down Expand Up @@ -74,18 +76,11 @@
fan_8:
length: 8

battery_relay_state:
battery_relay_info:
id: 58
target:
bms_carrier:
watchdog: 0
centre_console:
power_distribution:
watchdog: 0
signals:
hv:
length: 8
gnd:
state:
length: 8



2 changes: 2 additions & 0 deletions libraries/codegen/boards/motor_controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
target:
centre_console:
watchdog: 0
power_distribution:
watchdog: 0
signals:
limit_bitset_l:
length: 8
Expand Down
8 changes: 8 additions & 0 deletions libraries/codegen/boards/power_distribution.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@
signals:
fault_bitset:
length: 8
bms_relays:
id: 10
target:
bms_carrier:
watchdog: 0
signals:
relays_state:
length: 8
12 changes: 8 additions & 4 deletions libraries/codegen/templates/_rx_all.c.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{% for message, message_data in messages.items() %}
{%- if message_data["target"][board]["watchdog"] != 0 %}
static CanWatchDog s_{{message}}_msg_watchdog = {0, {{message_data["target"][board]["watchdog"] | lower}}, 0};
CanWatchDog s_{{message}}_msg_watchdog = {0, {{message_data["target"][board]["watchdog"] | lower}}, 0};
{%- endif %}
{%- endfor %}

Expand Down Expand Up @@ -45,6 +45,7 @@ void clear_rx_received() {
}

StatusCode check_can_watchdogs() {
StatusCode status = STATUS_CODE_OK;
{%- for message, message_data in messages.items() %}
{%- if message_data["target"][board]["watchdog"] != 0 %}
if (!g_rx_struct.received_{{message}}) {
Expand All @@ -53,11 +54,14 @@ StatusCode check_can_watchdogs() {
LOG_CRITICAL("DID NOT RECEIVE CAN MESSAGE: %u IN MAX CYCLES : %u\n", SYSTEM_CAN_MESSAGE_{{message_data["src"] | upper}}_{{message | upper}},
s_{{message}}_msg_watchdog.max_cycles);
s_{{message}}_msg_watchdog.missed = 1;
} else {
s_{{message}}_msg_watchdog.missed = 0;
status = STATUS_CODE_TIMEOUT;
}
} else {
s_{{message}}_msg_watchdog.cycles_over = 0;
s_{{message}}_msg_watchdog.missed = 0;
}
{%- endif %}
{%- endfor %}
return STATUS_CODE_OK;
return status;
}

14 changes: 14 additions & 0 deletions libraries/codegen/templates/_rx_structs.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
{%- set messages = data["Messages"] %}
#include <stdint.h>
#include <stdbool.h>
#include "can_watchdog.h"

{% for message, message_data in messages.items() %}
{%- if message_data["target"][board]["watchdog"] != 0 %}
#define check_{{message}}_msg_watchdog() \
s_{{message}}_msg_watchdog.missed
{%- endif %}
{%- endfor %}

typedef struct
{
Expand All @@ -16,3 +24,9 @@ typedef struct
bool received_{{message}};
{%- endfor %}
} {{board}}_rx_struct;

{%- for message, message_data in messages.items() %}
{% if message_data["target"][board]["watchdog"] != 0 %}
extern CanWatchDog s_{{message}}_msg_watchdog;
{%- endif %}
{%- endfor %}
2 changes: 1 addition & 1 deletion libraries/ms-common/inc/exported_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ typedef enum EESolarRelayOpenErrorReason {
NUM_EE_SOLAR_RELAY_OPEN_ERROR_REASON
} EESolarRelayOpenErrorReason;

enum GlobalErrorCode {
typedef enum {
NO_ERROR = 0,
PEDAL_OFFLINE,
MCI_CAN_FAULT,
Expand Down
2 changes: 1 addition & 1 deletion libraries/ms-common/inc/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#include "status.h"
#include "tasks.h"

#define FSM_PRIORITY 1
#define FSM_PRIORITY 2
// TODO(mitchellostler): make user defined
#define FSM_TASK_STACK TASK_STACK_1024
#define FSM_TIMEOUT_MS 1000
Expand Down
32 changes: 23 additions & 9 deletions projects/fsm_demo/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,38 @@
#include "fsm1.h"
#include "fsm2.h"
#include "log.h"
#include "master_task.h"
#include "tasks.h"

TASK(master_task, TASK_STACK_512) {
while (true) {
fsm_run_cycle(fsm1);
wait_tasks(1);
fsm_run_cycle(fsm2);
wait_tasks(1);
delay_ms(1000);
}
// TASK(master_task, TASK_STACK_512) {
// while (true) {
// fsm_run_cycle(fsm1);
// wait_tasks(1);
// fsm_run_cycle(fsm2);
// wait_tasks(1);
// delay_ms(1000);
// }
// }

void pre_loop_init() {}
void run_slow_cycle() {}
void run_fast_cycle() {}

void run_medium_cycle() {
fsm_run_cycle(fsm1);
wait_tasks(1);
fsm_run_cycle(fsm2);
wait_tasks(1);
delay_ms(1000);
}

int main(void) {
log_init();
tasks_init();
init_fsm1();
init_fsm2();
tasks_init_task(master_task, TASK_PRIORITY(3), NULL);
init_master_task();
// tasks_init_task(master_task, TASK_PRIORITY(2), NULL);

LOG_DEBUG("FSM Demo...\n");
tasks_start();
Expand Down
4 changes: 2 additions & 2 deletions projects/leds/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ TASK(leds_task, TASK_STACK_512) {
}

while (true) {
#ifdef MS_PLATFORM_X86
// #ifdef MS_PLATFORM_X86
LOG_DEBUG("blink\n");
#endif
// #endif
for (uint8_t i = 0; i < SIZEOF_ARRAY(leds); i++) {
gpio_toggle_state(&leds[i]);
delay_ms(50);
Expand Down
32 changes: 19 additions & 13 deletions projects/power_distribution/inc/outputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ typedef enum {
SOLAR_SENSE_2,
PEDAL,
STEERING,
LEFT_TURN,
RIGHT_TURN,
DRL_LIGHT,
BRAKE_LIGHT,

CENTER_CONSOLE,
BMS_DCDC,
MCI,
BPS_LIGHT,

SPARE_12V_1,
SPARE_12V_2,
BMS_AUX,
DRIVER_FAN,
REAR_CAM_LCD,
SPARE_5V_DCDC,

TELEMETRY,
SPARE_5V_AUX,
NUM_POWER_FSM_OUTPUTS,
} OutputPowerFsm;

typedef enum {
BPS_LIGHT = NUM_POWER_FSM_OUTPUTS,
RIGHT_TURN,
LEFT_TURN,
BRAKE_LIGHT,
NUM_OUTPUTS,
} Output;
} OutputLights;

typedef enum {
OUTPUT_STATE_OFF = 0,
Expand All @@ -57,10 +57,14 @@ typedef enum {
typedef enum {
OUTPUT_GROUP_ALL = 0, // Reserved Value
// Lights
OUTPUT_GROUP_LEFT_TURN,
OUTPUT_GROUP_RIGHT_TURN,
OUTPUT_GROUP_HAZARD,
OUTPUT_GROUP_TEST,
OUTPUT_GROUP_LIGHTS_LEFT_TURN,
OUTPUT_GROUP_LIGHTS_RIGHT_TURN,
OUTPUT_GROUP_LIGHTS_HAZARD,
// Power States
OUTPUT_GROUP_POWER_OFF,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OUTPUT_GROUP_POWER_... -> Power fsm output group
OUTPUT_GROUP_LIGHTS_... -> lights output group

OUTPUT_GROUP_POWER_ON,
OUTPUT_GROUP_POWER_DRIVE,
OUTPUT_GROUP_POWER_FAULT,
NUM_OUTPUT_GROUPS,
} OutputGroup;

Expand All @@ -70,7 +74,7 @@ typedef struct OutputMeasurementConfig {

typedef struct OutputGroupDef {
uint8_t num_outputs;
Output outputs[];
uint8_t outputs[];
} OutputGroupDef;

extern BtsLoadSwitchOutput g_output_config[NUM_OUTPUTS];
Expand All @@ -81,3 +85,5 @@ StatusCode pd_output_init(void);

// Sets an output group to an output state
StatusCode pd_set_output_group(OutputGroup group, OutputState state);

StatusCode pd_set_active_output_group(OutputGroup group);
5 changes: 3 additions & 2 deletions projects/power_distribution/inc/pd_fault.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include "status.h"
#include <stdint.h>

#include "status.h"
/*
* @brief Checks for PD fault states and updates fault bitset CAN message
* @return STATUS_CODE_OK on success or appropriate error code
*/
StatusCode check_pd_fault(void);
uint8_t check_pd_fault(void);
26 changes: 19 additions & 7 deletions projects/power_distribution/inc/power_seq_fsm.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#pragma once

#include "can.h"
#include "exported_enums.h"
#include "fsm.h"
#include "log.h"
#include "task.h"

#define NUM_POWER_SEQ_STATES 5
#define NUM_POWER_SEQ_TRANSITIONS 9
// TODO: figure out actual values for timeout
#define BMS_RESPONSE_TIMEOUT_MS 10000
#define MCI_RESPONSE_TIMEOUT_MS 12000

#define NUM_POWER_STATES 6

DECLARE_FSM(power_seq);
typedef enum PowerSeqStateId {
POWER_SEQ_INIT_STATE = 0,
POWER_SEQ_EVERYTHING_ON,
POWER_SEQ_DRIVER_CONTROLS,
POWER_SEQ_MAIN_OPERATION,
POWER_SEQ_AUX_POWER
POWER_STATE_OFF = 0,
TRANSMIT_BMS_CLOSE_RELAYS,
POWER_STATE_ON,
TURN_ON_DRIVE_OUTPUTS,
POWER_STATE_DRIVE,
POWER_STATE_FAULT,
} PowerSeqStateId;

typedef struct {
PowerSeqStateId target_state;
PowerSeqStateId latest_state;
TickType_t timer_start_ticks;
uint8_t fault;
} PowerFsmContext;

StatusCode init_power_seq(void);
12 changes: 6 additions & 6 deletions projects/power_distribution/src/lights_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ static void prv_lights_signal_blinker(SoftTimerId id) {
switch (light_id_callback) {
case EE_LIGHT_TYPE_SIGNAL_LEFT:
left_signal_state ^= 1;
pd_set_output_group(OUTPUT_GROUP_LEFT_TURN, left_signal_state);
pd_set_output_group(OUTPUT_GROUP_LIGHTS_LEFT_TURN, left_signal_state);
if (right_signal_state == OUTPUT_STATE_ON) {
right_signal_state = OUTPUT_STATE_OFF;
pd_set_output_group(OUTPUT_GROUP_RIGHT_TURN, right_signal_state);
pd_set_output_group(OUTPUT_GROUP_LIGHTS_RIGHT_TURN, right_signal_state);
}
soft_timer_start(&s_timer_single);
break;
case EE_LIGHT_TYPE_SIGNAL_RIGHT:
right_signal_state ^= 1;
pd_set_output_group(OUTPUT_GROUP_RIGHT_TURN, right_signal_state);
pd_set_output_group(OUTPUT_GROUP_LIGHTS_RIGHT_TURN, right_signal_state);
if (left_signal_state == OUTPUT_STATE_ON) {
left_signal_state = OUTPUT_STATE_OFF;
pd_set_output_group(OUTPUT_GROUP_LEFT_TURN, left_signal_state);
pd_set_output_group(OUTPUT_GROUP_LIGHTS_LEFT_TURN, left_signal_state);
}
soft_timer_start(&s_timer_single);
break;
Expand All @@ -44,13 +44,13 @@ static void prv_lights_signal_blinker(SoftTimerId id) {
}
left_signal_state ^= 1;
right_signal_state ^= 1;
pd_set_output_group(OUTPUT_GROUP_HAZARD, left_signal_state);
pd_set_output_group(OUTPUT_GROUP_LIGHTS_HAZARD, left_signal_state);
soft_timer_start(&s_timer_single);
break;
default:
left_signal_state = OUTPUT_STATE_OFF;
right_signal_state = OUTPUT_STATE_OFF;
pd_set_output_group(OUTPUT_GROUP_HAZARD, left_signal_state);
pd_set_output_group(OUTPUT_GROUP_LIGHTS_HAZARD, left_signal_state);
}
}

Expand Down
Loading