clean up code"

abrazik
JbLb 4 years ago
parent d836b77b09
commit 275af8599b

@ -34,7 +34,7 @@
#define BTN_PLUS 4 #define BTN_PLUS 4
#define BTN_MOINS 5 #define BTN_MOINS 5
#define BTN_MARCHE 6 #define BTN_MARCHE 6
#define MAX_SPEED 400 #define MAX_SPEED 420
// ########################## DEFINES ########################## // ########################## DEFINES ##########################
@ -42,7 +42,7 @@
#define SERIAL_BAUD 115200 // [-] Baud rate for built-in Serial (used for the Serial Monitor) #define SERIAL_BAUD 115200 // [-] Baud rate for built-in Serial (used for the Serial Monitor)
#define START_FRAME 0xABCD // [-] Start frme definition for reliable serial communication #define START_FRAME 0xABCD // [-] Start frme definition for reliable serial communication
#define TIME_SEND 25 // [ms] Sending time interval #define TIME_SEND 25 // [ms] Sending time interval
#define SPEED_MAX_TEST 300 // [-] Maximum speed for testing #define SPEED_MAX_TEST 490 // [-] 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)
// ########################## macros ############################ // ########################## macros ############################
@ -51,12 +51,6 @@
#include <FastLED.h> #include <FastLED.h>
// #include <SoftwareSerial.h>
// SoftwareSerial HoverSerial(2,3); // RX, TX
// Global variables // Global variables
uint8_t idx = 0; // Index for new data pointer uint8_t idx = 0; // Index for new data pointer
uint16_t bufStartFrame; // Buffer Start Frame uint16_t bufStartFrame; // Buffer Start Frame
@ -86,11 +80,6 @@ typedef struct {
SerialFeedback Feedback; SerialFeedback Feedback;
SerialFeedback NewFeedback; SerialFeedback NewFeedback;
int cmd1; // normalized input values. -1000 to 1000
int cmd2;
int motor_test_direction = 1;
int accel_speed =10; int accel_speed =10;
int eeprom_address = 0; int eeprom_address = 0;
@ -154,17 +143,18 @@ void Send(int16_t uSteer, int16_t uSpeed)
// Write to Serial // Write to Serial
Serial1.write((uint8_t *) & Command, sizeof(Command)); Serial1.write((uint8_t *) & Command, sizeof(Command));
// Serial.println(uSpeed);
} }
// ########################## 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 (Serial1.available()) { if (Serial1.available()) {
incomingByte = Serial1.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)(incomingByte) << 8) | incomingBytePrev; // Construct the start frame
} else { }
else {
return; return;
} }
@ -184,50 +174,39 @@ void Receive()
*p++ = incomingByte; *p++ = incomingByte;
idx++; idx++;
} }
// Check if we reached the end of the package // Check if we reached the end of the package
if (idx == sizeof(SerialFeedback)) { if (idx == sizeof(SerialFeedback)) {
uint16_t checksum; uint16_t checksum;
checksum = checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas
(uint16_t) (NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas
^ NewFeedback.batVoltage ^ NewFeedback.boardTemp ^ NewFeedback.cmdLed); ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp ^ NewFeedback.cmdLed);
// Check validity of the new data // Check validity of the new data
if (NewFeedback.start == START_FRAME if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) {
&& checksum == NewFeedback.checksum) {
// Copy the new data // Copy the new data
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback)); memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback));
// Print data to CDC Serial if available // Print data to built-in Serial
if (Serial) { if (Serial) {
Serial.print("1: "); Serial.print("1: "); Serial.print(Feedback.cmd1);
Serial.print(Feedback.cmd1); Serial.print(" 2: "); Serial.print(Feedback.cmd2);
Serial.print(" 2: "); Serial.print(" 3: "); Serial.print(Feedback.speedR_meas);
Serial.print(Feedback.cmd2); Serial.print(" 4: "); Serial.print(Feedback.speedL_meas);
Serial.print(" 3: "); Serial.print(" 5: "); Serial.print(Feedback.batVoltage);
Serial.print(Feedback.speedR_meas); Serial.print(" 6: "); Serial.print(Feedback.boardTemp);
Serial.print(" 4: "); Serial.print(" 7: "); Serial.println(Feedback.cmdLed);
Serial.print(Feedback.speedL_meas);
Serial.print(" 5: ");
Serial.print(Feedback.batVoltage);
Serial.print(" 6: ");
Serial.print(Feedback.boardTemp);
Serial.print(" 7: ");
Serial.println(Feedback.cmdLed);
} }
} else { } else {
if (Serial) if (Serial)
Serial.println("Non-valid data skipped"); 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)
} }
// Update previous states // Update previous states
incomingBytePrev = incomingByte; incomingBytePrev = incomingByte;
} }
// ############################# // #############################
//This function will write a 2 byte integer to the eeprom at the specified address and address + 1 //This function will write a 2 byte integer to the eeprom at the specified address and address + 1
void EEPROMWriteInt(int p_address, int p_value) void EEPROMWriteInt(int p_address, int p_value)

Loading…
Cancel
Save