2
2
#include < ACAN2515.h>
3
3
#include < Adafruit_NeoPixel.h>
4
4
#include " ArduinoJson.h"
5
-
6
- // Config
5
+ # include < CONBus.h >
6
+ # include < CANBusDriver.h >
7
7
8
8
static const int BLINK_PERIOD_MS = 500 ;
9
9
static const int DEFAULT_BRIGHTNESS = 50 ;
10
10
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 ;
17
15
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 ;
21
19
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)
24
22
25
- Adafruit_NeoPixel strip (NUM_COLOR_LEDS, COLOR_PIN , NEO_GRB);
23
+ Adafruit_NeoPixel strip (CLED_COUNT, CLED_PIN , NEO_GRB);
26
24
27
- // Start as mobility start and not autonomuos
25
+ // Start as mobility start and not autonomous
28
26
bool is_estopped = false ;
29
27
bool is_mobility_stopped = false ;
30
28
bool is_autonomous = false ;
@@ -35,63 +33,32 @@ int current_brightness = DEFAULT_BRIGHTNESS;
35
33
StaticJsonDocument<256 > json_in;
36
34
37
35
// Default to fading purple as a "connecting" state
38
- int color_mode = 2 ;
36
+ int color_mode = 0 ;
39
37
int current_color = strip.Color(0 ,255 ,255 );
40
38
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
+
41
45
typedef struct SafetyLightsMessage {
42
46
uint8_t autonomous: 1 ;
43
47
uint8_t eco: 1 ;
44
48
uint8_t mode: 6 ;
45
49
uint8_t brightness;
50
+
46
51
uint8_t red;
47
52
uint8_t green;
48
53
uint8_t blue;
54
+ uint8_t blink_period;
49
55
} SafetyLightsMessage;
50
56
51
57
SafetyLightsMessage last_CAN_message;
52
58
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
95
62
96
63
void modeSelector (){ // RGB mode selector
97
64
switch (color_mode){
@@ -104,6 +71,9 @@ void modeSelector(){ // RGB mode selector
104
71
case 2 : // Fade
105
72
colorFade ();
106
73
break ;
74
+ // case 3: //party mode?
75
+ // partyTime();
76
+ // break;
107
77
default :
108
78
colorSolid ();
109
79
break ;
@@ -112,51 +82,67 @@ void modeSelector(){ // RGB mode selector
112
82
113
83
CANMessage frame;
114
84
115
- void onCanRecieve () {
85
+ void onCanReceive () {
116
86
can.isr ();
87
+ can.receive (frame);
88
+
89
+ conbus_can.readCanMessage (frame.id , frame.data );
117
90
}
118
91
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 ();
122
98
123
99
SPI.setSCK (MCP2515_SCK);
124
100
SPI.setTX (MCP2515_MOSI);
125
101
SPI.setRX (MCP2515_MISO);
126
102
SPI.setCS (MCP2515_CS);
127
103
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
+
134
105
strip.begin ();
135
106
strip.show ();
136
107
strip.setBrightness (current_brightness);
137
108
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 );
142
110
111
+ Serial.println (" Configure ACAN2515" ) ;
112
+ ACAN2515Settings settings (QUARTZ_FREQUENCY, 100UL * 1000UL ) ; // CAN bit rate 100 kb/s
143
113
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
+
145
116
if (errorCode == 0 ) {
146
- Serial.println (" ACAN Configured??? " );
117
+ Serial.println (" L " );
147
118
}
148
119
else {
149
- Serial.print ( " Error: " ) ;
150
- Serial.print (errorCode) ;
120
+ Serial.print ( " Configuration error 0x " ) ;
121
+ Serial.println (errorCode, HEX) ;
151
122
}
152
123
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 , ¤t_blink_period);
128
+ conbus.addRegister (99 , ¤t_color);
129
+
153
130
}
154
131
155
132
void loop () {
156
-
157
133
// Serial.println("test");
134
+ // frame.id = 0x13;
135
+ // frame.len = 6;
136
+ // can.tryToSend (frame) ;
158
137
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
+ }
160
146
161
147
if (Serial.available ()) {
162
148
// Read the JSON document from the "link" serial port
@@ -172,6 +158,7 @@ void loop() {
172
158
is_eco = json_in[" eco" ].as <bool >();
173
159
color_mode = json_in[" mode" ].as <int >();
174
160
current_brightness = json_in[" brightness" ].as <int >();
161
+ current_blink_period = json_in[" blink_period" ].as <int >();
175
162
current_color = strip.Color (json_green, json_red, json_blue);
176
163
}
177
164
else
@@ -184,56 +171,47 @@ void loop() {
184
171
185
172
if (can.available ()) {
186
173
can.receive (frame);
187
- // Serial.print("Received CAN ");
188
- // Serial.println(frame.id);
174
+ Serial.print (" Received CAN " );
175
+ Serial.println (frame.id );
189
176
190
177
switch (frame.id ) {
191
178
case 1 : // Mobility stop
192
179
is_mobility_stopped = true ;
180
+ break ;
193
181
case 9 : // Mobility start
194
182
is_mobility_stopped = false ;
183
+ break ;
195
184
case 13 : // Safety Lights
196
185
last_CAN_message = *(SafetyLightsMessage*)frame.data ;
197
-
198
186
is_autonomous = last_CAN_message.autonomous ;
199
187
is_eco = last_CAN_message.eco ;
200
188
color_mode = last_CAN_message.mode ;
201
189
current_brightness = last_CAN_message.brightness ;
190
+ current_blink_period = 10 *last_CAN_message.blink_period ;
202
191
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 ;
213
193
}
214
194
}
215
-
216
195
whiteFlash ();
217
196
modeSelector ();
218
-
219
- // delay(10); // Just so we aren't looping too fast
197
+ // delay(1000); // Just so we aren't looping too fast
220
198
}
221
199
222
200
void whiteFlash () {
223
201
if (!is_autonomous || is_estopped || is_mobility_stopped) {
224
202
225
203
if (!is_eco) {
226
- digitalWrite (WHITE_PIN , LOW);
204
+ digitalWrite (W_PIN , LOW);
227
205
} else {
228
- analogWrite (WHITE_PIN , 128 );
206
+ analogWrite (W_PIN , 128 );
229
207
}
230
208
231
209
return ;
232
210
}
233
211
if (!is_eco) {
234
- digitalWrite (WHITE_PIN , (millis () / BLINK_PERIOD_MS ) % 2 );
212
+ digitalWrite (W_PIN , (millis () / current_blink_period ) % 2 );
235
213
} else {
236
- analogWrite (WHITE_PIN,(( millis () / BLINK_PERIOD_MS ) % 2 ) * 128 );
214
+ analogWrite (W_PIN,( 255 -(( millis () / current_blink_period ) % 2 ) * 127 ) );
237
215
}
238
216
}
239
217
@@ -245,7 +223,7 @@ void colorSolid() { // LED strip solid effect
245
223
246
224
void colorFlash (){ // LED strip flashing effect
247
225
strip.setBrightness (current_brightness);
248
- if ((millis () / BLINK_PERIOD_MS ) % 2 == 0 ){
226
+ if ((millis () / current_blink_period ) % 2 == 0 ){
249
227
strip.fill (current_color);
250
228
strip.show ();
251
229
}
@@ -255,7 +233,7 @@ void colorFlash(){ // LED strip flashing effect
255
233
}
256
234
257
235
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);
259
237
strip.fill (current_color);
260
238
strip.show ();
261
239
}
@@ -264,3 +242,18 @@ void colorClear(){ // Clear LED strip
264
242
strip.fill (0 );
265
243
strip.show ();
266
244
}
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