diff --git a/fail_safe_firmware/FS_MCU_Arduino_Nano/FS_MCU_Arduino_Nano.h b/fail_safe_firmware/FS_MCU_Arduino_Nano/FS_MCU_Arduino_Nano.h index 61c565b..e032965 100644 --- a/fail_safe_firmware/FS_MCU_Arduino_Nano/FS_MCU_Arduino_Nano.h +++ b/fail_safe_firmware/FS_MCU_Arduino_Nano/FS_MCU_Arduino_Nano.h @@ -1,39 +1,43 @@ //DC input pins - const byte pin_arm_input = 14; //Pin A0: RX Arm - const byte pin_OtA_KS_input = 15; //Pin A1: RX Over the Air Kill Switch - const byte pin_HW_KS_input = 16; //Pin A2: HW Kill Switch - const byte pin_RX_timeout_input = 17; //Pin A3: RX timeout, if low then err +#define pin_arm_input 14 //Pin A0: RX Arm +#define pin_OtA_KS_input = 15 //Pin A1: RX Over the Air Kill Switch +#define pin_HW_KS_input = 16 //Pin A2: HW Kill Switch +#define pin_RX_timeout_input = 17 //Pin A3: RX timeout, if low then err - const byte pin_RX_operation_mode_input = 19; //Pin A5: Operation Mode -- Software/Manual RX - const byte pin_SW_KS_input = 20; //Pin A6: SW Kill Switch - const byte pin_SW_operation_mode_input = 21; //Pin A7: Software Operation Mode -- Manual/Autonomous +#define pin_RX_operation_mode_input = 19 //Pin A5: Operation Mode -- Software/Manual RX +#define pin_SW_KS_input = 20 //Pin A6: SW Kill Switch +#define pin_SW_operation_mode_input = 21 //Pin A7: Software Operation Mode -- Manual/Autonomous //DC output pins - const byte pin_SW_fail_safe_status_output = 18; //Pin A4: SW Kill switch return - - const byte pin_main_output = 2; //Pin D2: MCU fail safe system MAIN OUTPUT - const byte pin_LED_FS_locked_output = 3; //Pin D3: LED KS locked - const byte pin_status_light_Y_output = 4; //Pin D4: Status light Y - const byte pin_LED_RX_timeout_output = 5; //Pin D5: LED RX timeout - const byte pin_LED_armed_output = 6; //Pin D6: LED Armed - const byte pin_status_light_B_output = 7; //Pin D7: Status light B - const byte pin_status_light_G_output = 8; //Pin D8: Status light G - const byte pin_LED_SW_KS_status_output = 9; //Pin D9: LED SW KS - const byte pin_LED_HW_KS_status_output = 10; //Pin D10: LED HW KS - const byte pin_LED_OtA_KS_status_output = 11; //Pin D11: LED OtA KS - const byte pin_status_light_R_output = 12; //Pin D12: Status light R +#define pin_SW_fail_safe_status_output = 18 //Pin A4: SW Kill switch return +#define pin_main_output = 2 //Pin D2: MCU fail safe system MAIN OUTPUT +#define pin_LED_FS_locked_output = 3 //Pin D3: LED KS locked +#define pin_LED_RX_timeout_output = 5 //Pin D5: LED RX timeout +#define pin_LED_armed_output = 6 //Pin D6: LED Armed +#define pin_LED_SW_KS_status_output = 9 //Pin D9: LED SW KS +#define pin_LED_HW_KS_status_output = 10 //Pin D10: LED HW KS +#define pin_LED_OtA_KS_status_output = 11 //Pin D11: LED OtA KS + + +#define pin_LED_Y_output +#define pin_LED_G_output +#define pin_LED_R_output +#define FULL_BRIGHTNESS 255 +#define DIMMED_BRIGHTNESS 255 //The SW inputs may send 3.3 V or less and therefore need to be read by the ADC //The analog range 0V to 5V corresponds to the integer range 0 to 1023. - const int analog_logic_high_cutoff = 670; +#define analog_logic_high_cutoff = 670; //Declare functions - void test_fail_safe_trigger(byte input_pin, byte LED_output_pin); - void set_main_output(bool main_output_value); - void status_lights(); - void set_RGBY(bool R, bool G, bool B, bool Y); +void test_fail_safe_trigger(byte input_pin, byte LED_output_pin); +void set_main_output(bool main_output_value); +void status_lights(); +void set_LED_RGY(byte red, byte green, byte yellow); +void default_pulsing_sequence(); +void startup_sequence(); //Global Variables - bool armed; - bool fail_safe_locked; - bool all_systems_go; \ No newline at end of file +bool armed; +bool fail_safe_locked; +bool all_systems_go; \ No newline at end of file diff --git a/fail_safe_firmware/FS_MCU_Arduino_Nano/FS_MCU_Arduino_Nano.ino b/fail_safe_firmware/FS_MCU_Arduino_Nano/FS_MCU_Arduino_Nano.ino index a50ed21..0f2286d 100644 --- a/fail_safe_firmware/FS_MCU_Arduino_Nano/FS_MCU_Arduino_Nano.ino +++ b/fail_safe_firmware/FS_MCU_Arduino_Nano/FS_MCU_Arduino_Nano.ino @@ -21,18 +21,19 @@ void setup() { pinMode(pin_SW_fail_safe_status_output, OUTPUT); pinMode(pin_main_output, OUTPUT); pinMode(pin_LED_FS_locked_output, OUTPUT); - pinMode(pin_status_light_Y_output, OUTPUT); pinMode(pin_LED_RX_timeout_output, OUTPUT); pinMode(pin_LED_armed_output, OUTPUT); - pinMode(pin_status_light_B_output, OUTPUT); - pinMode(pin_status_light_G_output, OUTPUT); pinMode(pin_LED_SW_KS_status_output, OUTPUT); pinMode(pin_LED_HW_KS_status_output, OUTPUT); pinMode(pin_LED_OtA_KS_status_output, OUTPUT); - pinMode(pin_status_light_R_output, OUTPUT); - //blackout of 24V-Status-Light - set_RGBY(LOW, LOW, LOW, LOW); + pinMode(pin_LED_R_output, output); + pinMode(pin_LED_G_output, output); + pinMode(pin_LED_Y_output, output); + + digitalWrite(pin_LED_Y_output, LOW); + digitalWrite(pin_LED_G_output, LOW); + digitalWrite(pin_LED_R_output, LOW); //test FS-PCB-LEDs digitalWrite(pin_LED_FS_locked_output, HIGH); @@ -53,28 +54,8 @@ void setup() { //test 24V-Status-Light - set_RGBY(HIGH, LOW, LOW, LOW); - delay(1000); //1 Second solid "Red" - set_RGBY(LOW, LOW, LOW, LOW); - delay(333); //.333 Second blackout - - set_RGBY(LOW, HIGH, LOW, LOW); - delay(1000); //1 Second solid "Green" - set_RGBY(LOW, LOW, LOW, LOW); - delay(333); //.333 Second blackout - - set_RGBY(LOW, LOW, HIGH, LOW); - delay(1000); //1 Second solid "Blue" - set_RGBY(LOW, LOW, LOW, LOW); - delay(333); //.333 Second blackout - - set_RGBY(LOW, LOW, LOW, HIGH); - delay(1000); //1 Second solid "Yellow" - - set_RGBY(LOW, LOW, LOW, LOW); - delay(2000); //2 Second blackout - - //Setup finished + startup_sequence(); + set_LED_RGY(DIMMED_BRIGHTNESS, DIMMED_BRIGHTNESS, DIMMED_BRIGHTNESS); } void loop() { @@ -175,35 +156,35 @@ void status_lights(){ if (all_systems_go && armed){ if(!digitalRead(pin_RX_operation_mode_input)){ //Manual - set_RGBY(LOW,LOW,HIGH,LOW); //use LOW,LOW,LOW,HIGH for competition + set_LED_RGY(0, 0, FULL_BRIGHTNESS); } else{ //SW controlled if (analogRead(pin_SW_operation_mode_input)>analog_logic_high_cutoff){ //Software manual control (solid yellow) - set_RGBY(LOW,LOW,LOW,HIGH); //change to LOW,HIGH,LOW,LOW if using simplified SW mode + set_LED_RGY(0, 0, FULL_BRIGHTNESS); } else{ //Autonomous (solid green) - set_RGBY(LOW, HIGH, LOW, LOW); + set_LED_RGY(0, FULL_BRIGHTNESS, 0); } } } else if (all_systems_go && !armed){ if(!digitalRead(pin_RX_operation_mode_input)){ - //Manual - unarmed (yellow-red flash) - set_RGBY(LOW,LOW,HIGH,HIGH); //use HIGH,LOW,HIGH,HIGH for competition + //Manual - unarmed + default_pulsing_sequence(); } else{ - //SW controlled - unarmed (yellow-red flash) + //SW controlled - unarmed if (analogRead(pin_SW_operation_mode_input)>analog_logic_high_cutoff){ - //Software manual control unarmed (yellow-red flash) - set_RGBY(HIGH,LOW,HIGH,HIGH); //change to LOW,HIGH,HIGH,LOW if using simplified SW mode + //Software manual control unarmed + default_pulsing_sequence(); } else{ - //Autonomous - unarmed (green-red flash) - set_RGBY(LOW, HIGH, HIGH, LOW); + //Autonomous - unarmed + default_pulsing_sequence(); } } @@ -211,14 +192,39 @@ void status_lights(){ else{ //KS triggered - set_RGBY(HIGH, LOW, LOW, LOW); + set_LED_RGY(FULL_BRIGHTNESS, 0, 0); } } -void set_RGBY(bool R, bool G, bool B, bool Y){ - //due to incorrectly selected Mosfet, states are inverted - digitalWrite(pin_status_light_R_output, R); //RED status pin - digitalWrite(pin_status_light_G_output, G); //GREEN status pin - digitalWrite(pin_status_light_B_output, B); //BLUE status pin - digitalWrite(pin_status_light_Y_output, Y); //YELLOW status pin -} \ No newline at end of file +void startup_sequence() { + set_LED_RGY(DIMMED_BRIGHTNESS, 0, 0); + delay(1000); + set_LED_RGY(0, DIMMED_BRIGHTNESS, 0); + delay(1000); + set_LED_RGY(0, 0, DIMMED_BRIGHTNESS); + delay(1000); + set_LED_RGY(0, 0, 0); + delay(1000); +} + +void default_pulsing_sequence() { + for(int i = 0; i < 50; i += 5) { + analogWrite(pin_LED_R_output, i); + analogWrite(pin_LED_G_output, i); + analogWrite(pin_LED_Y_output, i); + delay(30); + } + for(int i = 50; i >= 0; i -= 5) { + analogWrite(pin_LED_R_output, i); + analogWrite(pin_LED_G_output, i); + analogWrite(pin_LED_Y_output, i); + delay(30); + } +} + +void set_LED_RGY(byte red, byte green, byte yellow) { + analogWrite(pin_LED_R_output, red); // Set Red PWM + analogWrite(pin_LED_G_output, green); // Set Green PWM + analogWrite(pin_LED_Y_output, yellow); // Set Yellow PWM +} + diff --git a/status_light_MCU_Arduino_Nano/status_light_MCU_Arduino_Nano.h b/status_light_MCU_Arduino_Nano/status_light_MCU_Arduino_Nano.h new file mode 100644 index 0000000..f078d87 --- /dev/null +++ b/status_light_MCU_Arduino_Nano/status_light_MCU_Arduino_Nano.h @@ -0,0 +1,8 @@ +#include + + +//define pin numbers +#define PIN_RED 11 +#define PIN_YELLOW 10 +#define PIN_GREEN 9 +#define LED_PIN 13 // Onboard LED pin \ No newline at end of file diff --git a/status_light_MCU_Arduino_Nano/status_light_MCU_Arduino_Nano.ino b/status_light_MCU_Arduino_Nano/status_light_MCU_Arduino_Nano.ino new file mode 100644 index 0000000..46d0197 --- /dev/null +++ b/status_light_MCU_Arduino_Nano/status_light_MCU_Arduino_Nano.ino @@ -0,0 +1,88 @@ +#include "freyaStatusLightArduinoCode.h" + +//define variables +int redValue = 26; // Default value +int yellowValue = 26; // Default value +int greenValue = 26; // Default value + +int header = 0; + +enum State { RUNNING, SIGNALLOSS }; +State currentState = RUNNING; +const int timeout = 1500; +unsigned long currentTime; +unsigned long lastMessageTime = millis(); + +void receiveEvent(int howMany) { + lastMessageTime = millis(); + // Blink the LED to indicate that data was received + digitalWrite(LED_PIN, HIGH); // Turn on LED (indicate end of reception) + + if (howMany >= 4) { // Expecting 3 bytes + header = Wire.read(); + redValue = Wire.read(); + yellowValue = Wire.read(); + greenValue = Wire.read(); + } + // Return to RUNNING state if in SIGNALLOSS state + if (currentState == SIGNALLOSS) { + currentState = RUNNING; + digitalWrite(LED_PIN, LOW); // Turn off LED in RUNNING state + } +} + + +void setup() { + // Initialize the PWM pins as outputs + pinMode(PIN_RED, OUTPUT); + pinMode(PIN_YELLOW, OUTPUT); + pinMode(PIN_GREEN, OUTPUT); + + // Set the default PWM values + analogWrite(PIN_RED, redValue); + analogWrite(PIN_YELLOW, yellowValue); + analogWrite(PIN_GREEN, greenValue); + + // Initialize the I2C bus as a slave device with address 8 + Wire.begin(8); + + // Register the receive event + Wire.onReceive(receiveEvent); + + // Store current time for signal loss detection + currentTime = millis(); + + // Turn on the onboard LED to indicate that the code is running + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, LOW); +} + +void loop() { + + switch (currentState) { + case RUNNING: + // Update the PWM signals on the pins + analogWrite(PIN_RED, redValue); + analogWrite(PIN_YELLOW, yellowValue); + analogWrite(PIN_GREEN, greenValue); + // Check for timeout + currentTime = millis(); + if (currentTime - lastMessageTime > timeout) { + currentState = SIGNALLOSS; + } + break; + + case SIGNALLOSS: + analogWrite(PIN_RED, 5); + analogWrite(PIN_YELLOW, 5); + analogWrite(PIN_GREEN, 5); + delay(200); + analogWrite(PIN_RED, 0); + analogWrite(PIN_YELLOW, 0); + analogWrite(PIN_GREEN, 0); + delay(190); + break; + delay(10); + } +} +