Skip to content

Commit c86d9c4

Browse files
committed
Improvements and clean-up
- clean-up printfs - removed consoleLog function with respective files - removed Delay when using printf - renamed speedL, speedR to cmdL, cmdR - corrected Arduino baud rate - updated FLASH write pointer cast int16_t to uint16_t
1 parent d1286e2 commit c86d9c4

File tree

10 files changed

+909
-270
lines changed

10 files changed

+909
-270
lines changed

Arduino/hoverserial/hoverserial.ino

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
// *******************************************************************
2424

2525
// ########################## DEFINES ##########################
26-
#define HOVER_SERIAL_BAUD 38400 // [-] Baud rate for HoverSerial (used to communicate with the hoverboard)
26+
#define HOVER_SERIAL_BAUD 115200 // [-] Baud rate for HoverSerial (used to communicate with the hoverboard)
2727
#define SERIAL_BAUD 115200 // [-] Baud rate for built-in Serial (used for the Serial Monitor)
2828
#define START_FRAME 0xABCD // [-] Start frme definition for reliable serial communication
2929
#define TIME_SEND 100 // [ms] Sending time interval
3030
#define SPEED_MAX_TEST 300 // [-] Maximum speed for testing
31-
//#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable)
31+
// #define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable)
3232

3333
#include <SoftwareSerial.h>
34-
SoftwareSerial HoverSerial(2,3); // RX, TX
34+
SoftwareSerial HoverSerial(2,3); // RX, TX
3535

3636
// Global variables
3737
uint8_t idx = 0; // Index for new data pointer
@@ -41,7 +41,7 @@ byte incomingByte;
4141
byte incomingBytePrev;
4242

