Skip to content

Commit 004a028

Browse files
authored
Merge pull request #3 from KentuckySolarCar/light-router-dev
Light Router Re-Implementation from TeensyBottomShellReborn into Develop
2 parents f3271d9 + 984b9e2 commit 004a028

File tree

6 files changed

+441
-0
lines changed

6 files changed

+441
-0
lines changed

.ccls

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
clang
2+
3+
%c -Wall -ffunction-sections -fdata-sections -mthumb -mcpu=cortex-m7 -nostdlib -mfloat-abi=hard -mfpu=fpv5-d16 -O2
4+
%cpp -fno-exceptions -felide-constructors -fno-rtti -std=gnu++17 -Wno-error=narrowing -fpermissive -fno-threadsafe-statics -Wall -ffunction-sections -fdata-sections -mthumb -mcpu=cortex-m7 -nostdlib -mfloat-abi=hard -mfpu=fpv5-d16 -O2
5+
6+
-I/home/ben/solar-car/TeensyBottomShellReborn/include
7+
-I/home/ben/solar-car/TeensyBottomShellReborn/src
8+
-I/home/ben/solar-car/TeensyBottomShellReborn/.pio/libdeps/teensy41/ChRt/src
9+
-I/home/ben/solar-car/TeensyBottomShellReborn/lib/function
10+
-I/home/ben/.platformio/packages/framework-arduinoteensy/cores/teensy4
11+
-I/home/ben/solar-car/TeensyBottomShellReborn/.pio/libdeps/teensy41/Unity/src
12+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/ADC
13+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/AccelStepper/src
14+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Adafruit_NeoPixel
15+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Adafruit_STMPE610
16+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Adafruit_VS1053
17+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Adafruit_nRF8001
18+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Adafruit_nRF8001/utility
19+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/AltSoftSerial
20+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Artnet
21+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Audio
22+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Audio/utility
23+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Bounce
24+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Bounce2/src
25+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/CapacitiveSensor
26+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/CryptoAccel/src
27+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/DS1307RTC
28+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/DmxSimple
29+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/DogLcd
30+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/EEPROM
31+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/EasyTransfer/src
32+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/EasyTransferI2C/src
33+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Encoder
34+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Encoder/utility
35+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Entropy
36+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Ethernet/src
37+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FNET/src
38+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FastCRC
39+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FastLED/src
40+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FlexCAN
41+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FlexCAN_T4
42+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FlexIO_t4/src
43+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FlexiTimer2
44+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FreqCount
45+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FreqMeasure
46+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FreqMeasureMulti
47+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/FrequencyTimer2
48+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/ILI9341_t3
49+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/ILI9488_t3/src
50+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/IRremote/src
51+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Keypad/src
52+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/LedControl/src
53+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/LedDisplay
54+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/LiquidCrystal/src
55+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/LiquidCrystalFast
56+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/LittleFS/src
57+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/LowPower
58+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/MFRC522/src
59+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/MIDI/src
60+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Metro
61+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/MsTimer2
62+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/NXPMotionSense
63+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/NXPMotionSense/utility
64+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/NativeEthernet/src
65+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/OSC
66+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/OctoWS2811
67+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/OneWire
68+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/PS2Keyboard
69+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/PS2Keyboard/utility
70+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/PWMServo
71+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Ping
72+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/PulsePosition
73+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/QuadEncoder
74+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/RA8875/src
75+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/RadioHead
76+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/ResponsiveAnalogRead/src
77+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/SD/src
78+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/SPI
79+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/SPIFlash
80+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/ST7735_t3/src
81+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/SdFat/src
82+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/SerialFlash
83+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Servo
84+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/ShiftPWM
85+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Snooze/src
86+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/SoftPWM
87+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/SoftwareSerial
88+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/TFT_ILI9163C
89+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Talkie
90+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/TeensyThreads
91+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Time
92+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/TimeAlarms
93+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/TimerOne
94+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/TimerThree
95+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/TinyGPS
96+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Tlc5940
97+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/TouchScreen
98+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/USBHost_t36
99+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/USBHost_t36/utility
100+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/UTFT
101+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/VirtualWire
102+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/WS2812Serial
103+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Wire
104+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/Wire/utility
105+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/XBee
106+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/XPT2046_Touchscreen
107+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/i2c_t3
108+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/ks0108
109+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/ssd1351
110+
-I/home/ben/.platformio/packages/framework-arduinoteensy/libraries/x10
111+
-I/home/ben/.platformio/packages/toolchain-gccarmnoneeabi-teensy/arm-none-eabi/include/c++/11.3.1
112+
-I/home/ben/.platformio/packages/toolchain-gccarmnoneeabi-teensy/arm-none-eabi/include/c++/11.3.1/arm-none-eabi
113+
-I/home/ben/.platformio/packages/toolchain-gccarmnoneeabi-teensy/lib/gcc/arm-none-eabi/11.3.1/include-fixed
114+
-I/home/ben/.platformio/packages/toolchain-gccarmnoneeabi-teensy/lib/gcc/arm-none-eabi/11.3.1/include
115+
-I/home/ben/.platformio/packages/toolchain-gccarmnoneeabi-teensy/arm-none-eabi/include
116+
117+
-DPLATFORMIO=60116
118+
-D__IMXRT1062__
119+
-DARDUINO_TEENSY41
120+
-DUSB_SERIAL
121+
-DARDUINO=10805
122+
-DTEENSYDUINO=159
123+
-DCORE_TEENSY
124+
-DF_CPU=600000000
125+
-DLAYOUT_US_ENGLISH

