Skip to content

Commit 1cacb4e

Browse files
authored
Updated safety lights code
1 parent f2e447f commit 1cacb4e

File tree

1 file changed

+93
-100
lines changed

1 file changed

+93
-100
lines changed

firmware/safety_lights/safety_lights.ino

Lines changed: 93 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@
22
#include <ACAN2515.h>
33
#include <Adafruit_NeoPixel.h>
44
#include "ArduinoJson.h"
5-
6-
// Config
5+
#include <CONBus.h>
6+
#include <CANBusDriver.h>
77

88
static const int BLINK_PERIOD_MS = 500;
99
static const int DEFAULT_BRIGHTNESS = 50;
1010

11-
// Definitions
12-
13-
static const int NUM_COLOR_LEDS = 61;
14-
static const int COLOR_PIN = 22;
15-
static const int E_STOP_PIN = 11;
16-
static const int WHITE_PIN = 18;
11+
static const int CLED_COUNT = 24;
12+
static const int CLED_PIN = 20;
13+
static const int ESTOP_PIN = 6;
14+
static const int W_PIN = 19;
1715

18-
static const byte MCP2515_SCK = 2; // SCK input of MCP2515
19-
static const byte MCP2515_MOSI = 3; // SDI input of MCP2515
20-
static const byte MCP2515_MISO = 0; // SDO output of MCP2517
16+
static const byte MCP2515_SCK = 2;
17+
static const byte MCP2515_MOSI = 3;
18+
static const byte MCP2515_MISO = 0;
2119

22-
static const byte MCP2515_CS = 1; // CS input of MCP2515
23-
static const byte MCP2515_INT = 4; // INT output of MCP2515
20+
static const byte MCP2515_CS = 1 ; // CS input of MCP2515 (adapt to your design)
21+
static const byte MCP2515_INT = 4 ; // INT output of MCP2515 (adapt to your design)
2422

25-
Adafruit_NeoPixel strip(NUM_COLOR_LEDS, COLOR_PIN, NEO_GRB);
23+
Adafruit_NeoPixel strip(CLED_COUNT, CLED_PIN, NEO_GRB);
2624

27-
// Start as mobility start and not autonomuos
25+
// Start as mobility start and not autonomous
2826
bool is_estopped = false;
2927
bool is_mobility_stopped = false;
3028
bool is_autonomous = false;
@@ -35,63 +33,32 @@ int current_brightness = DEFAULT_BRIGHTNESS;
3533
StaticJsonDocument<256> json_in;
3634

3735
// Default to fading purple as a "connecting" state
38-
int color_mode = 2;
36+
int color_mode = 0;
3937
int current_color = strip.Color(0,255,255);
4038

39+
// Setup CONBus variables
40+
CONBus::CONBus conbus;
41+
CONBus::CANBusDriver conbus_can(conbus, 13); // device id 13
42+
43+
int current_blink_period = BLINK_PERIOD_MS; // how fast the light would blink per
44+
4145
typedef struct SafetyLightsMessage {
4246
uint8_t autonomous: 1;
4347
uint8_t eco: 1;
4448
uint8_t mode: 6;
4549
uint8_t brightness;
50+
4651
uint8_t red;
4752
uint8_t green;
4853
uint8_t blue;
54+
uint8_t blink_period;
4955
} SafetyLightsMessage;
5056

5157
SafetyLightsMessage last_CAN_message;
5258

53-
ACAN2515 can(MCP2515_CS, SPI, MCP2515_INT);
54-
55-
static const uint32_t QUARTZ_FREQUENCY = 8UL * 1000UL * 1000UL; // 8 MHZ
56-
57-
uint32_t colorSelector(int num){ //0-15 to color
58-
switch(num){
59-
case 0: //black
60-
return strip.Color(0,0,0);
61-
case 1: //red
62-
return strip.Color(0,255,0);
63-
case 2: //orange
64-
return strip.Color(127,255,0);
65-
case 3: //yellow
66-
return strip.Color(255,255,0);
67-
case 4: //lime
68-
return strip.Color(255,127,0);
69-
case 5: //green
70-
return strip.Color(255,0,0);
71-
case 6: //cyan-green
72-
return strip.Color(255,0,127);
73-
case 7: //cyan
74-
return strip.Color(255,0,255);
75-
case 8: //light blue
76-
return strip.Color(127,0,255);
77-
case 9: //blue
78-
return strip.Color(0,0,255);
79-
case 10: //violet
80-
return strip.Color(0,127,255);
81-
case 11: //purple
82-
return strip.Color(0,255,255);
83-
case 12: //pink
84-
return strip.Color(0,255,127);
85-
case 13: //white green
86-
return strip.Color(255,127,127);
87-
case 14: //white red
88-
return strip.Color(127,255,127);
89-
case 15: //white
90-
return strip.Color(225,255,255);
91-
default:
92-
return strip.Color(0,0,0);
93-
}
94-
}
59+
ACAN2515 can (MCP2515_CS, SPI, MCP2515_INT) ;
60+
61+
static const uint32_t QUARTZ_FREQUENCY = 8UL * 1000UL * 1000UL ; // 8 MHz
9562