4343
typedef struct{
44-
uint16_t start;
44+
uint16_t start;
4545
int16_t steer;
4646
int16_t speed;
4747
uint16_t checksum;
@@ -50,12 +50,12 @@ SerialCommand Command;
5050

5151
typedef struct{
5252
uint16_t start;
53-
int16_t cmd1;
54-
int16_t cmd2;
55-
int16_t speedR_meas;
56-
int16_t speedL_meas;
57-
int16_t batVoltage;
58-
int16_t boardTemp;
53+
int16_t cmd1;
54+
int16_t cmd2;
55+
int16_t speedR_meas;
56+
int16_t speedL_meas;
57+
int16_t batVoltage;
58+
int16_t boardTemp;
5959
uint16_t cmdLed;
6060
uint16_t checksum;
6161
} SerialFeedback;
@@ -67,7 +67,7 @@ void setup()
6767
{
6868
Serial.begin(SERIAL_BAUD);
6969
Serial.println("Hoverboard Serial v1.0");
70-
70+
7171
HoverSerial.begin(HOVER_SERIAL_BAUD);
7272
pinMode(LED_BUILTIN, OUTPUT);
7373
}
@@ -88,59 +88,59 @@ void Send(int16_t uSteer, int16_t uSpeed)
8888
// ########################## RECEIVE ##########################
8989
void Receive()
9090
{
91-
// Check for new data availability in the Serial buffer
92-
if (HoverSerial.available()) {
93-
incomingByte = HoverSerial.read(); // Read the incoming byte
94-
bufStartFrame = ((uint16_t)(incomingByte) << 8) | incomingBytePrev; // Construct the start frame
95-
}
96-
else {
97-
return;
98-
}
91+
// Check for new data availability in the Serial buffer
92+
if (HoverSerial.available()) {
93+
incomingByte = HoverSerial.read(); // Read the incoming byte
94+
bufStartFrame = ((uint16_t)(incomingByte) << 8) | incomingBytePrev; // Construct the start frame
95+
}
96+
else {
97+
return;
98+
}
9999

100100
// If DEBUG_RX is defined print all incoming bytes
101101
#ifdef DEBUG_RX
102-
Serial.print(incomingByte);
103-
return;
104-
#endif
105-
106-
// Copy received data
107-
if (bufStartFrame == START_FRAME) { // Initialize if new data is detected
108-
p = (byte *)&NewFeedback;
109-
*p++ = incomingBytePrev;
110-
*p++ = incomingByte;
111-
idx = 2;
112-
} else if (idx >= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data
113-
*p++ = incomingByte;
114-
idx++;
115-
}
116-
117-
// Check if we reached the end of the package
118-
if (idx == sizeof(SerialFeedback)) {
119-
uint16_t checksum;
120-
checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas
121-
^ NewFeedback.batVoltage ^ NewFeedback.boardTemp ^ NewFeedback.cmdLed);
122-
123-
// Check validity of the new data
124-
if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) {
125-
// Copy the new data
126-
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback));
127-
128-
// Print data to built-in Serial
129-
Serial.print("1: "); Serial.print(Feedback.cmd1);
130-
Serial.print(" 2: "); Serial.print(Feedback.cmd2);
131-
Serial.print(" 3: "); Serial.print(Feedback.speedR_meas);
132-
Serial.print(" 4: "); Serial.print(Feedback.speedL_meas);
133-
Serial.print(" 5: "); Serial.print(Feedback.batVoltage);
134-
Serial.print(" 6: "); Serial.print(Feedback.boardTemp);
135-
Serial.print(" 7: "); Serial.println(Feedback.cmdLed);
136-
} else {
137-
Serial.println("Non-valid data skipped");
138-
}
139-
idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle)
140-
}
141-
142-
// Update previous states
143-
incomingBytePrev = incomingByte;
102+
Serial.print(incomingByte);
103+
return;
104+
#endif
105+
106+
// Copy received data
107+
if (bufStartFrame == START_FRAME) { // Initialize if new data is detected
108+
p = (byte *)&NewFeedback;
109+
*p++ = incomingBytePrev;
110+
*p++ = incomingByte;
111+
idx = 2;
112+
} else if (idx >= 2 && idx < sizeof(SerialFeedback)) { // Save the new received data
113+
*p++ = incomingByte;
114+
idx++;
115+
}
116+
117+
// Check if we reached the end of the package
118+
if (idx == sizeof(SerialFeedback)) {
119+
uint16_t checksum;
120+
checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas
121+
^ NewFeedback.batVoltage ^ NewFeedback.boardTemp ^ NewFeedback.cmdLed);
122+
123+
// Check validity of the new data
124+
if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) {
125+
// Copy the new data
126+
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback));
127+
128+
// Print data to built-in Serial
129+
Serial.print("1: "); Serial.print(Feedback.cmd1);
130+
Serial.print(" 2: "); Serial.print(Feedback.cmd2);
131+
Serial.print(" 3: "); Serial.print(Feedback.speedR_meas);
132+
Serial.print(" 4: "); Serial.print(Feedback.speedL_meas);
133+
Serial.print(" 5: "); Serial.print(Feedback.batVoltage);
134+
Serial.print(" 6: "); Serial.print(Feedback.boardTemp);
135+
Serial.print(" 7: "); Serial.println(Feedback.cmdLed);
136+
} else {
137+
Serial.println("Non-valid data skipped");
138+
}
139+
idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle)
140+
}
141+
142+
// Update previous states
143+
incomingBytePrev = incomingByte;
144144
}
145145

146146
// ########################## LOOP ##########################

