|
|
|
@ -14,7 +14,9 @@
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// by JbLb
|
|
|
|
// by JbLb
|
|
|
|
// • Converted to platformio
|
|
|
|
// • Converted to platformio
|
|
|
|
// • Adapted to Arduino leonardo to use CDC serail interface
|
|
|
|
// • Adapted to Arduino leonardo to use CDC serial interface
|
|
|
|
|
|
|
|
// • Serial1 connected to hoverboard board
|
|
|
|
|
|
|
|
// • CDC used to display data on serial monitor
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// CONFIGURATION on the hoverboard side in config.h:
|
|
|
|
// CONFIGURATION on the hoverboard side in config.h:
|
|
|
|
@ -36,13 +38,11 @@
|
|
|
|
#define SPEED_MAX_TEST 300 // [-] Maximum speed for testing
|
|
|
|
#define SPEED_MAX_TEST 300 // [-] Maximum speed for testing
|
|
|
|
//#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable)
|
|
|
|
//#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable)
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef AVR_LEONARDO
|
|
|
|
// #include <SoftwareSerial.h>
|
|
|
|
#include <SoftwareSerial.h>
|
|
|
|
// SoftwareSerial HoverSerial(2,3); // RX, TX
|
|
|
|
SoftwareSerial HoverSerial(2,3); // RX, TX
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Global variables
|
|
|
|
// Global variables
|
|
|
|
uint8_t idx = 0; // Index for new data pointer
|
|
|
|
uint8_t 1 idx = 0; // Index for new data pointer
|
|
|
|
uint16_t bufStartFrame; // Buffer Start Frame
|
|
|
|
uint16_t bufStartFrame; // Buffer Start Frame
|
|
|
|
byte *p; // Pointer declaration for the new received data
|
|
|
|
byte *p; // Pointer declaration for the new received data
|
|
|
|
byte incomingByte;
|
|
|
|
byte incomingByte;
|
|
|
|
@ -75,9 +75,11 @@ SerialFeedback NewFeedback;
|
|
|
|
void setup()
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Serial.begin(SERIAL_BAUD);
|
|
|
|
Serial.begin(SERIAL_BAUD);
|
|
|
|
Serial.println("Hoverboard Serial v1.0");
|
|
|
|
if (Serial) {
|
|
|
|
|
|
|
|
Serial.println("Hoverboard Serial v1.0");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HoverSerial.begin(HOVER_SERIAL_BAUD);
|
|
|
|
Serial1.begin(HOVER_SERIAL_BAUD);
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -91,15 +93,15 @@ void Send(int16_t uSteer, int16_t uSpeed)
|
|
|
|
Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed);
|
|
|
|
Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed);
|
|
|
|
|
|
|
|
|
|
|
|
// Write to Serial
|
|
|
|
// Write to Serial
|
|
|
|
HoverSerial.write((uint8_t *) &Command, sizeof(Command));
|
|
|
|
Serial1.write((uint8_t *) &Command, sizeof(Command));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ########################## RECEIVE ##########################
|
|
|
|
// ########################## RECEIVE ##########################
|
|
|
|
void Receive()
|
|
|
|
void Receive()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Check for new data availability in the Serial buffer
|
|
|
|
// Check for new data availability in the Serial buffer
|
|
|
|
if (HoverSerial.available()) {
|
|
|
|
if (Serial1.available()) {
|
|
|
|
incomingByte = HoverSerial.read(); // Read the incoming byte
|
|
|
|
incomingByte = Serial1.read(); // Read the incoming byte
|
|
|
|
bufStartFrame = ((uint16_t)(incomingBytePrev) << 8) + incomingByte; // Construct the start frame
|
|
|
|
bufStartFrame = ((uint16_t)(incomingBytePrev) << 8) + incomingByte; // Construct the start frame
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
@ -110,7 +112,7 @@ void Receive()
|
|
|
|
#ifdef DEBUG_RX
|
|
|
|
#ifdef DEBUG_RX
|
|
|
|
Serial.print(incomingByte);
|
|
|
|
Serial.print(incomingByte);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Copy received data
|
|
|
|
// Copy received data
|
|
|
|
if (bufStartFrame == START_FRAME) { // Initialize if new data is detected
|
|
|
|
if (bufStartFrame == START_FRAME) { // Initialize if new data is detected
|
|
|
|
@ -134,18 +136,21 @@ void Receive()
|
|
|
|
// Copy the new data
|
|
|
|
// Copy the new data
|
|
|
|
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback));
|
|
|
|
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback));
|
|
|
|
|
|
|
|
|
|
|
|
// Print data to built-in Serial
|
|
|
|
// Print data to CDC Serial if available
|
|
|
|
Serial.print("1: "); Serial.print(Feedback.cmd1);
|
|
|
|
if (Serial) {
|
|
|
|
Serial.print(" 2: "); Serial.print(Feedback.cmd2);
|
|
|
|
Serial.print("1: "); Serial.print(Feedback.cmd1);
|
|
|
|
Serial.print(" 3: "); Serial.print(Feedback.speedR);
|
|
|
|
Serial.print(" 2: "); Serial.print(Feedback.cmd2);
|
|
|
|
Serial.print(" 4: "); Serial.print(Feedback.speedL);
|
|
|
|
Serial.print(" 3: "); Serial.print(Feedback.speedR);
|
|
|
|
Serial.print(" 5: "); Serial.print(Feedback.speedR_meas);
|
|
|
|
Serial.print(" 4: "); Serial.print(Feedback.speedL);
|
|
|
|
Serial.print(" 6: "); Serial.print(Feedback.speedL_meas);
|
|
|
|
Serial.print(" 5: "); Serial.print(Feedback.speedR_meas);
|
|
|
|
Serial.print(" 7: "); Serial.print(Feedback.batVoltage);
|
|
|
|
Serial.print(" 6: "); Serial.print(Feedback.speedL_meas);
|
|
|
|
Serial.print(" 8: "); Serial.println(Feedback.boardTemp);
|
|
|
|
Serial.print(" 7: "); Serial.print(Feedback.batVoltage);
|
|
|
|
|
|
|
|
Serial.print(" 8: "); Serial.println(Feedback.boardTemp);
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Serial.println("Non-valid data skipped");
|
|
|
|
if (Serial) Serial.println("Non-valid data skipped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle)
|
|
|
|
idx = 0; // Reset the index (it prevents to enter in this if condition in the next cycle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|