diff --git a/common/daq/can_config.json b/common/daq/can_config.json index 7d7048c1..dfbd7fd6 100644 --- a/common/daq/can_config.json +++ b/common/daq/can_config.json @@ -695,8 +695,40 @@ "msg_period":500, "msg_hlp":1, "msg_pgn":101 + }, + { + "msg_name":"dashboard_brake_status", + "msg_desc":"periodic dashboard brake health", + "signals":[ + {"sig_name": "brake_status", "type":"uint8_t", "length":1} + ], + "msg_period":500, + "msg_hlp":1, + "msg_pgn":33 + }, + { + "msg_name":"dashboard_tv_parameters", + "msg_desc":"periodic dashboard tv parameters", + "signals":[ + {"sig_name": "tv_enabled", "type":"uint8_t", "length":1}, + {"sig_name": "tv_deadband_val", "type":"uint16_t"}, + {"sig_name": "tv_intensity_val", "type":"uint16_t"}, + {"sig_name": "tv_p_val", "type":"uint16_t"} + ], + "msg_period":500, + "msg_hlp":1, + "msg_pgn":55 + }, + { + "msg_name":"dashboard_start_logging", + "msg_desc":"start logging", + "signals":[ + {"sig_name": "logging_enabled", "type":"uint8_t", "length":1} + ], + "msg_period":500, + "msg_hlp":1, + "msg_pgn":56 } - ], "rx":[ {"msg_name": "main_hb"}, @@ -712,7 +744,8 @@ {"msg_name": "coolant_temps"}, {"msg_name": "coolant_out", "callback": true}, {"msg_name": "gearbox"}, - {"msg_name": "dashboard_bl_cmd" , "callback": true} + {"msg_name": "dashboard_bl_cmd" , "callback": true}, + {"msg_name": "sdc_status"} ] }, { diff --git a/common/daq/per_dbc.dbc b/common/daq/per_dbc.dbc index 90ec567b..268db160 100644 --- a/common/daq/per_dbc.dbc +++ b/common/daq/per_dbc.dbc @@ -362,6 +362,18 @@ BO_ 2214598981 dashboard_volts_temp: 6 Dashboard SG_ volts_5v : 16|16@1+ (1,0) [0|0] "" Vector__XXX SG_ mcu_temp : 0|16@1- (1,0) [0|0] "" Vector__XXX +BO_ 2214594629 dashboard_brake_status: 1 Dashboard + SG_ brake_status : 0|1@1+ (1,0) [0|0] "" Vector__XXX + +BO_ 2214596037 dashboard_tv_parameters: 7 Dashboard + SG_ tv_p_val : 33|16@1+ (1,0) [0|0] "" Vector__XXX + SG_ tv_intensity_val : 17|16@1+ (1,0) [0|0] "" Vector__XXX + SG_ tv_deadband_val : 1|16@1+ (1,0) [0|0] "" Vector__XXX + SG_ tv_enabled : 0|1@1+ (1,0) [0|0] "" Vector__XXX + +BO_ 2214596101 dashboard_start_logging: 1 Dashboard + SG_ logging_enabled : 0|1@1+ (1,0) [0|0] "" Vector__XXX + BO_ 2148059781 fault_sync_dashboard: 3 Dashboard SG_ latched : 16|1@1+ (1,0) [0|0] "" Vector__XXX SG_ idx : 0|16@1+ (1,0) [0|0] "" Vector__XXX @@ -885,6 +897,15 @@ CM_ BO_ 2214598981 "periodic dashboard MCU temp"; CM_ SG_ 2214598981 volts_3v3 ""; CM_ SG_ 2214598981 volts_5v ""; CM_ SG_ 2214598981 mcu_temp ""; +CM_ BO_ 2214594629 "periodic dashboard brake health"; +CM_ SG_ 2214594629 brake_status ""; +CM_ BO_ 2214596037 "periodic dashboard tv parameters"; +CM_ SG_ 2214596037 tv_p_val ""; +CM_ SG_ 2214596037 tv_intensity_val ""; +CM_ SG_ 2214596037 tv_deadband_val ""; +CM_ SG_ 2214596037 tv_enabled ""; +CM_ BO_ 2214596101 "start logging"; +CM_ SG_ 2214596101 logging_enabled ""; CM_ BO_ 2148059781 "Fault status message"; CM_ SG_ 2148059781 latched ""; CM_ SG_ 2148059781 idx ""; diff --git a/common/faults/faults.c b/common/faults/faults.c index d6ba8d0c..5acfad8d 100644 --- a/common/faults/faults.c +++ b/common/faults/faults.c @@ -220,6 +220,7 @@ static uint16_t currCount; static q_handle_t *q_tx; uint16_t most_recent_latched; +q_handle_t q_fault_history; //Variables containing the index/limits of owned faults (heartbeat) static uint16_t ownedidx; @@ -315,6 +316,7 @@ void handleCallbacks(uint16_t id, bool latched) { if (!currStatus->latched) { currStatus->latched = recievedStatus.latched; most_recent_latched = GET_IDX(id); + qSendToBack(&q_fault_history, &most_recent_latched); switch(faultArray[GET_IDX(recievedStatus.f_ID)].priority) { case FAULT_WARNING: warnCount++; @@ -396,6 +398,7 @@ bool updateFault(uint16_t idx) { fault->bounces = 0; currCount++; most_recent_latched = idx; + qSendToBack(&q_fault_history, &idx); switch(fault->priority) { case FAULT_WARNING: warnCount++; @@ -593,6 +596,7 @@ static void forceFault(int id, bool state) { if (state & !statusArray[idx].latched) { currCount++; most_recent_latched = idx; + qSendToBack(&q_fault_history, &idx); switch(faultArray[idx].priority) { case FAULT_WARNING: warnCount++; @@ -665,4 +669,5 @@ void initFaultLibrary(uint8_t mcu, q_handle_t* txQ, uint32_t ext) { errorCount = 0; fatalCount = 0; currCount = 0; + qConstruct(&q_fault_history, sizeof(uint16_t)); } \ No newline at end of file diff --git a/common/phal_F4_F7/usart/usart.c b/common/phal_F4_F7/usart/usart.c index bf5fa5f4..2c4dc355 100644 --- a/common/phal_F4_F7/usart/usart.c +++ b/common/phal_F4_F7/usart/usart.c @@ -667,7 +667,6 @@ USART TX and RX interrupts - need to modify when adding a usart peripheral #ifdef STM32F407xx // USART1: - void DMA2_Stream7_IRQHandler() //TX { handleDMAxComplete(USART1_ACTIVE_IDX, DMA2_Stream7_IRQn, USART_DMA_TX); @@ -678,6 +677,7 @@ void DMA2_Stream5_IRQHandler() //RX handleDMAxComplete(USART1_ACTIVE_IDX, DMA2_Stream5_IRQn, USART_DMA_RX); } + // USART2: // Add new DMA interrupt handlers here, passing in new active struct index, along with @@ -696,6 +696,7 @@ void DMA1_Stream5_IRQHandler() //RX // Add new USART Interrupts as new peripherals are needed, // feeding in the new USART peripheral, along with active array index + void USART1_IRQHandler() { handleUsartIRQ(USART1, USART1_ACTIVE_IDX); diff --git a/source/dashboard/can/can_parse.c b/source/dashboard/can/can_parse.c index 09b5acb3..729ad21f 100644 --- a/source/dashboard/can/can_parse.c +++ b/source/dashboard/can/can_parse.c @@ -177,6 +177,23 @@ void canRxUpdate() can_data.dashboard_bl_cmd.data = msg_data_a->dashboard_bl_cmd.data; dashboard_bl_cmd_CALLBACK(msg_data_a); break; + case ID_SDC_STATUS: + can_data.sdc_status.IMD = msg_data_a->sdc_status.IMD; + can_data.sdc_status.BMS = msg_data_a->sdc_status.BMS; + can_data.sdc_status.BSPD = msg_data_a->sdc_status.BSPD; + can_data.sdc_status.BOTS = msg_data_a->sdc_status.BOTS; + can_data.sdc_status.inertia = msg_data_a->sdc_status.inertia; + can_data.sdc_status.c_estop = msg_data_a->sdc_status.c_estop; + can_data.sdc_status.main = msg_data_a->sdc_status.main; + can_data.sdc_status.r_estop = msg_data_a->sdc_status.r_estop; + can_data.sdc_status.l_estop = msg_data_a->sdc_status.l_estop; + can_data.sdc_status.HVD = msg_data_a->sdc_status.HVD; + can_data.sdc_status.hub = msg_data_a->sdc_status.hub; + can_data.sdc_status.TSMS = msg_data_a->sdc_status.TSMS; + can_data.sdc_status.pchg_out = msg_data_a->sdc_status.pchg_out; + can_data.sdc_status.stale = 0; + can_data.sdc_status.last_rx = sched.os_ticks; + break; case ID_FAULT_SYNC_PDU: can_data.fault_sync_pdu.idx = msg_data_a->fault_sync_pdu.idx; can_data.fault_sync_pdu.latched = msg_data_a->fault_sync_pdu.latched; @@ -256,6 +273,9 @@ void canRxUpdate() CHECK_STALE(can_data.gearbox.stale, sched.os_ticks, can_data.gearbox.last_rx, UP_GEARBOX); + CHECK_STALE(can_data.sdc_status.stale, + sched.os_ticks, can_data.sdc_status.last_rx, + UP_SDC_STATUS); /* END AUTO STALE CHECKS */ } @@ -295,16 +315,17 @@ bool initCANFilter() CAN1->sFilterRegister[6].FR1 = (ID_GEARBOX << 3) | 4; CAN1->sFilterRegister[6].FR2 = (ID_DASHBOARD_BL_CMD << 3) | 4; CAN1->FA1R |= (1 << 7); // configure bank 7 - CAN1->sFilterRegister[7].FR1 = (ID_FAULT_SYNC_PDU << 3) | 4; - CAN1->sFilterRegister[7].FR2 = (ID_FAULT_SYNC_MAIN_MODULE << 3) | 4; + CAN1->sFilterRegister[7].FR1 = (ID_SDC_STATUS << 3) | 4; + CAN1->sFilterRegister[7].FR2 = (ID_FAULT_SYNC_PDU << 3) | 4; CAN1->FA1R |= (1 << 8); // configure bank 8 - CAN1->sFilterRegister[8].FR1 = (ID_FAULT_SYNC_A_BOX << 3) | 4; - CAN1->sFilterRegister[8].FR2 = (ID_FAULT_SYNC_TEST_NODE << 3) | 4; + CAN1->sFilterRegister[8].FR1 = (ID_FAULT_SYNC_MAIN_MODULE << 3) | 4; + CAN1->sFilterRegister[8].FR2 = (ID_FAULT_SYNC_A_BOX << 3) | 4; CAN1->FA1R |= (1 << 9); // configure bank 9 - CAN1->sFilterRegister[9].FR1 = (ID_SET_FAULT << 3) | 4; - CAN1->sFilterRegister[9].FR2 = (ID_RETURN_FAULT_CONTROL << 3) | 4; + CAN1->sFilterRegister[9].FR1 = (ID_FAULT_SYNC_TEST_NODE << 3) | 4; + CAN1->sFilterRegister[9].FR2 = (ID_SET_FAULT << 3) | 4; CAN1->FA1R |= (1 << 10); // configure bank 10 - CAN1->sFilterRegister[10].FR1 = (ID_DAQ_COMMAND_DASHBOARD << 3) | 4; + CAN1->sFilterRegister[10].FR1 = (ID_RETURN_FAULT_CONTROL << 3) | 4; + CAN1->sFilterRegister[10].FR2 = (ID_DAQ_COMMAND_DASHBOARD << 3) | 4; /* END AUTO FILTER */ CAN1->FMR &= ~CAN_FMR_FINIT; // Enable Filters (exit filter init mode) diff --git a/source/dashboard/can/can_parse.h b/source/dashboard/can/can_parse.h index 3e0ef05e..11bde266 100644 --- a/source/dashboard/can/can_parse.h +++ b/source/dashboard/can/can_parse.h @@ -26,6 +26,9 @@ #define ID_START_BUTTON 0x4000005 #define ID_DASHBOARD_HB 0x4001905 #define ID_DASHBOARD_VOLTS_TEMP 0x4001945 +#define ID_DASHBOARD_BRAKE_STATUS 0x4000845 +#define ID_DASHBOARD_TV_PARAMETERS 0x4000dc5 +#define ID_DASHBOARD_START_LOGGING 0x4000e05 #define ID_FAULT_SYNC_DASHBOARD 0x8ca85 #define ID_DAQ_RESPONSE_DASHBOARD 0x17ffffc5 #define ID_MAIN_HB 0x4001901 @@ -42,6 +45,7 @@ #define ID_COOLANT_OUT 0x40008df #define ID_GEARBOX 0x10000901 #define ID_DASHBOARD_BL_CMD 0x409c47e +#define ID_SDC_STATUS 0xc000381 #define ID_FAULT_SYNC_PDU 0x8cadf #define ID_FAULT_SYNC_MAIN_MODULE 0x8ca01 #define ID_FAULT_SYNC_A_BOX 0x8ca44 @@ -59,6 +63,9 @@ #define DLC_START_BUTTON 1 #define DLC_DASHBOARD_HB 1 #define DLC_DASHBOARD_VOLTS_TEMP 6 +#define DLC_DASHBOARD_BRAKE_STATUS 1 +#define DLC_DASHBOARD_TV_PARAMETERS 7 +#define DLC_DASHBOARD_START_LOGGING 1 #define DLC_FAULT_SYNC_DASHBOARD 3 #define DLC_DAQ_RESPONSE_DASHBOARD 8 #define DLC_MAIN_HB 2 @@ -75,6 +82,7 @@ #define DLC_COOLANT_OUT 3 #define DLC_GEARBOX 2 #define DLC_DASHBOARD_BL_CMD 5 +#define DLC_SDC_STATUS 2 #define DLC_FAULT_SYNC_PDU 3 #define DLC_FAULT_SYNC_MAIN_MODULE 3 #define DLC_FAULT_SYNC_A_BOX 3 @@ -135,6 +143,27 @@ data_a->dashboard_volts_temp.volts_3v3 = volts_3v3_;\ qSendToBack(&queue, &msg);\ } while(0) +#define SEND_DASHBOARD_BRAKE_STATUS(queue, brake_status_) do {\ + CanMsgTypeDef_t msg = {.Bus=CAN1, .ExtId=ID_DASHBOARD_BRAKE_STATUS, .DLC=DLC_DASHBOARD_BRAKE_STATUS, .IDE=1};\ + CanParsedData_t* data_a = (CanParsedData_t *) &msg.Data;\ + data_a->dashboard_brake_status.brake_status = brake_status_;\ + qSendToBack(&queue, &msg);\ + } while(0) +#define SEND_DASHBOARD_TV_PARAMETERS(queue, tv_enabled_, tv_deadband_val_, tv_intensity_val_, tv_p_val_) do {\ + CanMsgTypeDef_t msg = {.Bus=CAN1, .ExtId=ID_DASHBOARD_TV_PARAMETERS, .DLC=DLC_DASHBOARD_TV_PARAMETERS, .IDE=1};\ + CanParsedData_t* data_a = (CanParsedData_t *) &msg.Data;\ + data_a->dashboard_tv_parameters.tv_enabled = tv_enabled_;\ + data_a->dashboard_tv_parameters.tv_deadband_val = tv_deadband_val_;\ + data_a->dashboard_tv_parameters.tv_intensity_val = tv_intensity_val_;\ + data_a->dashboard_tv_parameters.tv_p_val = tv_p_val_;\ + qSendToBack(&queue, &msg);\ + } while(0) +#define SEND_DASHBOARD_START_LOGGING(queue, logging_enabled_) do {\ + CanMsgTypeDef_t msg = {.Bus=CAN1, .ExtId=ID_DASHBOARD_START_LOGGING, .DLC=DLC_DASHBOARD_START_LOGGING, .IDE=1};\ + CanParsedData_t* data_a = (CanParsedData_t *) &msg.Data;\ + data_a->dashboard_start_logging.logging_enabled = logging_enabled_;\ + qSendToBack(&queue, &msg);\ + } while(0) #define SEND_FAULT_SYNC_DASHBOARD(queue, idx_, latched_) do {\ CanMsgTypeDef_t msg = {.Bus=CAN1, .ExtId=ID_FAULT_SYNC_DASHBOARD, .DLC=DLC_FAULT_SYNC_DASHBOARD, .IDE=1};\ CanParsedData_t* data_a = (CanParsedData_t *) &msg.Data;\ @@ -166,6 +195,7 @@ #define UP_COOLANT_TEMPS 200 #define UP_COOLANT_OUT 1000 #define UP_GEARBOX 2000 +#define UP_SDC_STATUS 200 /* END AUTO UP DEFS */ #define CHECK_STALE(stale, curr, last, period) if(!stale && \ @@ -221,6 +251,18 @@ typedef union { uint64_t volts_5v: 16; uint64_t volts_3v3: 16; } dashboard_volts_temp; + struct { + uint64_t brake_status: 1; + } dashboard_brake_status; + struct { + uint64_t tv_enabled: 1; + uint64_t tv_deadband_val: 16; + uint64_t tv_intensity_val: 16; + uint64_t tv_p_val: 16; + } dashboard_tv_parameters; + struct { + uint64_t logging_enabled: 1; + } dashboard_start_logging; struct { uint64_t idx: 16; uint64_t latched: 1; @@ -342,6 +384,21 @@ typedef union { uint64_t cmd: 8; uint64_t data: 32; } dashboard_bl_cmd; + struct { + uint64_t IMD: 1; + uint64_t BMS: 1; + uint64_t BSPD: 1; + uint64_t BOTS: 1; + uint64_t inertia: 1; + uint64_t c_estop: 1; + uint64_t main: 1; + uint64_t r_estop: 1; + uint64_t l_estop: 1; + uint64_t HVD: 1; + uint64_t hub: 1; + uint64_t TSMS: 1; + uint64_t pchg_out: 1; + } sdc_status; struct { uint64_t idx: 16; uint64_t latched: 1; @@ -516,6 +573,23 @@ typedef struct { uint8_t cmd; uint32_t data; } dashboard_bl_cmd; + struct { + uint8_t IMD; + uint8_t BMS; + uint8_t BSPD; + uint8_t BOTS; + uint8_t inertia; + uint8_t c_estop; + uint8_t main; + uint8_t r_estop; + uint8_t l_estop; + uint8_t HVD; + uint8_t hub; + uint8_t TSMS; + uint8_t pchg_out; + uint8_t stale; + uint32_t last_rx; + } sdc_status; struct { uint16_t idx; uint8_t latched; diff --git a/source/dashboard/hdd/hdd.c b/source/dashboard/hdd/hdd.c deleted file mode 100644 index e69de29b..00000000 diff --git a/source/dashboard/hdd/hdd.h b/source/dashboard/hdd/hdd.h deleted file mode 100644 index fd898d3f..00000000 --- a/source/dashboard/hdd/hdd.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef __HDD_H__ -#define __HDD_H__ - - -typedef struct { - uint8_t deadband_pos; - uint8_t intensity_pos; - uint8_t deadband_prev; - uint8_t intensity_prev; -} hdd_value_t; - -#endif diff --git a/source/dashboard/lcd/lcd.c b/source/dashboard/lcd/lcd.c index 77ba915b..05f9200f 100644 --- a/source/dashboard/lcd/lcd.c +++ b/source/dashboard/lcd/lcd.c @@ -1,5 +1,4 @@ #include "lcd.h" -#include "hdd.h" #include "common/psched/psched.h" #include "pedals.h" #include "common/faults/faults.h" @@ -8,55 +7,158 @@ #include #include -volatile page_t curr_page; -volatile page_t prev_page; +volatile page_t curr_page; // Current page displayed on the LCD +volatile page_t prev_page; // Previous page displayed on the LCD +uint16_t cur_fault_buf_ndx; // Current index in the fault buffer +volatile uint16_t fault_buf[5] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}; // Buffer of displayed faults +bool sendFirsthalf; // Flag for sending data to data page +char *errorText; // Pointer to data to display for the Error, Warning, and Critical Fault codes +extern uint16_t filtered_pedals; // Global from pedals module for throttle display +extern q_handle_t q_tx_can; // Global queue for CAN tx +extern q_handle_t q_fault_history; // Global queue from fault library for fault history +volatile settings_t settings; // Data for the settings page +volatile tv_settings_t tv_settings; // Data for the tvsettings page +volatile driver_config_t driver_config; // Data for the driver page +extern lcd_t lcd_data; +uint8_t fault_time_displayed; // Amount of units of time that the fault has been shown to the driver -bool sendFirsthalf; +// Call initially to ensure the LCD is initialized to the proper value - +// should be replaced with the struct prev page stuff eventually +bool zeroEncoder(volatile int8_t* start_pos) +{ + // Collect initial raw reading from encoder + uint8_t raw_enc_a = PHAL_readGPIO(ENC_A_GPIO_Port, ENC_A_Pin); + uint8_t raw_enc_b = PHAL_readGPIO(ENC_B_GPIO_Port, ENC_B_Pin); + uint8_t raw_res = (raw_enc_b | (raw_enc_a << 1)); + *start_pos = raw_res; + lcd_data.encoder_position = 0; -uint8_t display_time; - -char *errorText; - -static uint8_t preflight; - -extern hdd_value_t hdd; - -extern uint16_t filtered_pedals; - -extern q_handle_t q_tx_can; - -volatile tv_options_t tv; - -volatile settings_t settings; - -//1 = deadband, 0 = intensity -bool knob; -bool knob_old; - -bool midAdjustment; + // Set page (leave preflight) + updatePage(); + return true; +} +// Initialize the LCD screen +// Preflight will be shown on power on, then reset to RACE void initLCD() { curr_page = PAGE_RACE; - prev_page = PAGE_PREFLIGHT; - display_time = 0; + prev_page = PAGE_PREFLIGHT; errorText = 0; - knob = false; - knob_old = false; - midAdjustment = 0; - tv = (tv_options_t) {0, 0, 0, 0, 0, 0, "\0"}; settings = (settings_t) {0, 0, 0, 0, 0, 0, 0, 0}; sendFirsthalf = true; - + tv_settings = (tv_settings_t) {true, 0, 12, 10, 10}; } void updatePage() { + // Only update the encoder if we are on a "selectable" page + if ((curr_page != PAGE_ERROR) && (curr_page != PAGE_WARNING) && (curr_page != PAGE_FATAL)) + { + curr_page = lcd_data.encoder_position; + fault_time_displayed = 0; + } + + // If we do not detect a page update (most notably detect if encoder did not move), do nothing if (curr_page == prev_page) { return; } + + // Parsed value represents: char parsed_value[3] = "\0"; + + // Parse the page that was passed into the function switch (curr_page) { + case PAGE_LOGGING: + prev_page = PAGE_LOGGING; + set_page(LOGGING_STRING); + break; + case PAGE_DRIVER: + prev_page = PAGE_DRIVER; + set_page(DRIVER_STRING); + + driver_config.curr_hover = DRIVER_DEFAULT_SELECT; + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_BG); + + if (driver_config.curr_select == DRIVER_DEFAULT_SELECT) + { + set_value(DRIVER_DEFAULT_OP, NXT_VALUE, 1); + } + else + { + set_value(DRIVER_DEFAULT_OP, NXT_VALUE, 0); + } + + if (driver_config.curr_select == DRIVER_TYLER_SELECT) + { + set_value(DRIVER_TYLER_OP, NXT_VALUE, 1); + } + else + { + set_value(DRIVER_TYLER_OP, NXT_VALUE, 0); + } + + if (driver_config.curr_select == DRIVER_RUHAAN_SELECT) + { + set_value(DRIVER_RUHAAN_OP, NXT_VALUE, 1); + } + else + { + set_value(DRIVER_RUHAAN_OP, NXT_VALUE, 0); + } + + if (driver_config.curr_select == DRIVER_LUKE_SELECT) + { + set_value(DRIVER_LUKE_OP, NXT_VALUE, 1); + } + else + { + set_value(DRIVER_LUKE_OP, NXT_VALUE, 0); + } + break; + + case PAGE_SDCINFO: + prev_page = PAGE_SDCINFO; + set_page(SDCINFO_STRING); + break; + case PAGE_TVSETTINGS: + prev_page = PAGE_TVSETTINGS; + + // Switch page + set_page(TVSETTINGS_STRING); + + // Establish hover position + tv_settings.curr_hover = TV_INTENSITY_HOVER; + + // Set background colors + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + + // Set displayed data + set_value(TV_INTENSITY_FLT, NXT_VALUE, tv_settings.tv_intensity_val); + set_value(TV_PROPORTION_FLT, NXT_VALUE, tv_settings.tv_p_val); + set_text(TV_DEAD_TXT, NXT_TEXT, int_to_char(tv_settings.tv_deadband_val, parsed_value)); + bzero(parsed_value, 3); + set_value(TV_ENABLE_OP, NXT_VALUE, tv_settings.tv_enable_selected); + break; + + case PAGE_ERROR: + set_page(ERR_STRING); + set_text(ERR_TXT, NXT_TEXT, errorText); + break; + case PAGE_WARNING: + set_page(WARN_STRING); + set_text(ERR_TXT, NXT_TEXT, errorText); + break; + case PAGE_FATAL: + set_page(FATAL_STRING); + set_text(ERR_TXT, NXT_TEXT, errorText); + break; case PAGE_RACE: - prev_page = PAGE_RACE; + prev_page = PAGE_RACE; set_page(RACE_STRING); break; case PAGE_DATA: @@ -64,27 +166,38 @@ void updatePage() { set_page(DATA_STRING); break; case PAGE_SETTINGS: + // Show page prev_page = PAGE_SETTINGS; set_page(SETTINGS_STRING); - settings.curr_hover = DT_FAN_HOVER; - set_value(DT_FAN_TXT, NXT_BACKGROUND_COLOR, SETTINGS_HOVER_BG); - set_value(DT_FAN_BAR, NXT_VALUE, settings.d_fan_val); - set_value(DT_FAN_VAL, NXT_FONT_COLOR, SETTINGS_BAR_BG); - set_text(DT_FAN_VAL, NXT_TEXT, int_to_char(settings.d_fan_val, parsed_value)); - bzero(parsed_value, 3); - if (settings.d_pump_selected) { + + settings.curr_hover = DT_FAN_HOVER; // Set hover + set_value(DT_FAN_TXT, NXT_BACKGROUND_COLOR, SETTINGS_HOVER_BG); // Set t2 with settings hover + set_value(DT_FAN_BAR, NXT_VALUE, settings.d_fan_val); // Set progress bar for j0 + set_value(DT_FAN_VAL, NXT_FONT_COLOR, SETTINGS_BAR_BG); // Set color for t8 (background of bar?) + set_text(DT_FAN_VAL, NXT_TEXT, int_to_char(settings.d_fan_val, parsed_value)); // Set fan value for t8 + bzero(parsed_value, 3); // Clear our char buffer + + // Set drivetrain pump selector color + if (settings.d_pump_selected) { set_value(DT_PUMP_OP, NXT_FONT_COLOR, SETTINGS_UV_SELECT); } else { set_value(DT_PUMP_OP, NXT_BACKGROUND_COLOR, SETTINGS_HOVER_BG); } + + // Set drivetrain pump selector status set_value(DT_PUMP_OP, NXT_VALUE, settings.d_pump_selected); + + // Set Battery fan c3 (Pump 1?) + // todo: Why is this here? if (settings.b_fan2_selected) { set_value(B_FAN2_OP, NXT_FONT_COLOR, SETTINGS_UV_SELECT); } else { set_value(B_FAN2_OP, NXT_BACKGROUND_COLOR, SETTINGS_HOVER_BG); } + + // Set value for c3 battery pump 1 set_value(B_FAN2_OP, NXT_VALUE, settings.b_fan2_selected); if (settings.b_pump_selected) { set_value(B_PUMP_OP, NXT_FONT_COLOR, SETTINGS_UV_SELECT); @@ -92,204 +205,189 @@ void updatePage() { else { set_value(B_PUMP_OP, NXT_BACKGROUND_COLOR, SETTINGS_HOVER_BG); } + + // Set Battery Pump 2 value set_value(B_PUMP_OP, NXT_VALUE, settings.b_pump_selected); + + // Set battery fan bar, text, color set_value(B_FAN1_BAR, NXT_VALUE, settings.b_fan_val); set_text(B_FAN1_VAL, NXT_TEXT, int_to_char(settings.b_fan_val, parsed_value)); + bzero(parsed_value, 3); set_value(B_FAN1_VAL, NXT_FONT_COLOR, SETTINGS_BAR_BG); - break; - case PAGE_TV: - prev_page = PAGE_TV; - set_page(TV_STRING); - tv.p_hover = true; - set_value(P_BAR, NXT_VALUE, tv.yaw_p_val); - set_value(I_BAR, NXT_VALUE, tv.yaw_i_val); - switch (tv.p_selected) { - case NONE_SELECTED: - set_value(P_BAR, NXT_BACKGROUND_COLOR, TV_HOVER_BG); - set_value(P_BAR, NXT_FONT_COLOR, TV_HOVER_FG_P); - break; - case P_SELECTED: - set_value(P_BAR, NXT_BACKGROUND_COLOR, BLACK); - set_value(P_BAR, NXT_FONT_COLOR, WHITE); - break; - case I_SELECTED: - set_value(I_BAR, NXT_BACKGROUND_COLOR, BLACK); - set_value(I_BAR, NXT_FONT_COLOR, WHITE); - break; + + case PAGE_FAULTS: + prev_page = PAGE_FAULTS; + set_page(FAULT_STRING); + if (fault_buf[0] == 0xFFFF) + { + set_text(FAULT_1_TXT, NXT_TEXT, FAULT_NONE_STRING); + set_value(FLT_STAT_1_TXT, NXT_BACKGROUND_COLOR, WHITE); } - send_p_val(); - send_i_val(); - char value_to_send[2] = "\0"; - if (hdd.intensity_pos > 9) { - value_to_send[0] = (hdd.intensity_pos / 10) + 48; - value_to_send[1] = (hdd.intensity_pos % 10) + 48; + else + { + set_text(FAULT_1_TXT, NXT_TEXT, faultArray[fault_buf[0]].screen_MSG); + // if (checkFault(fault_buf[fault_buf[0]]) == false) + // { + // set_value(FLT_STAT_1_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + // } + // else + // { + // set_value(FLT_STAT_1_TXT, NXT_BACKGROUND_COLOR, RED); + // } } - else { - value_to_send[0] = hdd.intensity_pos + 48; + if (fault_buf[1] == 0xFFFF) + { + set_text(FAULT_2_TXT, NXT_TEXT, FAULT_NONE_STRING); + set_value(FLT_STAT_2_TXT, NXT_BACKGROUND_COLOR, WHITE); } - set_text(TV_IN_TXT, NXT_TEXT, value_to_send); - tv.deadband_msg = get_deadband(); - set_text(TV_DB_TXT, NXT_TEXT, tv.deadband_msg); - break; - case PAGE_KNOBS: - switch (display_time++) { - case 0: - if (knob) { - set_page(DEADBAND_STRING); - tv.deadband_msg = get_deadband(); - set_text(KNB_TXT, NXT_TEXT, tv.deadband_msg); - } - else { - set_page(INTENSITY_STRING); - char value_to_send[2] = "\0"; - if (hdd.intensity_pos > 9) { - value_to_send[0] = (hdd.intensity_pos / 10) + 48; - value_to_send[1] = (hdd.intensity_pos % 10) + 48; - } - else { - value_to_send[0] = hdd.intensity_pos + 48; - } - set_text(KNB_TXT, NXT_TEXT, value_to_send); - } - break; - case 5: - set_value(TIME_BAR, NXT_VALUE, 100); - display_time = 0; - curr_page = prev_page; - prev_page = PAGE_PREFLIGHT; - updatePage(); - break; - default: - set_value(TIME_BAR, NXT_VALUE, (display_time * 20)); - break; + else + { + set_text(FAULT_2_TXT, NXT_TEXT, faultArray[fault_buf[1]].screen_MSG); + // if (checkFault(fault_buf[fault_buf[1]]) == false) + // { + // set_value(FLT_STAT_2_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + // } + // else + // { + // set_value(FLT_STAT_2_TXT, NXT_BACKGROUND_COLOR, RED); + // } } - break; - default: - switch(display_time++) { - case 0: - switch(curr_page) { - case PAGE_ERROR: - set_page(ERR_STRING); - break; - case PAGE_WARNING: - set_page(WARN_STRING); - break; - case PAGE_FATAL: - set_page(FATAL_STRING); - break; - } - set_value(TIME_BAR, NXT_VALUE, 0); - set_text(ERR_TXT, NXT_TEXT, errorText); - break; - case 10: - set_value(TIME_BAR, NXT_VALUE, 100); - curr_page = prev_page; - prev_page = PAGE_PREFLIGHT; - updatePage(); - display_time = 0; - break; - default: - set_value(TIME_BAR, NXT_VALUE, (display_time * 10)); - break; + + if (fault_buf[2] == 0xFFFF) + { + set_text(FAULT_3_TXT, NXT_TEXT, FAULT_NONE_STRING); + set_value(FLT_STAT_3_TXT, NXT_BACKGROUND_COLOR, WHITE); + } + else + { + set_text(FAULT_3_TXT, NXT_TEXT, faultArray[fault_buf[2]].screen_MSG); + // if (checkFault(fault_buf[fault_buf[2]]) == false) + // { + // set_value(FLT_STAT_3_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + // } + // else + // { + // set_value(FLT_STAT_3_TXT, NXT_BACKGROUND_COLOR, RED); + // } } - } -} -void moveLeft() { - switch(curr_page) { - case PAGE_RACE: - curr_page = PAGE_TV; - updatePage(); - break; - case PAGE_SETTINGS: - if (settings.curr_hover < DT_FAN_SELECT) { - curr_page = PAGE_DATA; - updatePage(); + if (fault_buf[3] == 0xFFFF) + { + set_text(FAULT_4_TXT, NXT_TEXT, FAULT_NONE_STRING); + set_value(FLT_STAT_4_TXT, NXT_BACKGROUND_COLOR, WHITE); } - break; - case PAGE_DATA: - curr_page = PAGE_RACE; - updatePage(); - break; - case PAGE_TV: - if (tv.p_selected == NONE_SELECTED) { - curr_page = PAGE_SETTINGS; - updatePage(); + else + { + set_text(FAULT_4_TXT, NXT_TEXT, faultArray[fault_buf[3]].screen_MSG); + // if (checkFault(fault_buf[fault_buf[3]]) == false) + // { + // set_value(FLT_STAT_4_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + // } + // else + // { + // set_value(FLT_STAT_4_TXT, NXT_BACKGROUND_COLOR, RED); + // } } - break; - default: - curr_page = prev_page; - prev_page = PAGE_PREFLIGHT; - display_time = 0; - updatePage(); - break; - } -} -void moveRight() { - switch(curr_page) { - case PAGE_RACE: - curr_page = PAGE_DATA; - updatePage(); - break; - case PAGE_SETTINGS: - if (settings.curr_hover < DT_FAN_SELECT) { - curr_page = PAGE_TV; - updatePage(); + if (fault_buf[4] == 0xFFFF) + { + set_text(FAULT_5_TXT, NXT_TEXT, FAULT_NONE_STRING); + set_value(FLT_STAT_5_TXT, NXT_BACKGROUND_COLOR, WHITE); } - break; - case PAGE_DATA: - curr_page = PAGE_SETTINGS; - updatePage(); - break; - case PAGE_TV: - if (tv.p_selected == NONE_SELECTED) { - curr_page = PAGE_RACE; - updatePage(); + else + { + set_text(FAULT_5_TXT, NXT_TEXT, faultArray[fault_buf[4]].screen_MSG); + // if (checkFault(fault_buf[fault_buf[4]]) == false) + // { + // set_value(FLT_STAT_5_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + // } + // else + // { + // set_value(FLT_STAT_5_TXT, NXT_BACKGROUND_COLOR, RED); + // } } - break; - default: - curr_page = prev_page; - prev_page = PAGE_PREFLIGHT; - display_time = 0; - updatePage(); - break; + break; } } void moveUp() { - if (((curr_page != PAGE_SETTINGS) || (curr_page != PAGE_TV))&& curr_page > PAGE_TV) { - curr_page = prev_page; - prev_page = PAGE_PREFLIGHT; - display_time = 0; - updatePage(); - return; - } - if (curr_page == PAGE_TV) { - if (tv.p_hover && tv.p_selected == NONE_SELECTED) { - tv.p_hover = false; - set_value(P_BAR, NXT_BACKGROUND_COLOR, TV_BG); - set_value(P_BAR, NXT_FONT_COLOR, TV_P_FG); - set_value(I_BAR, NXT_BACKGROUND_COLOR, TV_HOVER_BG); - set_value(I_BAR, NXT_FONT_COLOR, TV_HOVER_FG_I); - } - else if (tv.p_selected == NONE_SELECTED) { - tv.p_hover = true; - set_value(P_BAR, NXT_BACKGROUND_COLOR, TV_HOVER_BG); - set_value(P_BAR, NXT_FONT_COLOR, TV_HOVER_FG_P); - set_value(I_BAR, NXT_BACKGROUND_COLOR, TV_BG); - set_value(I_BAR, NXT_FONT_COLOR, TV_I_FG); - } - else if (tv.p_selected == P_SELECTED){ - tv.yaw_p_val = ((tv.yaw_p_val >= 100) ? 0 : tv.yaw_p_val + 10); - set_value(P_BAR, NXT_VALUE, tv.yaw_p_val); - send_p_val(); - } - else { - tv.yaw_i_val = ((tv.yaw_i_val >= 100) ? 0 : tv.yaw_i_val + 10); - set_value(I_BAR, NXT_VALUE, tv.yaw_i_val); - send_i_val(); + char parsed_value[3] = "\0"; + if (curr_page == PAGE_TVSETTINGS) + { + // If Intensity is selected + if (tv_settings.curr_hover == TV_INTENSITY_SELECTED) + { + // Increase the intensity value + tv_settings.tv_intensity_val = (tv_settings.tv_intensity_val + 1) % 100; + + // Update the page items + set_value(TV_INTENSITY_FLT, NXT_VALUE, tv_settings.tv_intensity_val); + } + else if (tv_settings.curr_hover == TV_INTENSITY_HOVER) + { + // Wrap around to enable + tv_settings.curr_hover = TV_ENABLE_HOVER; + + // Update the background + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + } + else if (tv_settings.curr_hover == TV_P_SELECTED) + { + // Increase the p value + tv_settings.tv_p_val = (tv_settings.tv_p_val + 1) % 100; + + // Update the page items + set_value(TV_PROPORTION_FLT, NXT_VALUE, tv_settings.tv_p_val); + + } + else if (tv_settings.curr_hover == TV_P_HOVER) + { + // Scroll up to Intensity + tv_settings.curr_hover = TV_INTENSITY_HOVER; + + // Update the background + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (tv_settings.curr_hover == TV_DEADBAND_SELECTED) + { + // Increase the deadband value + tv_settings.tv_deadband_val = (tv_settings.tv_deadband_val + 1) % 30; + + // Update the page items + set_text(TV_DEAD_TXT, NXT_TEXT, int_to_char(tv_settings.tv_deadband_val, parsed_value)); + bzero(parsed_value, 3); + + } + else if (tv_settings.curr_hover == TV_DEADBAND_HOVER) + { + // Scroll up to P + tv_settings.curr_hover = TV_P_HOVER; + + // Update the background + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (tv_settings.curr_hover == TV_ENABLE_HOVER) + { + // Scroll up to deadband + tv_settings.curr_hover = TV_DEADBAND_HOVER; + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else + { + // ? } } else if (curr_page == PAGE_SETTINGS) { @@ -326,6 +424,7 @@ void moveUp() { settings.d_fan_val = (settings.d_fan_val == 100) ? 0 : settings.d_fan_val + 10; set_value(DT_FAN_BAR, NXT_VALUE, settings.d_fan_val); set_text(DT_FAN_VAL, NXT_TEXT, int_to_char(settings.d_fan_val, parsed_value)); + bzero(parsed_value, 3); set_value(DT_FAN_VAL, NXT_FONT_COLOR, BLACK); break; case FAN1_SELECT: @@ -334,44 +433,152 @@ void moveUp() { settings.b_fan_val = (settings.b_fan_val == 100) ? 0 : settings.b_fan_val + 10; set_value(B_FAN1_BAR, NXT_VALUE, settings.b_fan_val); set_text(B_FAN1_VAL, NXT_TEXT, int_to_char(settings.b_fan_val, parsed_value)); + bzero(parsed_value, 3); set_value(B_FAN1_VAL, NXT_FONT_COLOR, BLACK); break; } } + else if (curr_page == PAGE_DRIVER) + { + if (driver_config.curr_hover == DRIVER_DEFAULT_SELECT) + { + // Wrap around to enable + driver_config.curr_hover = DRIVER_LUKE_SELECT; + driver_config.curr_select = DRIVER_LUKE_SELECT; + // Update the background + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + } + else if (driver_config.curr_hover == DRIVER_TYLER_SELECT) + { + driver_config.curr_hover = DRIVER_DEFAULT_SELECT; + driver_config.curr_select = DRIVER_DEFAULT_SELECT; + + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (driver_config.curr_hover == DRIVER_RUHAAN_SELECT) + { + driver_config.curr_hover = DRIVER_TYLER_SELECT; + driver_config.curr_select = DRIVER_TYLER_SELECT; + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (driver_config.curr_hover == DRIVER_LUKE_SELECT) + { + driver_config.curr_hover = DRIVER_RUHAAN_SELECT; + driver_config.curr_select = DRIVER_RUHAAN_SELECT; + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_BG); + } + } } void moveDown() { - if (((curr_page != PAGE_SETTINGS) || (curr_page != PAGE_TV))&& curr_page > PAGE_TV) { - curr_page = prev_page; - prev_page = PAGE_PREFLIGHT; - display_time = 0; - updatePage(); - return; - } - if (curr_page == PAGE_TV) { - if (tv.p_hover && tv.p_selected == NONE_SELECTED) { - tv.p_hover = false; - set_value(P_BAR, NXT_BACKGROUND_COLOR, TV_BG); - set_value(P_BAR, NXT_FONT_COLOR, TV_P_FG); - set_value(I_BAR, NXT_BACKGROUND_COLOR, TV_HOVER_BG); - set_value(I_BAR, NXT_FONT_COLOR, TV_HOVER_FG_I); - } - else if (tv.p_selected == NONE_SELECTED) { - tv.p_hover = true; - set_value(P_BAR, NXT_BACKGROUND_COLOR, TV_HOVER_BG); - set_value(P_BAR, NXT_FONT_COLOR, TV_HOVER_FG_P); - set_value(I_BAR, NXT_BACKGROUND_COLOR, TV_BG); - set_value(I_BAR, NXT_FONT_COLOR, TV_I_FG); - } - else if (tv.p_selected == P_SELECTED){ - tv.yaw_p_val = ((tv.yaw_p_val <= 0) ? 100 : tv.yaw_p_val - 10); - set_value(P_BAR, NXT_VALUE, tv.yaw_p_val); - send_p_val(); - } - else { - tv.yaw_i_val = ((tv.yaw_i_val <= 0) ? 100 : tv.yaw_i_val - 10); - set_value(I_BAR, NXT_VALUE, tv.yaw_i_val); - send_i_val(); + char parsed_value[3] = "\0"; + if (curr_page == PAGE_TVSETTINGS) + { + if (tv_settings.curr_hover == TV_INTENSITY_SELECTED) + { + // Decrease the intensity value + if (tv_settings.tv_intensity_val == 0) + { + tv_settings.tv_intensity_val = 100; + } + else + { + tv_settings.tv_intensity_val--; + } + + // Update the page item + set_value(TV_INTENSITY_FLT, NXT_VALUE, tv_settings.tv_intensity_val); + } + else if (tv_settings.curr_hover == TV_INTENSITY_HOVER) + { + // Scroll down to P + tv_settings.curr_hover = TV_P_HOVER; + + // Update the background + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (tv_settings.curr_hover == TV_P_SELECTED) + { + // Decrease the P value + if (tv_settings.tv_p_val == 0) + { + tv_settings.tv_p_val = 100; + } + else + { + tv_settings.tv_p_val--; + } + + // Update the page items + set_value(TV_PROPORTION_FLT, NXT_VALUE, tv_settings.tv_p_val); + + } + else if (tv_settings.curr_hover == TV_P_HOVER) + { + // Scroll down to deadband + tv_settings.curr_hover = TV_DEADBAND_HOVER; + + // Update the background + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (tv_settings.curr_hover == TV_DEADBAND_SELECTED) + { + // Decrease the deadband value + if (tv_settings.tv_deadband_val == 0) + { + tv_settings.tv_deadband_val = 30; + } + else + { + tv_settings.tv_deadband_val--; + } + + // Update the page items + set_text(TV_DEAD_TXT, NXT_TEXT, int_to_char(tv_settings.tv_deadband_val, parsed_value)); + bzero(parsed_value, 3); + + } + else if (tv_settings.curr_hover == TV_DEADBAND_HOVER) + { + // Scroll down to enable + tv_settings.curr_hover = TV_ENABLE_HOVER; + + // Update the background + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + } + else if (tv_settings.curr_hover == TV_ENABLE_HOVER) + { + // Scroll down to intensity + tv_settings.curr_hover = TV_INTENSITY_HOVER; + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else + { + // ? } } else if (curr_page == PAGE_SETTINGS) { @@ -408,6 +615,7 @@ void moveDown() { settings.d_fan_val = (settings.d_fan_val == 0) ? 100 : settings.d_fan_val - 10; set_value(DT_FAN_BAR, NXT_VALUE, settings.d_fan_val); set_text(DT_FAN_VAL, NXT_TEXT, int_to_char(settings.d_fan_val, parsed_value)); + bzero(parsed_value, 3); set_value(DT_FAN_VAL, NXT_FONT_COLOR, BLACK); break; case FAN1_SELECT: @@ -416,41 +624,140 @@ void moveDown() { settings.b_fan_val = (settings.b_fan_val == 0) ? 100 : settings.b_fan_val - 10; set_value(B_FAN1_BAR, NXT_VALUE, settings.b_fan_val); set_text(B_FAN1_VAL, NXT_TEXT, int_to_char(settings.b_fan_val, parsed_value)); + bzero(parsed_value, 3); set_value(B_FAN1_VAL, NXT_FONT_COLOR, BLACK); break; } } + else if (curr_page == PAGE_DRIVER) + { + if (driver_config.curr_hover == DRIVER_DEFAULT_SELECT) + { + driver_config.curr_hover = DRIVER_TYLER_SELECT; + driver_config.curr_select = DRIVER_TYLER_SELECT; + // Update the background + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (driver_config.curr_hover == DRIVER_TYLER_SELECT) + { + driver_config.curr_hover = DRIVER_RUHAAN_SELECT; + driver_config.curr_select = DRIVER_RUHAAN_SELECT; + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (driver_config.curr_hover == DRIVER_RUHAAN_SELECT) + { + driver_config.curr_hover = DRIVER_LUKE_SELECT; + driver_config.curr_select = DRIVER_LUKE_SELECT; + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + } + else if (driver_config.curr_hover == DRIVER_LUKE_SELECT) + { + driver_config.curr_hover = DRIVER_DEFAULT_SELECT; + driver_config.curr_select = DRIVER_DEFAULT_SELECT; + set_value(DRIVER_DEFAULT_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(DRIVER_TYLER_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_RUHAAN_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(DRIVER_LUKE_TXT, NXT_BACKGROUND_COLOR, TV_BG); + } + } } void selectItem() { - if (((curr_page != PAGE_SETTINGS) || (curr_page != PAGE_TV))&& curr_page > PAGE_TV) { + // User has selected to clear the current fault screen + if ((curr_page == PAGE_ERROR) || (curr_page == PAGE_FATAL) || (curr_page == PAGE_WARNING)) + { + // Go back to where we were before curr_page = prev_page; + // so select item doesnt't break prev_page = PAGE_PREFLIGHT; - display_time = 0; + fault_time_displayed = 0; updatePage(); - return; } - if (curr_page == PAGE_TV) { - if (tv.p_hover && tv.p_selected == NONE_SELECTED) { - set_value(P_BAR, NXT_BACKGROUND_COLOR, BLACK); - set_value(P_BAR, NXT_FONT_COLOR, WHITE); - tv.p_selected = P_SELECTED; - } - else if (tv.p_selected == NONE_SELECTED) { - set_value(I_BAR, NXT_BACKGROUND_COLOR, BLACK); - set_value(I_BAR, NXT_FONT_COLOR, WHITE); - tv.p_selected = I_SELECTED; - } - else if (tv.p_selected == P_SELECTED){ - tv.p_selected = NONE_SELECTED; - tv.p_hover = true; - set_value(P_BAR, NXT_BACKGROUND_COLOR, TV_HOVER_BG); - set_value(P_BAR, NXT_FONT_COLOR, TV_HOVER_FG_P); - } - else { - tv.p_selected = NONE_SELECTED; - set_value(I_BAR, NXT_BACKGROUND_COLOR, TV_HOVER_BG); - set_value(I_BAR, NXT_FONT_COLOR, TV_HOVER_FG_I); + else if (curr_page == PAGE_LOGGING) + { + SEND_DASHBOARD_START_LOGGING(q_tx_can, 1); + } + else if (curr_page == PAGE_TVSETTINGS) + { + // So if we hit select on an already selected item, unselect it (switch to hover) + + if (tv_settings.curr_hover == TV_INTENSITY_HOVER) + { + tv_settings.curr_hover = TV_INTENSITY_SELECTED; + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, ORANGE); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + // todo Rot encoder state should let us scroll through value options + // for now just use buttons for move up and move down + } + else if (tv_settings.curr_hover == TV_INTENSITY_SELECTED) + { + // "submit" -> CAN payload will update automatically? decide + // Think about edge case when the user leaves the page? Can they without unselecting -> no. What if fault? + tv_settings.curr_hover = TV_INTENSITY_HOVER; + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + // rot encoder state goes back to page move instead of value move + } + else if (tv_settings.curr_hover == TV_P_HOVER) + { + tv_settings.curr_hover = TV_P_SELECTED; + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, ORANGE); + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (tv_settings.curr_hover == TV_P_SELECTED) + { + tv_settings.curr_hover = TV_P_HOVER; + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (tv_settings.curr_hover == TV_DEADBAND_HOVER) + { + tv_settings.curr_hover = TV_DEADBAND_SELECTED; + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, ORANGE); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (tv_settings.curr_hover == TV_DEADBAND_SELECTED) + { + tv_settings.curr_hover = TV_DEADBAND_HOVER; + set_value(TV_PROPORTION_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_INTENSITY_FLT, NXT_BACKGROUND_COLOR, TV_BG); + set_value(TV_DEAD_TXT, NXT_BACKGROUND_COLOR, TV_HOVER_BG); + set_value(TV_ENABLE_OP, NXT_BACKGROUND_COLOR, TV_BG); + } + else if (tv_settings.curr_hover == TV_ENABLE_HOVER) + { + // Don't change the curr_hover + + // Toggle the option + tv_settings.tv_enable_selected = (tv_settings.tv_enable_selected == 0); + + // Set the option + set_value(TV_ENABLE_OP, NXT_VALUE, tv_settings.tv_enable_selected); + + // Update CAN as necessary + } + else + { + // ? } } else if (curr_page == PAGE_SETTINGS) { @@ -521,18 +828,147 @@ void selectItem() { } SEND_COOLING_DRIVER_REQUEST(q_tx_can, settings.d_pump_selected, settings.d_fan_val, settings.b_fan2_selected, settings.b_pump_selected, settings.b_fan_val); } + else if (curr_page == PAGE_DRIVER) + { + switch(driver_config.curr_hover) + { + case DRIVER_DEFAULT_SELECT: + set_value(DRIVER_DEFAULT_OP, NXT_VALUE, 1); + set_value(DRIVER_TYLER_OP, NXT_VALUE, 0); + set_value(DRIVER_RUHAAN_OP, NXT_VALUE, 0); + set_value(DRIVER_LUKE_OP, NXT_VALUE, 0); + break; + case DRIVER_TYLER_SELECT: + set_value(DRIVER_DEFAULT_OP, NXT_VALUE, 0); + set_value(DRIVER_TYLER_OP, NXT_VALUE, 1); + set_value(DRIVER_RUHAAN_OP, NXT_VALUE, 0); + set_value(DRIVER_LUKE_OP, NXT_VALUE, 0); + break; + case DRIVER_RUHAAN_SELECT: + set_value(DRIVER_DEFAULT_OP, NXT_VALUE, 0); + set_value(DRIVER_TYLER_OP, NXT_VALUE, 0); + set_value(DRIVER_RUHAAN_OP, NXT_VALUE, 1); + set_value(DRIVER_LUKE_OP, NXT_VALUE, 0); + break; + case DRIVER_LUKE_SELECT: + set_value(DRIVER_DEFAULT_OP, NXT_VALUE, 0); + set_value(DRIVER_TYLER_OP, NXT_VALUE, 0); + set_value(DRIVER_RUHAAN_OP, NXT_VALUE, 0); + set_value(DRIVER_LUKE_OP, NXT_VALUE, 1); + break; + } + } } void updateFaultDisplay() { - if (most_recent_latched == 0xFFFF) + if ((curr_page == PAGE_ERROR || (curr_page == PAGE_WARNING) || (curr_page == PAGE_FATAL))) + { + if (++fault_time_displayed > 8) + { + curr_page = prev_page; + prev_page = PAGE_PREFLIGHT; + updatePage(); + } + + } + else + { + fault_time_displayed = 0; + } + + // No new fault to display + if (qIsEmpty(&q_fault_history) && (most_recent_latched == 0xFFFF)) { return; } - display_time = 0; - curr_page = faultArray[most_recent_latched].priority + 5; - errorText = faultArray[most_recent_latched].screen_MSG; + + // Track if we alrady have this fault in the display buffer + bool faultAlreadyInBuffer = false; + bool pageUpdateRequired = false; + bool faultWasInserted = false; + + // Process up to 5 faults each time for now + for (int i = 0; i < 5; i++) + { + faultAlreadyInBuffer = false; + uint16_t next_to_check = 0xFFFF; + faultWasInserted = false; + + if (qReceive(&q_fault_history, &next_to_check)) + { + // Iterate through fault buffer for existance of fault already + for (int j = 0; j < 5; j++) + { + // This should be based off of the queue item not anything else + if (fault_buf[j] == next_to_check) + { + faultAlreadyInBuffer = true; + break; + } + } + + // New fault to add to the display, if room + if (false == faultAlreadyInBuffer) + { + // try all the slots for inserting the fault + for (uint8_t k = 0; k < 5; k++) + { + // If fault is currently not in our fault buffer, replace it if the current fault is cleared, + // or if the new fault has higher priority + if (fault_buf[cur_fault_buf_ndx] != 0xFFFF) + { + if ((checkFault(fault_buf[cur_fault_buf_ndx]) == false ) || + (faultArray[next_to_check].priority > faultArray[fault_buf[cur_fault_buf_ndx]].priority)) + { + fault_buf[cur_fault_buf_ndx] = next_to_check; + faultWasInserted = true; + pageUpdateRequired = true; + break; + } + } + else + { + // Empty slot just insert + fault_buf[cur_fault_buf_ndx] = next_to_check; + faultWasInserted = true; + pageUpdateRequired = true; + break; + } + cur_fault_buf_ndx = (cur_fault_buf_ndx + 1) % 5; + } + + // Put back in the queue if it wasn't processed + if (false == faultWasInserted) + { + qSendToBack(&q_fault_history, &next_to_check); + } + + } + } + else + { + // Break out if issue or the queue is empty + break; + } + + } + + // Set the alert page to show based on most_recent_latched + if ((most_recent_latched != 0xFFFF)) + { + curr_page = faultArray[most_recent_latched].priority + 9; + errorText = faultArray[most_recent_latched].screen_MSG; + pageUpdateRequired = true; + } + + // Update page if required + if (pageUpdateRequired) + { + updatePage(); + } + + // Await next fault most_recent_latched = 0xFFFF; - updatePage(); } void update_data_pages() { @@ -544,8 +980,9 @@ void update_data_pages() { if (can_data.rear_wheel_speeds.stale) { set_text(SPEED, NXT_TEXT, "S"); } + // Vehicle Speed [m/s] = Wheel Speed [RPM] * 16 [in] * PI * 0.0254 / 60 else { - set_text(SPEED, NXT_TEXT, int_to_char((uint16_t)((float)MAX(can_data.rear_wheel_speeds.left_speed_mc, can_data.rear_wheel_speeds.right_speed_mc) / GEAR_RATIO), parsed_value)); + set_text(SPEED, NXT_TEXT, int_to_char((uint16_t)((float)MAX(can_data.rear_wheel_speeds.left_speed_mc, can_data.rear_wheel_speeds.right_speed_mc) * 16.0 * 3.14159265358 * 0.254 / 60.0), parsed_value)); bzero(parsed_value, 3); } if (sendFirsthalf) { @@ -576,15 +1013,17 @@ void update_data_pages() { } else { switch(can_data.main_hb.car_state) { + case CAR_STATE_PRECHARGING: + set_value(CAR_STAT, NXT_BACKGROUND_COLOR, ORANGE); + set_text(CAR_STAT, NXT_TEXT, "PRCHG"); + break; + case CAR_STATE_ENERGIZED: + set_value(CAR_STAT, NXT_BACKGROUND_COLOR, ORANGE); + set_text(CAR_STAT, NXT_TEXT, "ENER"); + break; case CAR_STATE_IDLE: - if (can_data.main_hb.precharge_state == 0) { - set_value(CAR_STAT, NXT_BACKGROUND_COLOR, INFO_GRAY); - set_text(CAR_STAT, NXT_TEXT, "INIT"); - } - else { - set_value(CAR_STAT, NXT_BACKGROUND_COLOR, ORANGE); - set_text(CAR_STAT, NXT_TEXT, "PRCHG"); - } + set_value(CAR_STAT, NXT_BACKGROUND_COLOR, INFO_GRAY); + set_text(CAR_STAT, NXT_TEXT, "INIT"); break; case CAR_STATE_READY2DRIVE: set_value(CAR_STAT, NXT_BACKGROUND_COLOR, RACE_GREEN); @@ -628,41 +1067,6 @@ void update_data_pages() { } } -//1 = deadband, 0 = intensity -void knobDisplay() { - if (preflight < 2) { - preflight++; - return; - } - if (curr_page == PAGE_KNOBS) { - if (knob_old == knob && knob) { - display_time = 1; - tv.deadband_msg = get_deadband(); - set_text(KNB_TXT, NXT_TEXT, tv.deadband_msg); - return; - } - else if (knob_old == knob && !knob) { - display_time = 1; - tv.intensity = hdd.intensity_pos; - char value_to_send[2] = "\0"; - if (hdd.intensity_pos > 9) { - value_to_send[0] = (hdd.intensity_pos / 10) + 48; - value_to_send[1] = (hdd.intensity_pos % 10) + 48; - } - else { - value_to_send[0] = hdd.intensity_pos + 48; - } - set_text(KNB_TXT, NXT_TEXT, value_to_send); - knob_old = knob; - return; - } - } - curr_page = PAGE_KNOBS; - display_time = 0; - knob_old = knob; - updatePage(); -} - void coolant_out_CALLBACK(CanParsedData_t* msg_data_a) { char parsed_value[3] = "\0"; if (curr_page != PAGE_SETTINGS) { @@ -700,66 +1104,6 @@ void coolant_out_CALLBACK(CanParsedData_t* msg_data_a) { } - -void send_p_val() { - if (tv.yaw_p_val == 100) { - set_text(P_TXT, NXT_TEXT, "100"); - } - else if (tv.yaw_p_val == 0) { - set_text(P_TXT, NXT_TEXT, "0"); - } - else { - char msg_to_send[2] = "\0"; - msg_to_send[0] = (tv.yaw_p_val / 10) + 48; - msg_to_send[1] = (tv.yaw_p_val % 10) + 48; - set_text(P_TXT, NXT_TEXT, msg_to_send); - } -} - -void send_i_val() { - if (tv.yaw_i_val == 100) { - set_text(I_TXT, NXT_TEXT, "100"); - } - else if (tv.yaw_i_val == 0) { - set_text(I_TXT, NXT_TEXT, "0"); - } - else { - char msg_to_send[2] = "\0"; - msg_to_send[0] = (tv.yaw_i_val / 10) + 48; - msg_to_send[1] = (tv.yaw_i_val % 10) + 48; - set_text(I_TXT, NXT_TEXT, msg_to_send); - } -} - -char *get_deadband() { - switch (hdd.deadband_pos) { - case 0: - return "0"; - case 1: - return "2.2"; - case 2: - return "4.4"; - case 3: - return "6.6"; - case 4: - return "8.8"; - case 5: - return "11"; - case 6: - return "13.2"; - case 7: - return "15.4"; - case 8: - return "17.6"; - case 9: - return "19.8"; - case 10: - return "22"; - case 11: - return "24"; - } -} - char *int_to_char(int16_t val, char *val_to_send) { char *orig_ptr = val_to_send; if (val < 10) { @@ -782,3 +1126,221 @@ char *int_to_char(int16_t val, char *val_to_send) { return orig_ptr; } } + +void sendTVParameters() +{ + SEND_DASHBOARD_TV_PARAMETERS(q_tx_can, tv_settings.tv_enable_selected, tv_settings.tv_deadband_val, tv_settings.tv_intensity_val, tv_settings.tv_p_val); +} + +void updateFaultPageIndicators() +{ + if (curr_page == PAGE_FAULTS) + { + if (fault_buf[0] == 0xFFFF) + { + set_value(FLT_STAT_1_TXT, NXT_BACKGROUND_COLOR, WHITE); + } + else + { + if (checkFault(fault_buf[fault_buf[0]]) == false) + { + set_value(FLT_STAT_1_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + } + else + { + set_value(FLT_STAT_1_TXT, NXT_BACKGROUND_COLOR, RED); + } + } + if (fault_buf[1] == 0xFFFF) + { + set_value(FLT_STAT_2_TXT, NXT_BACKGROUND_COLOR, WHITE); + } + else + { + if (checkFault(fault_buf[fault_buf[1]]) == false) + { + set_value(FLT_STAT_2_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + } + else + { + set_value(FLT_STAT_2_TXT, NXT_BACKGROUND_COLOR, RED); + } + } + if (fault_buf[2] == 0xFFFF) + { + set_value(FLT_STAT_3_TXT, NXT_BACKGROUND_COLOR, WHITE); + } + else + { + if (checkFault(fault_buf[fault_buf[2]]) == false) + { + set_value(FLT_STAT_3_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + } + else + { + set_value(FLT_STAT_3_TXT, NXT_BACKGROUND_COLOR, RED); + } + } + if (fault_buf[3] == 0xFFFF) + { + set_value(FLT_STAT_4_TXT, NXT_BACKGROUND_COLOR, WHITE); + } + else + { + if (checkFault(fault_buf[fault_buf[3]]) == false) + { + set_value(FLT_STAT_4_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + } + else + { + set_value(FLT_STAT_4_TXT, NXT_BACKGROUND_COLOR, RED); + } + } + if (fault_buf[4] == 0xFFFF) + { + set_value(FLT_STAT_5_TXT, NXT_BACKGROUND_COLOR, WHITE); + } + else + { + if (checkFault(fault_buf[fault_buf[4]]) == false) + { + set_value(FLT_STAT_5_TXT, NXT_BACKGROUND_COLOR, RACE_GREEN); + } + else + { + set_value(FLT_STAT_5_TXT, NXT_BACKGROUND_COLOR, RED); + } + } + } +} + +void updateSDCDashboard() +{ + static uint8_t updateCode = 0U; + if (curr_page == PAGE_SDCINFO) + { + switch (++updateCode) + { + case 1: + // IMD from ABOX + if (can_data.precharge_hb.IMD) + { + set_value(SDC_IMD_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_IMD_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + + if (can_data.precharge_hb.BMS) + { + set_value(SDC_BMS_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_BMS_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + if (false == checkFault(ID_BSPD_LATCHED_FAULT)) + { + set_value(SDC_BSPD_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_BSPD_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + + if (can_data.sdc_status.BOTS) + { + set_value(SDC_BOTS_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_BOTS_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + + if (can_data.sdc_status.inertia) + { + set_value(SDC_INER_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_INER_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + break; + + case 2: + if (can_data.sdc_status.c_estop) + { + set_value(SDC_CSTP_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_CSTP_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + if (can_data.sdc_status.main) + { + set_value(SDC_MAIN_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_MAIN_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + if (can_data.sdc_status.r_estop) + { + set_value(SDC_RSTP_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_RSTP_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + if (can_data.sdc_status.l_estop) + { + set_value(SDC_LSTP_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_LSTP_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + if (can_data.sdc_status.HVD) + { + set_value(SDC_HVD_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_HVD_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + break; + case 3: + if (can_data.sdc_status.hub) + { + set_value(SDC_RHUB_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_RHUB_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + if (can_data.sdc_status.TSMS) + { + set_value(SDC_TSMS_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_TSMS_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + if (can_data.sdc_status.pchg_out) + { + set_value(SDC_PCHG_STAT_TXT, NXT_BACKGROUND_COLOR, GREEN); + } + else + { + set_value(SDC_PCHG_STAT_TXT, NXT_BACKGROUND_COLOR, RED); + } + //todo set first trip from latest change in the sdc + updateCode = 0U; + break; + default: + updateCode = 0; + break; + } + } +} \ No newline at end of file diff --git a/source/dashboard/lcd/lcd.h b/source/dashboard/lcd/lcd.h index c68ad9d9..ae030848 100644 --- a/source/dashboard/lcd/lcd.h +++ b/source/dashboard/lcd/lcd.h @@ -10,26 +10,66 @@ #define RACE_STRING "race" #define SETTINGS_STRING "settings" #define DATA_STRING "data" -#define TV_STRING "tv" #define ERR_STRING "error" #define WARN_STRING "warning" #define FATAL_STRING "critical" -#define DEADBAND_STRING "deadband" -#define INTENSITY_STRING "intensity" +#define FAULT_STRING "faults" +#define TVSETTINGS_STRING "tvsettings" +#define DRIVER_STRING "driver" +#define SDCINFO_STRING "sdcinfo" +#define LOGGING_STRING "logging" #define GEAR_RATIO ((49.0F * 111.0F / 27.0F / 26.0F) + 1U) + +// Driver Configuration Page +#define DRIVER_DEFAULT_OP "r0" +#define DRIVER_TYLER_OP "r1" +#define DRIVER_RUHAAN_OP "r2" +#define DRIVER_LUKE_OP "r3" +#define DRIVER_DEFAULT_TXT "t1" +#define DRIVER_TYLER_TXT "t2" +#define DRIVER_RUHAAN_TXT "t3" +#define DRIVER_LUKE_TXT "t4" + //Error/Knob page values #define TIME_BAR "j0" #define ERR_TXT "t1" -#define KNB_TXT "t3" -//TV Page Values -#define P_BAR "j0" -#define I_BAR "j1" -#define P_TXT "t3" -#define I_TXT "t5" -#define TV_DB_TXT "t7" -#define TV_IN_TXT "t9" +// SDC Info Page Values +#define SDC_IMD_STAT_TXT "t2" +#define SDC_BMS_STAT_TXT "t8" +#define SDC_BSPD_STAT_TXT "t14" +#define SDC_BOTS_STAT_TXT "t20" +#define SDC_INER_STAT_TXT "t26" +#define SDC_CSTP_STAT_TXT "t4" +#define SDC_MAIN_STAT_TXT "t10" +#define SDC_RSTP_STAT_TXT "t16" +#define SDC_LSTP_STAT_TXT "t22" +#define SDC_HVD_STAT_TXT "t6" +#define SDC_RHUB_STAT_TXT "t12" +#define SDC_TSMS_STAT_TXT "t18" +#define SDC_PCHG_STAT_TXT "t24" +#define SDC_FIRST_TRIP_TXT "t28" + + +//Fault Page Values +#define FAULT_1_TXT "t1" +#define FAULT_2_TXT "t2" +#define FAULT_3_TXT "t3" +#define FAULT_4_TXT "t4" +#define FAULT_5_TXT "t5" +#define FLT_STAT_1_TXT "t6" +#define FLT_STAT_2_TXT "t7" +#define FLT_STAT_3_TXT "t8" +#define FLT_STAT_4_TXT "t9" +#define FLT_STAT_5_TXT "t10" +#define FAULT_NONE_STRING "NONE\0" + +//TV Settings Page Values +#define TV_INTENSITY_FLT "x0" +#define TV_PROPORTION_FLT "x1" +#define TV_DEAD_TXT "t6" +#define TV_ENABLE_OP "c0" //Setings page values #define DT_FAN_TXT "t2" @@ -47,10 +87,6 @@ //Colors #define TV_BG 38066 -#define TV_P_FG 64512 -#define TV_I_FG 31 -#define TV_HOVER_FG_P 65106 -#define TV_HOVER_FG_I 33823 #define TV_HOVER_BG 52857 #define SETTINGS_HOVER_BG 28223 #define SETTINGS_BAR_BG 48631 @@ -80,33 +116,25 @@ typedef enum { - PAGE_PREFLIGHT, + + // Pages selectable with the rot encoder + // Should corresspond with the page count in main.h PAGE_RACE, PAGE_SETTINGS, PAGE_DATA, - PAGE_TV, + PAGE_FAULTS, + PAGE_TVSETTINGS, + PAGE_SDCINFO, + PAGE_DRIVER, + PAGE_LOGGING, + + // Pages that can be displayed but not selected with the encoder + PAGE_PREFLIGHT, PAGE_WARNING, PAGE_ERROR, PAGE_FATAL, - PAGE_KNOBS } page_t; -typedef enum { - NONE_SELECTED, - P_SELECTED, - I_SELECTED -} tv_select_t; - -typedef struct { - uint8_t yaw_p_val; - uint8_t yaw_i_val; - bool p_hover; - tv_select_t p_selected; - uint8_t intensity; - uint8_t deadband; - char *deadband_msg; -} tv_options_t; - typedef enum { DT_FAN_HOVER, DT_PUMP_HOVER, @@ -117,6 +145,42 @@ typedef enum { FAN1_SELECT } hover_state_t; +typedef enum { + TV_INTENSITY_HOVER, + TV_P_HOVER, + TV_DEADBAND_HOVER, + TV_ENABLE_HOVER, + TV_INTENSITY_SELECTED, + TV_P_SELECTED, + TV_DEADBAND_SELECTED, + TV_NONE_SELECTED, + +} tv_hover_state_t; + +typedef struct { + bool tv_enable_selected; + tv_hover_state_t curr_hover; + uint8_t tv_deadband_val; + + // intensity and p are 10x the float equivalent + uint16_t tv_intensity_val; + uint16_t tv_p_val; + +} tv_settings_t; + +typedef enum { + DRIVER_DEFAULT_SELECT, + DRIVER_TYLER_SELECT, + DRIVER_RUHAAN_SELECT, + DRIVER_LUKE_SELECT, + +} driver_select_state_t; + +typedef struct { + driver_select_state_t curr_hover; + driver_select_state_t curr_select; +} driver_config_t; + typedef struct { bool d_fan_selected; bool d_pump_selected; @@ -128,19 +192,22 @@ typedef struct { hover_state_t curr_hover; } settings_t; -void initLCD(); -void updatePage(); -void moveLeft(); -void moveRight(); -void moveUp(); -void moveDown(); -void selectItem(); -void updateFaultDisplay(); -void knobDisplay(); -void send_p_val(); -void send_i_val(); -void update_data_pages(); -char *get_deadband(); -char *int_to_char(int16_t val, char *val_to_send); +typedef struct { + volatile int8_t encoder_position; + +} lcd_t; + +void initLCD(); // Initialize LCD data structures and configuration +void updatePage(); // Change the current page of the LCD +void moveUp(); // Upward UI input detected (up button or in some cases encoder) +void moveDown(); // Downward UI input detected (down button or in some cases encoder) +void selectItem(); // Selection UI input detected +void updateFaultDisplay(); // Periodically poll recent faults and update the fault buffer and page as needed +void update_data_pages(); // Periodically poll recent telemetry and update the data page as needd +char *int_to_char(int16_t val, char *val_to_send); // Convert integer value to character for the nextion interface +bool zeroEncoder(volatile int8_t* start_pos); // Zero the encoder position for page selection +void sendTVParameters(); // Periodically send updates to the TV configuration to TV board +void updateFaultPageIndicators(); +void updateSDCDashboard(); #endif diff --git a/source/dashboard/main.c b/source/dashboard/main.c index 4e97e7ec..59fe1bf1 100644 --- a/source/dashboard/main.c +++ b/source/dashboard/main.c @@ -17,8 +17,6 @@ #include "pedals.h" #include "lcd.h" #include "nextion.h" -#include "hdd.h" - GPIOInitConfig_t gpio_config[] = { // Status Indicators @@ -31,18 +29,22 @@ GPIOInitConfig_t gpio_config[] = { GPIO_INIT_INPUT(START_BTN_GPIO_Port, START_BTN_Pin, GPIO_INPUT_PULL_UP), GPIO_INIT_INPUT(BRK_STAT_TAP_GPIO_Port, BRK_STAT_TAP_Pin, GPIO_INPUT_OPEN_DRAIN), GPIO_INIT_INPUT(BRK_FAIL_TAP_GPIO_Port, BRK_FAIL_TAP_Pin, GPIO_INPUT_OPEN_DRAIN), + // CAN GPIO_INIT_CANRX_PD0, GPIO_INIT_CANTX_PD1, + // SPI Peripherals GPIO_INIT_SPI2_SCK_PB13, GPIO_INIT_SPI2_MISO_PB14, GPIO_INIT_SPI2_MOSI_PB15, GPIO_INIT_OUTPUT(EEPROM_nWP_GPIO_Port, EEPROM_nWP_Pin, GPIO_OUTPUT_LOW_SPEED), GPIO_INIT_OUTPUT(EEPROM_NSS_GPIO_Port, EEPROM_NSS_Pin, GPIO_OUTPUT_LOW_SPEED), + // Throttle GPIO_INIT_ANALOG(THTL_1_GPIO_Port, THTL_1_Pin), GPIO_INIT_ANALOG(THTL_2_GPIO_Port, THTL_2_Pin), + // Brake GPIO_INIT_ANALOG(BRK_1_GPIO_Port, BRK_1_Pin), GPIO_INIT_ANALOG(BRK_2_GPIO_Port, BRK_2_Pin), @@ -58,6 +60,7 @@ GPIOInitConfig_t gpio_config[] = { // LCD GPIO_INIT_USART1TX_PA9, GPIO_INIT_USART1RX_PA10, + // Buttons/Switches GPIO_INIT_INPUT(B_OK_GPIO_Port, B_OK_Pin, GPIO_INPUT_OPEN_DRAIN), GPIO_INIT_INPUT(B_DOWN_GPIO_Port, B_DOWN_Pin, GPIO_INPUT_OPEN_DRAIN), @@ -82,63 +85,56 @@ ADCInitConfig_t adc_config = { .resolution = ADC_RES_12_BIT, .data_align = ADC_DATA_ALIGN_RIGHT, .cont_conv_mode = true, - .dma_mode = ADC_DMA_CIRCULAR + .dma_mode = ADC_DMA_CIRCULAR, + .adc_number = 1, }; -// TODO: check prescaler for udpate rate ADCChannelConfig_t adc_channel_config[] = { {.channel=THTL_1_ADC_CHNL, .rank=1, .sampling_time=ADC_CHN_SMP_CYCLES_480}, {.channel=THTL_2_ADC_CHNL, .rank=2, .sampling_time=ADC_CHN_SMP_CYCLES_480}, {.channel=BRK_1_ADC_CHNL, .rank=3, .sampling_time=ADC_CHN_SMP_CYCLES_480}, {.channel=BRK_2_ADC_CHNL, .rank=4, .sampling_time=ADC_CHN_SMP_CYCLES_480}, - {.channel=SHOCK_POT_L_ADC_CH, .rank=6, .sampling_time=ADC_CHN_SMP_CYCLES_480}, - {.channel=SHOCK_POT_R_ADC_CH, .rank=7, .sampling_time=ADC_CHN_SMP_CYCLES_480}, - {.channel=LV_5V_V_SENSE_ADC_CHNL, .rank=8, .sampling_time=ADC_CHN_SMP_CYCLES_480}, - {.channel=LV_3V3_V_SENSE_ADC_CHNL, .rank=9, .sampling_time=ADC_CHN_SMP_CYCLES_480}, + {.channel=SHOCK_POT_L_ADC_CH, .rank=5, .sampling_time=ADC_CHN_SMP_CYCLES_480}, + {.channel=SHOCK_POT_R_ADC_CH, .rank=6, .sampling_time=ADC_CHN_SMP_CYCLES_480}, + {.channel=LV_5V_V_SENSE_ADC_CHNL, .rank=7, .sampling_time=ADC_CHN_SMP_CYCLES_480}, + {.channel=LV_3V3_V_SENSE_ADC_CHNL, .rank=8, .sampling_time=ADC_CHN_SMP_CYCLES_480}, + {.channel=LV_12_V_SENSE_ADC_CHNL, .rank=9, .sampling_time=ADC_CHN_SMP_CYCLES_480}, + {.channel=LV_24_V_SENSE_ADC_CHNL, .rank=10, .sampling_time=ADC_CHN_SMP_CYCLES_480}, + {.channel=LOAD_FL_ADC_CH, .rank=11, .sampling_time=ADC_CHN_SMP_CYCLES_480}, + {.channel=LOAD_FR_ADC_CH, .rank=12, .sampling_time=ADC_CHN_SMP_CYCLES_480}, }; dma_init_t adc_dma_config = ADC1_DMA_CONT_CONFIG((uint32_t) &raw_adc_values, sizeof(raw_adc_values) / sizeof(raw_adc_values.t1), 0b01); -/* USART Confiugration */ -//Need new usart init struct -// dma_init_t usart_tx_dma_config = USART1_TXDMA_CONT_CONFIG(NULL, 1); -// dma_init_t usart_rx_dma_config = USART1_RXDMA_CONT_CONFIG(NULL, 2); -// usart_init_t lcd = { -// .baud_rate = 115200, -// .word_length = WORD_8, -// .stop_bits = SB_ONE, -// .parity = PT_NONE, -// .mode = MODE_TX_RX, -// .hw_flow_ctl = HW_DISABLE, -// .ovsample = OV_16, -// .obsample = OB_DISABLE, -// .adv_feature = { -// .auto_baud = false, -// .ab_mode = AB_START, -// .tx_inv = false, -// .rx_inv = false, -// .data_inv = false, -// .tx_rx_swp = false, -// .overrun = false, -// .dma_on_rx_err = false, -// .msb_first = false, -// }, -// .tx_dma_cfg = &usart_tx_dma_config, -// .rx_dma_cfg = &usart_rx_dma_config -// }; +// USART Configuration for LCD +dma_init_t usart_tx_dma_config = USART1_TXDMA_CONT_CONFIG(NULL, 1); +dma_init_t usart_rx_dma_config = USART1_RXDMA_CONT_CONFIG(NULL, 2); +usart_init_t lcd = { + .baud_rate = 115200, + .word_length = WORD_8, + .stop_bits = SB_ONE, + .parity = PT_NONE, + .hw_flow_ctl = HW_DISABLE, + .ovsample = OV_16, + .obsample = OB_DISABLE, + .periph = USART1, + .wake_addr = false, + .usart_active_num = USART1_ACTIVE_IDX, + .tx_dma_cfg = &usart_tx_dma_config, + .rx_dma_cfg = &usart_rx_dma_config +}; +// Clock Configuration #define TargetCoreClockrateHz 16000000 ClockRateConfig_t clock_config = { - .system_source = SYSTEM_CLOCK_SRC_HSI, - .system_clock_target_hz = TargetCoreClockrateHz, - .ahb_clock_target_hz = (TargetCoreClockrateHz / 1), - .apb1_clock_target_hz = (TargetCoreClockrateHz / (1)), - .apb2_clock_target_hz = (TargetCoreClockrateHz / (1)), + .system_source =SYSTEM_CLOCK_SRC_HSI, + .vco_output_rate_target_hz =160000000, + .system_clock_target_hz =TargetCoreClockrateHz, + .ahb_clock_target_hz =(TargetCoreClockrateHz / 1), + .apb1_clock_target_hz =(TargetCoreClockrateHz / (1)), + .apb2_clock_target_hz =(TargetCoreClockrateHz / (1)), }; -hdd_value_t hdd = { - .deadband_pos = 0, - .intensity_pos = 0, - .deadband_prev = 0, - .intensity_prev = 0 +lcd_t lcd_data = { + .encoder_position = 0, }; /* Locals for Clock Rates */ @@ -147,13 +143,10 @@ extern uint32_t APB2ClockRateHz; extern uint32_t AHBClockRateHz; extern uint32_t PLLClockRateHz; +// LCD Variables extern page_t curr_page; -extern uint8_t tvNotifiValue; -extern bool knob; - -static int32_t ts_ratio; -static uint16_t ts_cal_1, ts_cal_2; - +volatile int8_t prev_rot_state = 0; +static volatile uint8_t dashboard_input; /* Function Prototypes */ void preflightChecks(void); @@ -162,12 +155,12 @@ void heartBeatLED(); void canTxUpdate(); void usartTxUpdate(); extern void HardFault_Handler(); -void pollHDD(); void enableInterrupts(); -void sendMCUTempsVolts(); -void sendVoltageSense(); - +void encoder_ISR(); +void pollDashboardInput(); +void sendBrakeStatus(); +// Communication queues q_handle_t q_tx_can; q_handle_t q_rx_can; q_handle_t q_tx_usart; @@ -184,10 +177,20 @@ int main (void){ { HardFault_Handler(); } - if(!PHAL_initGPIO(gpio_config, sizeof(gpio_config)/sizeof(GPIOInitConfig_t))) + if(false == PHAL_initGPIO(gpio_config, sizeof(gpio_config)/sizeof(GPIOInitConfig_t))) + { + HardFault_Handler(); + } + if(false == PHAL_initADC(ADC1, &adc_config, adc_channel_config, sizeof(adc_channel_config)/sizeof(ADCChannelConfig_t))) + { + HardFault_Handler(); + } + if(false == PHAL_initDMA(&adc_dma_config)) { HardFault_Handler(); } + PHAL_startTxfer(&adc_dma_config); + PHAL_startADC(ADC1); initFaultLibrary(FAULT_NODE_NAME, &q_tx_can, ID_FAULT_SYNC_DASHBOARD); @@ -199,17 +202,21 @@ int main (void){ /* Task Creation */ schedInit(APB1ClockRateHz); - configureAnim(preflightAnimation, preflightChecks, 120, 2500); - - // taskCreate(updatePage, 500); - // taskCreate(updateFaultDisplay, 500); - // taskCreate(heartBeatLED, 500); - // taskCreate(heartBeatTask, 100); - // taskCreate(pollHDD, 250); - // taskCreate(update_data_pages, 200); - // taskCreate(pedalsPeriodic, 15); - // taskCreate(sendMCUTempsVolts, 500); - + configureAnim(preflightAnimation, preflightChecks, 60, 2500); + + taskCreate(updateFaultDisplay, 500); + taskCreate(updateFaultPageIndicators, 500); + taskCreate(heartBeatLED, 500); + taskCreate(pedalsPeriodic, 15); + taskCreate(pollDashboardInput, 25); + taskCreate(heartBeatTask, 100); + taskCreate(update_data_pages, 200); + taskCreate(sendBrakeStatus, 500); + taskCreate(sendTVParameters, 4000); + taskCreate(updateSDCDashboard, 500); + taskCreateBackground(usartTxUpdate); + taskCreateBackground(canTxUpdate); + taskCreateBackground(canRxUpdate); schedStart(); @@ -222,58 +229,44 @@ void preflightChecks(void) { switch (state++) { case 0: - if(!PHAL_initCAN(CAN1, false)) + if(false == PHAL_initCAN(CAN1, false)) { HardFault_Handler(); } NVIC_EnableIRQ(CAN1_RX0_IRQn); - break; + break; case 1: - // Make sure USART is still being initialized properly - // if(!PHAL_initUSART(USART1, &lcd, APB2ClockRateHz)) - // { - // HardFault_Handler(); - // } + if(false == PHAL_initUSART(&lcd, APB2ClockRateHz)) + { + HardFault_Handler(); + } break; case 2: - //Enable MCU Internal Thermistor - // ADC123_COMMON->CCR |= ADC_CCR_TSEN; - // ts_cal_1 = *(TS_CAL1_ADDR); - // ts_cal_2 = *(TS_CAL2_ADDR); - // ts_ratio = (int32_t)(TS_CAL2_VAL - TS_CAL1_VAL) / (ts_cal_2 - ts_cal_1); - break; - case 3: - // if(!PHAL_initI2C(I2C1)) - // { - // HardFault_Handler(); - // } - break; - case 4: - if(!PHAL_initADC(ADC1, &adc_config, adc_channel_config, sizeof(adc_channel_config)/sizeof(ADCChannelConfig_t))) + if(false == PHAL_initADC(ADC1, &adc_config, adc_channel_config, sizeof(adc_channel_config)/sizeof(ADCChannelConfig_t))) { HardFault_Handler(); } - if(!PHAL_initDMA(&adc_dma_config)) + if(false == PHAL_initDMA(&adc_dma_config)) { HardFault_Handler(); } PHAL_startTxfer(&adc_dma_config); PHAL_startADC(ADC1); break; - case 5: + case 3: /* Module Initialization */ initCANParse(&q_rx_can); if (daqInit(&q_tx_can)) HardFault_Handler(); break; + case 4: + enableInterrupts(); + break; case 6: - //Initialize HDD - // No longer have HDD, but we still have the functionality from it - // pollHDD(); - // enableInterrupts(); + // Zero Rotary Encoder + zeroEncoder(&prev_rot_state); break; - case 7: - //Initialize LCD + case 5: initLCD(); break; default: @@ -283,13 +276,35 @@ void preflightChecks(void) { } void preflightAnimation(void) { - static uint32_t time; + // Controls external LEDs since they are more visible when dash is in car + static uint32_t time_ext; PHAL_writeGPIO(BMS_LED_GPIO_Port, BMS_LED_Pin, 1); PHAL_writeGPIO(IMD_LED_GPIO_Port, IMD_LED_Pin, 1); PHAL_writeGPIO(PRCHG_LED_GPIO_Port, PRCHG_LED_Pin, 1); + static uint32_t time; + + PHAL_writeGPIO(HEART_LED_GPIO_Port, HEART_LED_Pin, 0); + PHAL_writeGPIO(ERROR_LED_GPIO_Port, ERROR_LED_Pin, 0); + PHAL_writeGPIO(CONN_LED_GPIO_Port, CONN_LED_Pin, 0); + + switch (time++ % 6) + { + case 0: + case 5: + PHAL_writeGPIO(HEART_LED_GPIO_Port, HEART_LED_Pin, 1); + break; + case 1: + case 4: + PHAL_writeGPIO(CONN_LED_GPIO_Port, CONN_LED_Pin, 1); + break; + case 2: + case 3: + PHAL_writeGPIO(ERROR_LED_GPIO_Port, ERROR_LED_Pin, 1); + break; + } - switch (time++ % 2) + switch (time_ext++ % 4) { case 0: PHAL_writeGPIO(BMS_LED_GPIO_Port, BMS_LED_Pin, 0); @@ -299,120 +314,6 @@ void preflightAnimation(void) { } } -// void sendMCUTempsVolts() { -// int16_t calc_temp = (int16_t) ((((int32_t) raw_adc_values.mcu_therm)*ADC_VREF_INT/ TS_VREF - ts_cal_1) * -// (TS_CAL2_VAL - TS_CAL1_VAL) / (ts_cal_2 - ts_cal_1) + TS_CAL1_VAL); -// float lv_5v_sense = ((VREF / 0xFFFU) * raw_adc_values.lv_5v_sense) / LV_5V_SCALE; -// float lv_3v3_sense = (VREF / 0xFFFU) * raw_adc_values.lv_3v3_sense; -// // SEND_DASHBOARD_VOLTS_TEMP(q_tx_can, calc_temp, (uint16_t)(lv_5v_sense * 100), (uint16_t)(lv_3v3_sense * 100)); -// SEND_DASHBOARD_VOLTS_TEMP(q_tx_can, raw_adc_values.mcu_therm, raw_adc_values.lv_5v_sense, raw_adc_values.lv_3v3_sense); -// } - -// void pollHDD() { -// hdd.deadband_prev = hdd.deadband_pos; -// hdd.intensity_prev = hdd.intensity_pos; -// for (uint8_t i = 0; i < 24; i++) { -// //BMUX0 == LSB, BMUX4 == LSB -// PHAL_writeGPIO(B_MUX_0_GPIO_Port, B_MUX_0_Pin, (bool)(i & 0x01)); -// PHAL_writeGPIO(B_MUX_1_GPIO_Port, B_MUX_1_Pin, (bool)(i & 0x02)); -// PHAL_writeGPIO(B_MUX_2_GPIO_Port, B_MUX_2_Pin, (bool)(i & 0x04)); -// PHAL_writeGPIO(B_MUX_3_GPIO_Port, B_MUX_3_Pin, (bool)(i & 0x08)); -// PHAL_writeGPIO(B_MUX_4_GPIO_Port, B_MUX_4_Pin, (bool)(i & 0x10)); -// for (uint8_t j = 0; j < 10; j++) { -// __asm__("nop"); -// } -// if (i <= 11) { -// if (PHAL_readGPIO(B_MUX_DATA_GPIO_Port, B_MUX_DATA_Pin)) { -// hdd.deadband_pos = i; -// if (hdd.deadband_pos != hdd.deadband_prev) { -// knob = 1; -// knobDisplay(); -// } -// } -// } -// else { -// if (PHAL_readGPIO(B_MUX_DATA_GPIO_Port, B_MUX_DATA_Pin)) { -// hdd.intensity_pos = i - 12; -// if (hdd.intensity_pos != hdd.intensity_prev) { -// knob = 0; -// knobDisplay(); -// } -// } -// } -// } -// } - - -// Old IRQHandlers from PER23 -// static volatile uint32_t last_click_time; -// void EXTI0_IRQHandler() { -// if (EXTI->PR1 & EXTI_PR1_PIF0) { -// PHAL_toggleGPIO(PRCHG_LED_GPIO_Port, PRCHG_LED_Pin); -// SEND_START_BUTTON(q_tx_can, 1); -// EXTI->PR1 |= EXTI_PR1_PIF0; -// } -// } - -// void EXTI9_5_IRQHandler() { -// if (EXTI->PR1 & EXTI_PR1_PIF8) { -// if (sched.os_ticks - last_click_time < 200) { -// last_click_time = sched.os_ticks; -// EXTI->PR1 |= EXTI_PR1_PIF8; -// } -// else { -// last_click_time = sched.os_ticks; -// moveLeft(); -// EXTI->PR1 |= EXTI_PR1_PIF8; -// } -// } -// } - -// void EXTI15_10_IRQHandler() { -// if (EXTI->PR1 & EXTI_PR1_PIF12) { -// if (sched.os_ticks - last_click_time < 300) { -// last_click_time = sched.os_ticks; -// EXTI->PR1 |= EXTI_PR1_PIF12; -// } -// else { -// last_click_time = sched.os_ticks; -// selectItem(); -// EXTI->PR1 |= EXTI_PR1_PIF12; -// } -// } -// else if (EXTI->PR1 & EXTI_PR1_PIF13) { -// if (sched.os_ticks - last_click_time < 250) { -// last_click_time = sched.os_ticks; -// EXTI->PR1 |= EXTI_PR1_PIF13; -// } -// else { -// last_click_time = sched.os_ticks; -// moveDown(); -// EXTI->PR1 |= EXTI_PR1_PIF13; -// } -// } -// else if (EXTI->PR1 & EXTI_PR1_PIF14) { -// if (sched.os_ticks - last_click_time < 250) { -// last_click_time = sched.os_ticks; -// EXTI->PR1 |= EXTI_PR1_PIF14; -// } -// else { -// last_click_time = sched.os_ticks; -// moveUp(); -// EXTI->PR1 |= EXTI_PR1_PIF14; -// } -// } -// else if (EXTI->PR1 & EXTI_PR1_PIF15) { -// if (sched.os_ticks - last_click_time < 250) { -// last_click_time = sched.os_ticks; -// EXTI->PR1 |= EXTI_PR1_PIF15; -// } -// else { -// last_click_time = sched.os_ticks; -// moveRight(); -// EXTI->PR1 |= EXTI_PR1_PIF15; -// } -// } -// } void heartBeatLED() { PHAL_toggleGPIO(HEART_LED_GPIO_Port, HEART_LED_Pin); @@ -435,62 +336,155 @@ void heartBeatLED() } } -// New MCU so these interrupts aren't the same -// void enableInterrupts() { -// RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; - -// //Unmask + Enable interrupt for start button -// SYSCFG->EXTICR[0] |= SYSCFG_EXTICR1_EXTI0_PE; -// EXTI->IMR1 |= EXTI_IMR1_IM0; -// EXTI->RTSR1 &= ~EXTI_RTSR1_RT0; -// EXTI->FTSR1 |= EXTI_FTSR1_FT0; -// NVIC_EnableIRQ(EXTI0_IRQn); - -// //Left button -// SYSCFG->EXTICR[2] |= SYSCFG_EXTICR3_EXTI8_PD; -// EXTI->IMR1 |= EXTI_IMR1_IM8; -// EXTI->FTSR1 |= EXTI_FTSR1_FT8; -// NVIC_EnableIRQ(EXTI9_5_IRQn); - -// //Ok button -// SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI12_PB; -// EXTI->IMR1 |= EXTI_IMR1_IM12; -// EXTI->FTSR1 |= EXTI_FTSR1_FT12; -// EXTI->RTSR1 &= ~EXTI_RTSR1_RT12; -// NVIC_EnableIRQ(EXTI15_10_IRQn); - -// //Down Button -// SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI13_PB; -// EXTI->IMR1 |= EXTI_IMR1_IM13; -// EXTI->FTSR1 |= EXTI_FTSR1_FT13; -// NVIC_EnableIRQ(EXTI15_10_IRQn); - -// //Up Button -// SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI14_PB; -// EXTI->IMR1 |= EXTI_IMR1_IM14; -// EXTI->FTSR1 |= EXTI_FTSR1_FT14; -// NVIC_EnableIRQ(EXTI15_10_IRQn); - -// //Right Button -// SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI15_PB; -// EXTI->IMR1 |= EXTI_IMR1_IM15; -// EXTI->FTSR1 |= EXTI_FTSR1_FT15; -// NVIC_EnableIRQ(EXTI15_10_IRQn); -// } - - -// Update to new USART functions +static volatile uint32_t last_click_time; + +void EXTI9_5_IRQHandler(void) { + // EXTI9 triggered the interrupt (ENC_B_FLT) + if (EXTI->PR & EXTI_PR_PR9) { + encoder_ISR(); + dashboard_input |= (1 << DASH_INPUT_ROT_ENC); + EXTI->PR |= EXTI_PR_PR9; // Clear the interrupt pending bit for EXTI9 + + } +} + +void EXTI15_10_IRQHandler() { + // EXTI10 triggered the interrupt (ENC_A_FLT) + if (EXTI->PR & EXTI_PR_PR10) { + encoder_ISR(); + dashboard_input |= (1 << DASH_INPUT_ROT_ENC); + EXTI->PR |= EXTI_PR_PR10; // Clear the interrupt pending bit for EXTI14 + } + + // EXTI14 triggered the interrupt (B1_FLT) + // This is the TOP button on the dashboard + if (EXTI->PR & EXTI_PR_PR14) { + if (sched.os_ticks - last_click_time < 200) { + last_click_time = sched.os_ticks; + EXTI->PR |= EXTI_PR_PR14; // Clear the interrupt pending bit for EXTI14 + } + else { + last_click_time = sched.os_ticks; + dashboard_input |= (1 << DASH_INPUT_UP_BUTTON); + EXTI->PR |= EXTI_PR_PR14; // Clear the interrupt pending bit for EXTI14 + } + } + + // EXTI13 triggered the interrupt (B2_FLT) + // This is the MIDDLE button on the dashbaord + if (EXTI->PR & EXTI_PR_PR13) + { + if (sched.os_ticks - last_click_time < 200) { + last_click_time = sched.os_ticks; + EXTI->PR |= EXTI_PR_PR13; // Clear the interrupt pending bit for EXTI13 + } + else + { + last_click_time = sched.os_ticks; + dashboard_input |= (1 << DASH_INPUT_DOWN_BUTTON); + EXTI->PR |= EXTI_PR_PR13; // Clear the interrupt pending bit for EXTI13 + } + } + + // EXTI12 triggered the interrupt (B3_FLT) + // This is the BOTTOM button on the dashboard + if (EXTI->PR & EXTI_PR_PR12) + { + if (sched.os_ticks - last_click_time < 300) { + last_click_time = sched.os_ticks; + EXTI->PR |= EXTI_PR_PR12; // Clear the interrupt pending bit for EXTI12 + } + else + { + last_click_time = sched.os_ticks; + dashboard_input |= (1 << DASH_INPUT_SELECT_BUTTON); + EXTI->PR |= EXTI_PR_PR12; // Clear the interrupt pending bit for EXTI12 + } + } + + // EXTI11 triggered the interrupt (START_FLT) + if (EXTI->PR & EXTI_PR_PR11) { + PHAL_toggleGPIO(ERROR_LED_GPIO_Port, ERROR_LED_Pin); // Toggle LED for testing + dashboard_input |= (1 << DASH_INPUT_START_BUTTON); + EXTI->PR |= EXTI_PR_PR11; // Clear the interrupt pending bit for EXTI11 + } +} + +// [prev_state][current_state] = direction (1 = CW, -1 = CCW, 0 = no movement) +const int8_t encoder_transition_table[ENC_NUM_STATES][ENC_NUM_STATES] = { + { 0, -1, 1, 0}, + { 1, 0, 0, -1}, + {-1, 0, 0, 1}, + { 0, 1, -1, 0} +}; + +void encoder_ISR() { + uint8_t raw_enc_a = PHAL_readGPIO(ENC_A_GPIO_Port, ENC_A_Pin); + uint8_t raw_enc_b = PHAL_readGPIO(ENC_B_GPIO_Port, ENC_B_Pin); + uint8_t current_state = (raw_enc_b | (raw_enc_a << 1)); + + // Get direction from the state transition table + int8_t direction = encoder_transition_table[prev_rot_state][current_state]; + + if (direction != 0) { + lcd_data.encoder_position += direction; + + if (lcd_data.encoder_position >= LCD_NUM_PAGES) { + lcd_data.encoder_position -= LCD_NUM_PAGES; + } else if (lcd_data.encoder_position < 0) { + lcd_data.encoder_position += LCD_NUM_PAGES; + } + } + + prev_rot_state = current_state; +} + +void enableInterrupts() +{ + // Enable the SYSCFG clock for interrupts + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; + + // START_FLT is on PD11 (EXTI11) + SYSCFG->EXTICR[2] |= SYSCFG_EXTICR3_EXTI11_PD; // Map PD11 to EXTI11 + + EXTI->IMR |= EXTI_IMR_MR11; // Unmask EXTI11 + EXTI->RTSR &= ~EXTI_RTSR_TR11; // Disable the rising edge trigger for START_FLT + EXTI->FTSR |= EXTI_FTSR_TR11; // Enable the falling edge trigger for START_FLT + + // ENC_B_FLT is on PD9 (EXTI9) + // ENC_A_FLT is on PD10 (EXTI10) + SYSCFG->EXTICR[2] |= SYSCFG_EXTICR3_EXTI9_PD; // Map PD9 to EXTI9 + SYSCFG->EXTICR[2] |= SYSCFG_EXTICR3_EXTI10_PD; // Map PD10 to EXTI10 + + EXTI->IMR |= (EXTI_IMR_MR9 | EXTI_IMR_MR10); // Unmask EXTI9 and EXTI10 + EXTI->RTSR |= (EXTI_RTSR_TR9 | EXTI_RTSR_TR10); // Enable the rising edge trigger for both ENC_B_FLT and ENC_A_FLT + EXTI->FTSR |= (EXTI_FTSR_TR9 | EXTI_FTSR_TR10); // Enable the falling edge trigger for both ENC_B_FLT and ENC_A_FLT + + // B3_FLT is on PD12 (EXTI 12) + // B2_FLT is on PD13 (EXTI 13) + // B1_FLT is on PD14 (EXTI 14) + SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI12_PD; // Map PD12 to EXTI 12 + SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI13_PD; // Map PD13 to EXTI 13 + SYSCFG->EXTICR[3] |= SYSCFG_EXTICR4_EXTI14_PD; // Map PD14 to EXTI 14 + + EXTI->IMR |= (EXTI_IMR_MR12 | EXTI_IMR_MR13 | EXTI_IMR_MR14); // Unmask EXTI12, EXTI13, and EXTI 14 + EXTI->RTSR &= ~(EXTI_RTSR_TR12 | EXTI_RTSR_TR13 | EXTI_RTSR_TR14); // Disable the rising edge trigger for B3_FLT, B2_FLT, B1_FLT + EXTI->FTSR |= (EXTI_FTSR_TR12 | EXTI_FTSR_TR13 | EXTI_FTSR_TR14); // Enable the falling edge trigger for B3_FLT, B2_FLT, B1_FLT + + NVIC_EnableIRQ(EXTI9_5_IRQn); // Enable EXTI9_5 IRQ for ENC_B_FLT + NVIC_EnableIRQ(EXTI15_10_IRQn); // Enable EXTI15_10 IRQ: START_FLT, ENC_A_FLT, B3_FLT, B2_FLT, B1_FLT +} + +// LCD USART Communication uint8_t cmd[NXT_STR_SIZE] = {'\0'}; void usartTxUpdate() { - // if (PHAL_usartTxDmaComplete(&lcd) && - // qReceive(&q_tx_usart, cmd) == SUCCESS_G) - // { - // PHAL_usartTxDma(USART1, &lcd, (uint16_t *) cmd, strlen(cmd)); - // } + if((false == PHAL_usartTxBusy(&lcd)) && (SUCCESS_G == qReceive(&q_tx_usart, cmd))) + { + PHAL_usartTxDma(&lcd, (uint16_t *) cmd, strlen(cmd)); + } } - void canTxUpdate() { CanMsgTypeDef_t tx_msg; @@ -535,7 +529,7 @@ void CAN1_RX0_IRQHandler() rx.Data[7] = (uint8_t) (CAN1->sFIFOMailBox[0].RDHR >> 24) & 0xFF; CAN1->RF0R |= (CAN_RF0R_RFOM0); - qSendToBack(&q_rx_can, &rx); // Add to queue (qSendToBack is interrupt safe) + qSendToBack(&q_rx_can, &rx); } } @@ -545,6 +539,87 @@ void dashboard_bl_cmd_CALLBACK(CanParsedData_t *msg_data_a) Bootloader_ResetForFirmwareDownload(); } + +static uint8_t upButtonBuffer; +static uint8_t downButtonBuffer; + +// Poll for Dashboard User Input +void pollDashboardInput() +{ + // Check for Encoder Input + upButtonBuffer <<= 1; + if (PHAL_readGPIO(GPIOD, 14) == 0) + { + upButtonBuffer |= 1; + } + upButtonBuffer &= 0b00011111; + if (upButtonBuffer == 0b00000001) + { + moveUp(); + } + + downButtonBuffer <<= 1; + if (PHAL_readGPIO(GPIOD, 13) == 0) + { + downButtonBuffer |= 1; + } + downButtonBuffer &= 0b00011111; + if (downButtonBuffer == 0b00000001) + { + moveDown(); + } + + if (dashboard_input & (1U << DASH_INPUT_ROT_ENC)) + { + updatePage(); + dashboard_input &= ~(1U << DASH_INPUT_ROT_ENC); + } + + // Check for Start Button Pressed + if (dashboard_input & (1U << DASH_INPUT_START_BUTTON)) + { + SEND_START_BUTTON(q_tx_can, 1); // Report start button pressed + dashboard_input &= ~(1U << DASH_INPUT_START_BUTTON); + } + + // Check Up/Down Pressed + // if (dashboard_input & (1U << DASH_INPUT_UP_BUTTON) && + // (dashboard_input & (1U << DASH_INPUT_DOWN_BUTTON))) + // { + // // Default to Up if Both Pressed in x ms + // moveUp(); + // dashboard_input &= ~(1U << DASH_INPUT_UP_BUTTON); + // dashboard_input &= ~(1U << DASH_INPUT_DOWN_BUTTON); + // } + // else if (dashboard_input & (1U << DASH_INPUT_UP_BUTTON)) + // { + // moveUp(); + // dashboard_input &= ~(1U << DASH_INPUT_UP_BUTTON); + // } + // else if (dashboard_input & (1U << DASH_INPUT_DOWN_BUTTON)) + // { + // moveDown(); + // dashboard_input &= ~(1U << DASH_INPUT_DOWN_BUTTON); + // } + // else + // { + // // nothing + // } + + // Check Select Item Pressed + if (dashboard_input & (1U << DASH_INPUT_SELECT_BUTTON)) + { + selectItem(); + dashboard_input &= ~(1U << DASH_INPUT_SELECT_BUTTON); + } +} + +void sendBrakeStatus() +{ + uint8_t isBrakeFailure = PHAL_readGPIO(BRK_FAIL_TAP_GPIO_Port, BRK_FAIL_TAP_Pin); + SEND_DASHBOARD_BRAKE_STATUS(q_tx_can, isBrakeFailure); +} + void HardFault_Handler() { schedPause(); diff --git a/source/dashboard/main.h b/source/dashboard/main.h index ef2879ef..0761336c 100644 --- a/source/dashboard/main.h +++ b/source/dashboard/main.h @@ -16,7 +16,7 @@ #define FAULT_NODE_NAME NODE_DASHBOARD -//STM32L496VGT6 +//STM32F407 typedef struct __attribute__((packed)) { @@ -31,10 +31,26 @@ typedef struct __attribute__((packed)) uint16_t shock_right; uint16_t lv_5v_sense; uint16_t lv_3v3_sense; + uint16_t lv_12v_sense; + uint16_t lv_24_v_sense; + uint16_t load_l; + uint16_t load_r; } raw_adc_values_t; volatile extern raw_adc_values_t raw_adc_values; +typedef enum +{ + DASH_INPUT_NONE, + DASH_INPUT_ROT_ENC, + DASH_INPUT_UP_BUTTON, + DASH_INPUT_DOWN_BUTTON, + DASH_INPUT_SELECT_BUTTON, + DASH_INPUT_START_BUTTON, + DASH_INPUT_COUNT, + DASH_INPUT_INVALID, +} dashboard_input_t; + // Status LED Indicators #define CONN_LED_GPIO_Port (GPIOE) #define CONN_LED_Pin (8) @@ -67,6 +83,7 @@ volatile extern raw_adc_values_t raw_adc_values; #define ENC_A_Pin (10) #define ENC_B_GPIO_Port (GPIOD) #define ENC_B_Pin (9) +#define ENC_NUM_STATES (4) // CAN #define VCAN_RX_GPIO_Port (GPIOD) @@ -134,6 +151,7 @@ volatile extern raw_adc_values_t raw_adc_values; #define LCD_UART_TX_Pin (9) #define LCD_UART_RX_GPIO_Port (GPIOA) #define LCD_UART_RX_Pin (10) +#define LCD_NUM_PAGES (8) // Number encoder selectable pages // LV Status #define LV_5V_V_SENSE_GPIO_Port (GPIOC) @@ -142,15 +160,14 @@ volatile extern raw_adc_values_t raw_adc_values; #define LV_3V3_V_SENSE_GPIO_Port (GPIOC) #define LV_3V3_V_SENSE_Pin (3) #define LV_3V3_V_SENSE_ADC_CHNL (13) -#define LV_12_V_SENSE_GPIO_Port (GPIOC) -#define LV_12_V_SENSE_Pin (4) -#define LV_12_V_SENSE_ADC_CHNL (14) -#define LV_24_V_SENSE_GPIO_Port (GPIOC) -#define LV_24_V_SENSE_Pin (5) -#define LV_24_V_SENSE_ADC_CHNL (15) -#define LV_24_V_FAULT_GPIO_Port (GPIOC) -#define LV_24_V_FAULT_Pin (8) +#define LV_12_V_SENSE_GPIO_Port (GPIOC) +#define LV_12_V_SENSE_Pin (4) +#define LV_12_V_SENSE_ADC_CHNL (14) +#define LV_24_V_SENSE_GPIO_Port (GPIOC) +#define LV_24_V_SENSE_Pin (5) +#define LV_24_V_SENSE_ADC_CHNL (15) +#define LV_24_V_FAULT_GPIO_Port (GPIOC) +#define LV_24_V_FAULT_Pin (8) #define LV_5V_SCALE (0.413F) - #endif diff --git a/source/dashboard/nextion/nextion.h b/source/dashboard/nextion/nextion.h index fb6bbf9c..c7022428 100644 --- a/source/dashboard/nextion/nextion.h +++ b/source/dashboard/nextion/nextion.h @@ -11,7 +11,7 @@ #include #include "common/queue/queue.h" -#define NXT_STR_SIZE 85 +#define NXT_STR_SIZE 100 // Important: Used for usart queue size, issues arise if less than len of longest fault msg #define SET_VALUE_EXTRA 13 #define SET_BCO_EXTRA 13 #define SET_TEXT_EXTRA 10 @@ -26,8 +26,6 @@ #define WHITE 65535 #define BLACK 0 - - #define FLAG_ENABLED_PIC 1 #define FLAG_DISABLED_PIC 1 @@ -37,14 +35,6 @@ #define NXT_TEXT ".txt=" #define NXT_PICTURE ".pic=" -typedef struct -{ - char* name; - uint8_t dirs[4]; // up right down left (CW) - uint8_t norm_id; - uint8_t high_id; -} button_t; - void set_flag(char* obj_name, uint8_t enable); void set_float(char* obj_name, char* param, float num, uint8_t precision); void set_value(char* obj_name, char* param, uint16_t val); diff --git a/source/dashboard/pedals/pedals.c b/source/dashboard/pedals/pedals.c index fec45f51..5e3722c8 100644 --- a/source/dashboard/pedals/pedals.c +++ b/source/dashboard/pedals/pedals.c @@ -6,10 +6,10 @@ pedals_t pedals = {0}; uint16_t thtl_limit = 4096; -pedal_calibration_t pedal_calibration = {.t1max=2015,.t1min=785, // WARNING: DAQ VARIABLE - .t2max=1920,.t2min=550, // IF EEPROM ENABLED, - .b1max=1200,.b1min=450, // VALUE WILL CHANGE - .b2max=1050,.b2min=425, // 1400, 400 +pedal_calibration_t pedal_calibration = {.t1max=2022,.t1min=652, // WARNING: DAQ VARIABLE + .t2max=1986,.t2min=590, // IF EEPROM ENABLED, + .b1max=1490,.b1min=420, // VALUE WILL CHANGE + .b2max=1240,.b2min=420, // 1400, 400 .b3max=124,.b3min=0}; // 910, 812 3312 3436 uint16_t b3_buff[8] = {0}; @@ -38,30 +38,30 @@ void pedalsPeriodic(void) uint16_t t2 = raw_adc_values.t2; uint16_t b1 = raw_adc_values.b1; uint16_t b2 = raw_adc_values.b2; - uint16_t b3_raw = /*raw_adc_values.b3*/0; //no longer use b3 + //uint16_t b3_raw = /*raw_adc_values.b3*/0; //no longer use b3 - b3_buff[b3_idx++] = b3_raw; - b3_idx %= 8; - uint32_t b3_sum = 0; - for (uint8_t i = 0; i < 8; i++) b3_sum += b3_buff[i]; - uint16_t b3 = MAX_PEDAL_MEAS - (b3_sum / 8); + // b3_buff[b3_idx++] = b3_raw; + // b3_idx %= 8; + // uint32_t b3_sum = 0; + // for (uint8_t i = 0; i < 8; i++) b3_sum += b3_buff[i]; + // uint16_t b3 = MAX_PEDAL_MEAS - (b3_sum / 8); //3.3R2/(R2 - R1) - // Calibrate minimum brake pot value after 2 seconds - if (!b3_cal_complete) - { - if (b3_start_cal_time == 0) b3_start_cal_time = sched.os_ticks; - else if (sched.os_ticks - b3_start_cal_time > 2000) - { - b3_cal_complete = 1; - b3_offset = b3 + 10; - } - return; - } - // subtract offset, prevent wrap around (uint) - uint16_t diff = b3 - b3_offset; - b3 = diff > b3 ? 0 : diff; + // // Calibrate minimum brake pot value after 2 seconds + // if (!b3_cal_complete) + // { + // if (b3_start_cal_time == 0) b3_start_cal_time = sched.os_ticks; + // else if (sched.os_ticks - b3_start_cal_time > 2000) + // { + // b3_cal_complete = 1; + // b3_offset = b3 + 10; + // } + // return; + // } + // // subtract offset, prevent wrap around (uint) + // uint16_t diff = b3 - b3_offset; + // b3 = diff > b3 ? 0 : diff; // bool apps_wiring_fail = false; @@ -91,12 +91,12 @@ void pedalsPeriodic(void) // setFault(ID_BSE_WIRING_B1_FAULT, b1); // setFault(ID_BSE_WIRING_B2_FAULT, b2); setFault(ID_BSE_FAULT, PHAL_readGPIO(BRK_FAIL_TAP_GPIO_Port, BRK_FAIL_TAP_Pin)); - if (PHAL_readGPIO(BRK_STAT_TAP_GPIO_Port, BRK_STAT_TAP_Pin)) { - setFault(ID_BSPD_FAULT, can_data.orion_currents_volts.pack_current); - } - else { - setFault(ID_BSPD_FAULT, 0); - } + // if (PHAL_readGPIO(BRK_STAT_TAP_GPIO_Port, BRK_STAT_TAP_Pin)) { + // setFault(ID_BSPD_FAULT, can_data.orion_currents_volts.pack_current); + // } + // else { + // setFault(ID_BSPD_FAULT, 0); + // } float t1_volts = (VREF / 0xFFFU) * t1; float t2_volts = (VREF / 0XFFFU) * t2; @@ -104,41 +104,41 @@ void pedalsPeriodic(void) t1 = (t1_volts * RESISTOR_T1) / (VREF - t1_volts); t2 = (t2_volts * RESISTOR_T2) / (VREF - t2_volts); - t1_buff[t1_idx++] = t1; - t2_buff[t2_idx++] = t2; - b1_buff[b1_idx++] = b1; - b2_buff[b2_idx++] = b2; - - t1_idx = t1_idx % 10; - t2_idx = t2_idx % 10; - b1_idx %= 10; - b2_idx %= 10; - - uint32_t t1_avg = 0; - uint32_t t2_avg = 0; - uint32_t b1_avg = 0; - uint32_t b2_avg = 0; - - for (uint8_t i = 0; i < 10; i++) { - t1_avg += t1_buff[i]; - t2_avg += t2_buff[i]; - b1_avg += b1_buff[i]; - b2_avg += b2_buff[i]; - } - - t1 = (uint16_t) (t1_avg / 10); - t2 = (uint16_t) (t2_avg / 10); - b1 = (uint16_t) (b1_avg / 10); - b2 = (uint16_t) (b2_avg / 10); + // t1_buff[t1_idx++] = t1; + // t2_buff[t2_idx++] = t2; + // b1_buff[b1_idx++] = b1; + // b2_buff[b2_idx++] = b2; + + // t1_idx = t1_idx % 10; + // t2_idx = t2_idx % 10; + // b1_idx %= 10; + // b2_idx %= 10; + + // uint32_t t1_avg = 0; + // uint32_t t2_avg = 0; + // uint32_t b1_avg = 0; + // uint32_t b2_avg = 0; + + // for (uint8_t i = 0; i < 10; i++) { + // t1_avg += t1_buff[i]; + // t2_avg += t2_buff[i]; + // b1_avg += b1_buff[i]; + // b2_avg += b2_buff[i]; + // } + // t1 = (uint16_t) (t1_avg / 10); + // t2 = (uint16_t) (t2_avg / 10); + // b1 = (uint16_t) (b1_avg / 10); + // b2 = (uint16_t) (b2_avg / 10); + // Scale values based on min and max t1 = CLAMP(t1, pedal_calibration.t1min, pedal_calibration.t1max); t2 = CLAMP(t2, pedal_calibration.t2min, pedal_calibration.t2max); b1 = CLAMP(b1, pedal_calibration.b1min, pedal_calibration.b1max); b2 = CLAMP(b2, pedal_calibration.b2min, pedal_calibration.b2max); - b3 = CLAMP(b3, pedal_calibration.b3min, pedal_calibration.b3max); + // b3 = CLAMP(b3, pedal_calibration.b3min, pedal_calibration.b3max); t1 = (uint16_t) ((((uint32_t) (t1 - pedal_calibration.t1min)) * MAX_PEDAL_MEAS) / (pedal_calibration.t1max - pedal_calibration.t1min)); t2 = (uint16_t) ((((uint32_t) (t2 - pedal_calibration.t2min)) * MAX_PEDAL_MEAS) / @@ -147,8 +147,8 @@ void pedalsPeriodic(void) (pedal_calibration.b1max - pedal_calibration.b1min)); b2 = (uint16_t) ((((uint32_t) (b2 - pedal_calibration.b2min)) * MAX_PEDAL_MEAS) / (pedal_calibration.b2max - pedal_calibration.b2min)); - b3 = (uint16_t) ((((uint32_t) (b3 - pedal_calibration.b3min)) * MAX_PEDAL_MEAS) / - (pedal_calibration.b3max - pedal_calibration.b3min)); + // b3 = (uint16_t) ((((uint32_t) (b3 - pedal_calibration.b3min)) * MAX_PEDAL_MEAS) / + // (pedal_calibration.b3max - pedal_calibration.b3min)); // Invert // t1 = MAX_PEDAL_MEAS - t1; // t2 = MAX_PEDAL_MEAS - t2; @@ -158,7 +158,7 @@ void pedalsPeriodic(void) // t2 &= 0xFFFC; // b1 &= 0xFFFC; // b2 &= 0xFFFC; - b3 &= 0xFFFC; + //b3 &= 0xFFFC; // APPS implaus check: wiring fail or 10% APPS deviation T.4.2.4 (after scaling) @@ -174,7 +174,7 @@ void pedalsPeriodic(void) // pedals.apps_implaus_detected = false; // pedals.apps_faulted = false; // } - setFault(ID_IMPLAUS_DETECTED_FAULT, ((t2>t1)?(t2-t1):(t1-t2))); + // setFault(ID_IMPLAUS_DETECTED_FAULT, ((t2>t1)?(t2-t1):(t1-t2))); //The following are commented as this is now handled by the fault library // If APPS implaus occurs > 100ms, set motor power to 0 T.4.2.5 @@ -206,20 +206,20 @@ void pedalsPeriodic(void) // { // pedals.apps_brake_faulted = false; // } + + // Both set at the same time if (b2 >= APPS_BRAKE_THRESHOLD && t2 >= APPS_THROTTLE_FAULT_THRESHOLD) { - // setFault(ID_APPS_BRAKE_FAULT, true); + // set warning fault and treq could be 0 + t2 = 0; + // Later - setup } else if (t2 <= APPS_THROTTLE_CLEARFAULT_THRESHOLD) { // setFault(ID_APPS_BRAKE_FAULT, false); } - - - - //Fault States detected by Main Module, which will exit ready2drive // if (pedals.apps_faulted || pedals.bse_faulted || pedals.apps_brake_faulted) // { @@ -231,7 +231,8 @@ void pedalsPeriodic(void) SEND_RAW_THROTTLE_BRAKE(q_tx_can, raw_adc_values.t1, raw_adc_values.t2, raw_adc_values.b1, raw_adc_values.b2, /*raw_adc_values.b3*/0); //no longer use b3 - SEND_FILT_THROTTLE_BRAKE(q_tx_can, t2, b2); + //SEND_FILT_THROTTLE_BRAKE(q_tx_can, t2, b2); + SEND_FILT_THROTTLE_BRAKE(q_tx_can, t1, b1); }