.vscode/settings.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"files.associations": {
3+
"cstdlib": "cpp",
4+
"iostream": "cpp",
5+
"iosfwd": "cpp",
6+
"ostream": "cpp",
7+
"*.tpp": "cpp",
8+
"*.tcc": "cpp"
9+
}
10+
}

src/common/common.h

+14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@
33

44
#include <Arduino.h>
55
#include "can_setup.h"
6+
#include "pin_definitions.h"
67
#include <ChRt.h>
78

9+
#define BAUD_RATE 500000
10+
11+
#define LIGHTS_PRIO (NORMALPRIO + 2)
12+
#define LIGHTS_WA 4096
13+
#define LIGHTS_SLEEP 100
14+
15+
#define ROUTER_PRIO (NORMALPRIO + 3)
16+
#define ROUTER_WA 2048
17+
#define ROUTER_SLEEP 40
18+
19+
#define HEARTBEAT_PRIO (LOWPRIO)
20+
#define HEARTBEAT_WA 512
21+
#define HEARTBEAT_SLEEP 100
822

923
#endif

src/common/pin_definitions.h

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @file pin_definitions.h
3+
* @brief Holds macros that serve as a hardware abstraction layer. Copied from TBS
4+
* @version 0.01
5+
*
6+
*
7+
*/
8+
9+
#ifndef BOTTOM_SHELL_H
10+
#define BOTTOM_SHELL_H
11+
12+
//TODO: Find pin for TRIGGER_MODE
13+
14+
//Teensy 4.1 pin mappings, all names are referenced to the Eagle schematics
15+
16+
//Left side of the board diagram
17+
#define TWELVE_V_MEASURE A0 //14, on the Eagle schematic as "12V_MEASURE"
18+
//15 is empty
19+
#define THROT1_IN A2 //16
20+
#define THROT2_IN A3 //17
21+
#define BPT_FR_IN A4 //18
22+
#define BPT_RR_IN A5 //19
23+
#define BPT_FL_IN A6 //20
24+
#define BPT_RL_IN A7 //21
25+
#define CAN0_TX A8 //22
26+
#define CAN0_RX A9 //23
27+
#define BRAKE_SWITCH A10 //24 EX7
28+
//A11 25 is empty
29+
//A12 26 is empty
30+
//A13 27 is empty
31+
#define RT_CTRL A14 //38
32+
#define LT_CTRL A15 //39
33+
#define BRAKE_CTRL A16 //40
34+
#define RUNNING_LIGHTS A17 //41
35+
//30 is empty
36+
//31 is empty
37+
#define BPS_STROBE 3
38+
//4 is empty
39+
#define FORWARD_REVERSE_IN 5 // EX1
40+
#define V_BAT VBAT
41+
42+
//Right side of the board diagram
43+
#define V_USB VUSB
44+
//#define USB0_D- D-
45+
//#define USB0_D+ D+
46+
#define CAN1_RX 0
47+
#define CAN_TX 1
48+
49+
//pins that were added on the updated bottom shell board schematic
50+
#define GPS_RX 7
51+
#define GPS_TX 8
52+
//28 is empty
53+
//29 is empty
54+
55+
#define CAN0_STB 34
56+
#define CAN1_STB 35
57+
#define EX4 10 // CHARGE_ENABLE/CHARGE_DISABLE
58+
#define EX5 11
59+
#define EX6 12
60+
//heartbeat is on 13, DO NOT USE
61+
//32 is empty
62+
#define RECALIBRATION_MODE_PIN 9 // EX 3
63+
#define TRIGGER_MODE 6 // EX 2
64+
#define GPS_MISC 33
65+
#define GPS_TRIG 36
66+
#define GPS_PPS 37
67+
68+
69+
70+
#endif