9663
void modeSelector(){ // RGB mode selector
9764
switch(color_mode){
@@ -104,6 +71,9 @@ void modeSelector(){ // RGB mode selector
10471
case 2: //Fade
10572
colorFade();
10673
break;
74+
// case 3: //party mode?
75+
// partyTime();
76+
// break;
10777
default:
10878
colorSolid();
10979
break;
@@ -112,51 +82,67 @@ void modeSelector(){ // RGB mode selector
11282

11383
CANMessage frame;
11484

115-
void onCanRecieve() {
85+
void onCanReceive() {
11686
can.isr();
87+
can.receive(frame);
88+
89+
conbus_can.readCanMessage(frame.id, frame.data);
11790
}
11891

119-
void setup() {
120-
Serial.begin (115200);
121-
delay(2000);
92+
void setup () {
93+
pinMode(LED_BUILTIN, OUTPUT);
94+
pinMode(W_PIN, OUTPUT);
95+
digitalWrite(LED_BUILTIN, LOW);
96+
97+
Serial.begin();
12298

12399
SPI.setSCK(MCP2515_SCK);
124100
SPI.setTX(MCP2515_MOSI);
125101
SPI.setRX(MCP2515_MISO);
126102
SPI.setCS(MCP2515_CS);
127103
SPI.begin();
128-
129-
pinMode(LED_BUILTIN, OUTPUT);
130-
pinMode(WHITE_PIN, OUTPUT);
131-
pinMode(E_STOP_PIN, INPUT);
132-
digitalWrite(LED_BUILTIN, HIGH);
133-
104+
134105
strip.begin();
135106
strip.show();
136107
strip.setBrightness(current_brightness);
137108

138-
analogWriteFreq(100);
139-
140-
Serial.println("Configure ACAN2515");
141-
ACAN2515Settings settings(QUARTZ_FREQUENCY, 100UL * 1000UL); // CAN bit rate 100 kb/s
109+
analogWriteFreq(100);
142110

111+
Serial.println ("Configure ACAN2515") ;
112+
ACAN2515Settings settings (QUARTZ_FREQUENCY, 100UL * 1000UL) ; // CAN bit rate 100 kb/s
143113
settings.mRequestedMode = ACAN2515Settings::NormalMode ; // Select Normal mode
144-
const uint16_t errorCode = can.begin(settings, onCanRecieve );
114+
const uint16_t errorCode = can.begin (settings, [] { can.isr () ; }) ;
115+
145116
if (errorCode == 0) {
146-
Serial.println("ACAN Configured???");
117+
Serial.println("L");
147118
}
148119
else{
149-
Serial.print("Error: ");
150-
Serial.print(errorCode);
120+
Serial.print ("Configuration error 0x") ;
121+
Serial.println (errorCode, HEX) ;
151122
}
152123

124+
// Create CONBus registers
125+
// The addresses can be anything from 0 to 255
126+
// Both the blink period and the color of the leds
127+
conbus.addRegister(7, &current_blink_period);
128+
conbus.addRegister(99, &current_color);
129+
153130
}
154131

155132
void loop() {
156-
157133
// Serial.println("test");
134+
// frame.id = 0x13;
135+
// frame.len = 6;
136+
// can.tryToSend (frame) ;
158137

159-
is_estopped = !digitalRead(E_STOP_PIN);
138+
is_estopped = !digitalRead(ESTOP_PIN);
139+
140+
if(is_estopped == false) {
141+
digitalWrite(LED_BUILTIN, HIGH);
142+
}
143+
else {
144+
digitalWrite(LED_BUILTIN, LOW);
145+
}
160146

161147
if (Serial.available()) {
162148
// Read the JSON document from the "link" serial port
@@ -172,6 +158,7 @@ void loop() {
172158
is_eco = json_in["eco"].as<bool>();
173159
color_mode = json_in["mode"].as<int>();
174160
current_brightness = json_in["brightness"].as<int>();
161+
current_blink_period = json_in["blink_period"].as<int>();
175162
current_color = strip.Color(json_green, json_red, json_blue);
176163
}
177164
else
@@ -184,56 +171,47 @@ void loop() {
184171

185172
if (can.available()) {
186173
can.receive(frame);
187-
// Serial.print("Received CAN ");
188-
// Serial.println(frame.id);
174+
Serial.print("Received CAN ");
175+
Serial.println(frame.id);
189176

190177
switch (frame.id) {
191178
case 1: // Mobility stop
192179
is_mobility_stopped = true;
180+
break;
193181
case 9: // Mobility start
194182
is_mobility_stopped = false;
183+
break;
195184
case 13: // Safety Lights
196185
last_CAN_message = *(SafetyLightsMessage*)frame.data;
197-
198186
is_autonomous = last_CAN_message.autonomous;
199187
is_eco = last_CAN_message.eco;
200188
color_mode = last_CAN_message.mode;
201189
current_brightness = last_CAN_message.brightness;
190+
current_blink_period = 10*last_CAN_message.blink_period;
202191
current_color = strip.Color(last_CAN_message.green, last_CAN_message.red, last_CAN_message.blue);
203-
204-
// Serial.print(last_CAN_message.mode);
205-
// Serial.print(", ");
206-
// Serial.print(last_CAN_message.red);
207-
// Serial.print(", ");
208-
// Serial.print(last_CAN_message.green);
209-
// Serial.print(", ");
210-
// Serial.print(last_CAN_message.blue);
211-
// Serial.print(", ");
212-
// Serial.println(last_CAN_message.brightness);
192+
break;
213193
}
214194
}
215-
216195
whiteFlash();
217196
modeSelector();
218-
219-
// delay(10); // Just so we aren't looping too fast
197+
// delay(1000); // Just so we aren't looping too fast
220198
}
221199

222200
void whiteFlash() {
223201
if (!is_autonomous || is_estopped || is_mobility_stopped) {
224202

225203
if (!is_eco) {
226-
digitalWrite(WHITE_PIN, LOW);
204+
digitalWrite(W_PIN, LOW);
227205
} else {
228-
analogWrite(WHITE_PIN, 128);
206+
analogWrite(W_PIN, 128);
229207
}
230208

231209
return;
232210
}
233211
if (!is_eco) {
234-
digitalWrite(WHITE_PIN, (millis() / BLINK_PERIOD_MS) % 2);
212+
digitalWrite(W_PIN, (millis() / current_blink_period) % 2);
235213
} else {
236-
analogWrite(WHITE_PIN,((millis() / BLINK_PERIOD_MS) % 2) * 128);
214+
analogWrite(W_PIN,(255-((millis() / current_blink_period) % 2) * 127));
237215
}
238216
}
239217

@@ -245,7 +223,7 @@ void colorSolid() { // LED strip solid effect
245223

246224
void colorFlash(){ // LED strip flashing effect
247225
strip.setBrightness(current_brightness);
248-
if ((millis() / BLINK_PERIOD_MS) % 2 == 0){
226+
if ((millis() / current_blink_period) % 2 == 0){
249227
strip.fill(current_color);
250228
strip.show();
251229
}
@@ -255,7 +233,7 @@ void colorFlash(){ // LED strip flashing effect
255233
}
256234

257235
void colorFade(){ // LED strip fading effect
258-
strip.setBrightness(abs(sin(millis() / (float)BLINK_PERIOD_MS)) * current_brightness);
236+
strip.setBrightness(abs(sin(millis() / (float)current_blink_period)) * current_brightness);
259237
strip.fill(current_color);
260238
strip.show();
261239
}
@@ -264,3 +242,18 @@ void colorClear(){ // Clear LED strip
264242
strip.fill(0);
265243
strip.show();
266244
}
245+
246+
// void partyTime() // I love parties
247+
// {
248+
// strip.setBrightness(current_brightness);
249+
// if((millis() / current_blink_period / 2) % 2 == 0) {
250+
// for(int i=0;i<CLED_COUNT;i++){
251+
// uint32_t color = strip.Color(random(256), random(256), random(256));
252+
// strip.setPixelColor(i, color);
253+
// }
254+
// }
255+
// strip.show();
256+
// }
257+
258+
259+

0 commit comments

Comments
 (0)