Inc/config.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
- button1 and button2: digital input values. 0 or 1
125125
- adc_buffer.l_tx2 and adc_buffer.l_rx2: unfiltered ADC values (you do not need them). 0 to 4095
126126
Outputs:
127-
- speedR and speedL: normal driving INPUT_MIN to INPUT_MAX
127+
- cmdL and cmdR: normal driving INPUT_MIN to INPUT_MAX
128128
*/
129129
#define COM_CTRL 0 // [-] Commutation Control Type
130130
#define SIN_CTRL 1 // [-] Sinusoidal Control Type
@@ -205,38 +205,30 @@
205205
* Be careful not to use the red wire of the cable. 15v will destroy everything.
206206
* If you are using VARIANT_NUNCHUK, disable it temporarily.
207207
* enable DEBUG_SERIAL_USART3 or DEBUG_SERIAL_USART2
208-
* and DEBUG_SERIAL_ASCII use asearial terminal.
209208
*
210209
*
211210
* DEBUG_SERIAL_ASCII output is:
212-
* // "1:345 2:1337 3:0 4:0 5:0 6:0 7:0 8:0\r\n"
211+
* // "in1:345 in2:1337 cmdL:0 cmdR:0 BatADC:0 BatV:0 TempADC:0 Temp:0\r\n"
213212
*
214-
* 1: (int16_t)input1); raw input1: ADC1, UART, PWM, PPM, iBUS
215-
* 2: (int16_t)input2); raw input2: ADC2, UART, PWM, PPM, iBUS
216-
* 3: (int16_t)speedR); output command: [-1000, 1000]
217-
* 4: (int16_t)speedL); output command: [-1000, 1000]
218-
* 5: (int16_t)adc_buffer.batt1); Battery adc-value measured by mainboard
219-
* 6: (int16_t)(batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC)); Battery calibrated voltage multiplied by 100 for verifying battery voltage calibration
220-
* 7: (int16_t)board_temp_adcFilt); for board temperature calibration
221-
* 8: (int16_t)board_temp_deg_c); Temperature in celcius for verifying board temperature calibration
213+
* in1: (int16_t)input1); raw input1: ADC1, UART, PWM, PPM, iBUS
214+
* in2: (int16_t)input2); raw input2: ADC2, UART, PWM, PPM, iBUS
215+
* cmdL: (int16_t)speedL); output command: [-1000, 1000]
216+
* cmdR: (int16_t)speedR); output command: [-1000, 1000]
217+
* BatADC: (int16_t)adc_buffer.batt1); Battery adc-value measured by mainboard
218+
* BatV: (int16_t)(batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC)); Battery calibrated voltage multiplied by 100 for verifying battery voltage calibration
219+
* TempADC: (int16_t)board_temp_adcFilt); for board temperature calibration
220+
* Temp: (int16_t)board_temp_deg_c); Temperature in celcius for verifying board temperature calibration
222221
*
223222
*/
224223

225224
// #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used!
226-
#if defined(VARIANT_ADC)
227-
#define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
228-
#endif
229-
230-
#ifndef VARIANT_TRANSPOTTER
231-
//#define DEBUG_SERIAL_SERVOTERM
232-
#define DEBUG_SERIAL_ASCII
233-
#endif
225+
// #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
234226
// ########################### END OF DEBUG SERIAL ############################
235227

236228

237229

238230
// ############################### DEBUG LCD ###############################
239-
//#define DEBUG_I2C_LCD // standard 16x2 or larger text-lcd via i2c-converter on right sensor board cable
231+
// #define DEBUG_I2C_LCD // standard 16x2 or larger text-lcd via i2c-converter on right sensor board cable
240232
// ########################### END OF DEBUG LCD ############################
241233

242234

@@ -276,6 +268,8 @@
276268
#define INPUT2_MID 0 // mid ADC2-value while poti at mid-position (INPUT2_MIN - INPUT2_MAX)
277269
#define INPUT2_MAX 4095 // max ADC2-value while poti at max-position (0 - 4095)
278270
#define INPUT2_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
271+
272+
#define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
279273
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
280274
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
281275
#endif
@@ -293,13 +287,13 @@
293287
#define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino
294288
#define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
295289
// Min / Max values of each channel (use DEBUG to determine these values)
296-
#define INPUT1_TYPE 1 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
290+
#define INPUT1_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
297291
#define INPUT1_MIN -1000 // (-1000 - 0)
298292
#define INPUT1_MID 0
299293
#define INPUT1_MAX 1000 // (0 - 1000)
300294
#define INPUT1_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
301295

302-
#define INPUT2_TYPE 1 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
296+
#define INPUT2_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
303297
#define INPUT2_MIN -1000 // (-1000 - 0)
304298
#define INPUT2_MID 0
305299
#define INPUT2_MAX 1000 // (0 - 1000)

0 commit comments

Comments
 (0)