src/light/light.cpp

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include "light.h"
2+
3+
// VERY TEMPORARY STUFF FOR ERROR'S SAKE
4+
#define CAN_BPS_STATUS 0
5+
#define CAN_BUTTON_LEFT_TURN 1
6+
#define CAN_BUTTON_RIGHT_TURN 2
7+
#define CAN_BUTTON_HEADLIGHTS 3
8+
#define CAN_BUTTON_Y 4
9+
10+
// the setup of this code is as follows:
11+
// we define two threads, one for routing can messages, and the other for updating the blinking of the lights.
12+
// the can router updates our internal state for the lights, while the blinker reflects this on the physical system.
13+
14+
struct {
15+
bool isLeftTurnOn = false;
16+
bool isRightTurnOn = false;
17+
bool isStrobeOn = false;
18+
bool isHeadlightOn = false;
19+
bool isBrakeOn = false;
20+
bool isLightTestOn = false;
21+
} lightState;
22+
23+
// Explanation of THD_WORKING_AREA:
24+
// the first parameter to this macro is the name of the data.
25+
// the second parameter is the size of the data we're working with. In this case, 4096 bytes.
26+
// we then define a function that operates in a thread called in the setup function.
27+
static THD_WORKING_AREA(wa_light_route, LIGHTS_WA);
28+
static THD_FUNCTION(light_route_thd, arg) {
29+
(void) arg;
30+
31+
CAN_message_t msg;
32+
while (true) {
33+
while (MainCan.read(msg)) {
34+
switch (msg.id) {
35+
case CAN_BPS_STATUS: updateBPSLight(msg); break;
36+
case CAN_BUTTON_LEFT_TURN: updateLeftLight(msg); break;
37+
case CAN_BUTTON_RIGHT_TURN: updateRightLight(msg); break;
38+
case CAN_BUTTON_HEADLIGHTS: updateHeadlights(msg); break;
39+
case CAN_BUTTON_Y: updateYButtonLong(msg); break;
40+
default: {
41+
// we'll figure out alert messages later
42+
break;
43+
}
44+
}
45+
}
46+
47+
if (isBrakePressed) {
48+
updateBrakeLights(isBrakePressed);
49+
}
50+
51+
handleBPSLight();
52+
chThdSleepMilliseconds(ROUTER_SLEEP);
53+
}
54+
}
55+
56+
static THD_WORKING_AREA(wa_light_blink, LIGHTS_WA);
57+
static THD_FUNCTION(light_blink_thd, arg) {
58+
// prevent compiler for complaining about unused stuff
59+
(void) arg;
60+
61+
uint32_t blinkTimer = millis();
62+
bool isOn = false;
63+
while (true) {
64+
// change light status
65+
if (!lightState.isLeftTurnOn && !lightState.isRightTurnOn) {
66+
isOn = false;
67+
digitalWrite(LT_CTRL, LOW);
68+
digitalWrite(RT_CTRL, LOW);
69+
} else if (millis() - blinkTimer > TURN_SIGNALS_SPEED) {
70+
blinkTimer = millis();
71+
isOn = !isOn;
72+
digitalWrite(LT_CTRL, isOn && lightState.isLeftTurnOn);
73+
digitalWrite(RT_CTRL, isOn && lightState.isRightTurnOn);
74+
}
75+
76+
digitalWrite(RUNNING_LIGHTS, lightState.isHeadlightOn);
77+
digitalWrite(BRAKE_CTRL, lightState.isBrakeOn);
78+
79+
if (lightState.isLightTestOn && isThrottleUnlocked) {
80+
if (millis() - blinkTimer > TURN_SIGNALS_BRIGHTNESS) {
81+
blinkTimer = millis();
82+
isOn = !isOn;
83+
digitalWrite(LT_CTRL, isOn);
84+
digitalWrite(RT_CTRL, isOn);
85+
digitalWrite(BRAKE_CTRL, isOn);
86+
digitalWrite(RUNNING_LIGHTS, isOn);
87+
digitalWrite(BPS_STROBE, isOn);
88+
}
89+
}
90+
91+
chThdSleepMilliseconds(LIGHTS_SLEEP);
92+
}
93+
}
94+
95+
void lightSetup() {
96+
pinMode(BPS_STROBE,OUTPUT);
97+
pinMode(LT_CTRL,OUTPUT);
98+
pinMode(RT_CTRL,OUTPUT);
99+
pinMode(RUNNING_LIGHTS,OUTPUT);
100+
pinMode(BRAKE_CTRL,OUTPUT);
101+
102+
digitalWrite(BPS_STROBE, LOW);
103+
104+
chThdCreateStatic(wa_light_route, sizeof(wa_light_route), ROUTER_PRIO, light_route_thd, NULL);
105+
chThdCreateStatic(wa_light_blink, sizeof(wa_light_blink), LIGHTS_PRIO, light_blink_thd, NULL);
106+
}
107+
108+
void updateLeftLight(CAN_message_t ltMsg){
109+
lightState.isLeftTurnOn = ltMsg.buf[0] % 2;
110+
}
111+
112+
void updateRightLight(CAN_message_t rtMsg){
113+
lightState.isRightTurnOn = rtMsg.buf[0] % 2;
114+
}
115+
116+
void updateHeadlights(CAN_message_t lightMsg){
117+
lightState.isHeadlightOn = lightMsg.buf[0] % 2;
118+
}
119+
120+
void updateBrakeLights(bool brakeMsg){
121+
lightState.isBrakeOn = brakeMsg;
122+
}
123+
124+
void updateBPSLight(CAN_message_t statusMsg){
125+
lightState.isStrobeOn = statusMsg.buf[1]; // This is 1(TRUE) if the battery pack is terminated by a fault, or 0(FALSE) if conditions are safe
126+
}
127+
128+
void updateYButtonLong(CAN_message_t lightTestMsg){
129+
lightState.isLightTestOn = lightTestMsg.buf[2] % 2;
130+
}
131+
132+
133+
void handleBPSLight(){
134+
static uint32_t timer = millis();
135+
static bool isOn = false;
136+
137+
if (lightState.isStrobeOn){
138+
if ((millis() - timer) > TURN_SIGNALS_SPEED){
139+
timer = millis();
140+
isOn = !isOn;
141+
digitalWrite(BPS_STROBE,isOn);
142+
}
143+
} else {
144+
isOn = false;
145+
digitalWrite(BPS_STROBE, isOn);
146+
}
147+
}

0 commit comments

Comments
 (0)