UART with mainboard works

- the sideboard can now send and receive Serial data from the mainboard
- fixed Processing sketch
This commit is contained in:
EmanuelFeru 2020-03-01 09:43:14 +01:00
parent e21f61ec2e
commit 5d9e79afaf
13 changed files with 105 additions and 97 deletions

2
.gitignore vendored
View File

@ -2,7 +2,7 @@
.pioenvs/ .pioenvs/
.vscode/ .vscode/
MDK-ARM/DebugConfig/ MDK-ARM/DebugConfig/
MDK-ARM/Listing/ MDK-ARM/Listings/
MDK-ARM/Objects/ MDK-ARM/Objects/
MDK-ARM/RTE/ MDK-ARM/RTE/
MDK-ARM/*.uvguix.* MDK-ARM/*.uvguix.*

View File

@ -72,8 +72,8 @@
#ifdef BYPASS_CUBEMX_DEFINES #ifdef BYPASS_CUBEMX_DEFINES
#define USART_MAIN_BAUD 38400 // [bit/s] MAIN Serial Tx/Rx baud rate #define USART_MAIN_BAUD 38400 // [bit/s] MAIN Serial Tx/Rx baud rate
#endif #endif
#define SERIAL_START_FRAME 0xAAAA // [-] Start frame definition for reliable serial communication #define SERIAL_START_FRAME 0xABCD // [-] Start frame definition for reliable serial communication
#define SERIAL_TIMEOUT 300 // [-] Numer of wrong received data for Serial timeout detection #define SERIAL_TIMEOUT 500 // [-] Numer of wrong received data for Serial timeout detection
/* ==================================== VALIDATE SETTINGS ==================================== */ /* ==================================== VALIDATE SETTINGS ==================================== */

View File

@ -90,6 +90,7 @@
/* =========================== Defines MPU-6050 =========================== */ /* =========================== Defines MPU-6050 =========================== */
#define log_i printf // redirect the log_i debug function to printf #define log_i printf // redirect the log_i debug function to printf
#define RAD2DEG 57.295779513082323 // RAD2DEG = 180/pi. Example: angle[deg] = angle[rad] * RAD2DEG #define RAD2DEG 57.295779513082323 // RAD2DEG = 180/pi. Example: angle[deg] = angle[rad] * RAD2DEG
#define q30 1073741824 // 1073741824 = 2^30
#define ACCEL_ON (0x01) #define ACCEL_ON (0x01)
#define GYRO_ON (0x02) #define GYRO_ON (0x02)
#define COMPASS_ON (0x04) #define COMPASS_ON (0x04)

View File

@ -75,7 +75,7 @@
<OPTFL> <OPTFL>
<tvExp>1</tvExp> <tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<IsCurrentTarget>1</IsCurrentTarget> <IsCurrentTarget>0</IsCurrentTarget>
</OPTFL> </OPTFL>
<CpuCode>18</CpuCode> <CpuCode>18</CpuCode>
<DebugOpt> <DebugOpt>
@ -233,7 +233,7 @@
<OPTFL> <OPTFL>
<tvExp>1</tvExp> <tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<IsCurrentTarget>0</IsCurrentTarget> <IsCurrentTarget>1</IsCurrentTarget>
</OPTFL> </OPTFL>
<CpuCode>18</CpuCode> <CpuCode>18</CpuCode>
<DebugOpt> <DebugOpt>

View File

@ -74,14 +74,14 @@ make flash
This firmware offers currently these variants (selectable in [platformio.ini](/platformio.ini) or [config.h](/Inc/config.h)): This firmware offers currently these variants (selectable in [platformio.ini](/platformio.ini) or [config.h](/Inc/config.h)):
- **VARIANT_DEBUG**: In this variant the user can interact with sideboard by sending commands via a Serial Monitor to observe and check the capabilities of the sideboard. - **VARIANT_DEBUG**: In this variant the user can interact with sideboard by sending commands via a Serial Monitor to observe and check the capabilities of the sideboard.
- **VARIANT_HOVERBOARD**: In this variant the sideboard is communicating with the mainboard of a hoverboard using the [FOC firmware repository](https://github.com/EmanuelFeru/hoverboard-firmware-hack-FOC). This Variant is not yet fully tested! - **VARIANT_HOVERBOARD**: In this variant the sideboard is communicating with the mainboard of a hoverboard using the [FOC firmware repository](https://github.com/EmanuelFeru/hoverboard-firmware-hack-FOC).
Of course the firmware can be further customized for other needs or projects. Of course the firmware can be further customized for other needs or projects.
--- ---
## 3D Visualization Demo ## 3D Visualization Demo
By [converting Quaternions to Euler angles](https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles), we can make a [3D visualization example](/docs/sketch_processing/sketch_processing.pde) in [Processing](https://processing.org/) as shown below. For this Demo VARIANT_HOVERBOARD was used. By [converting Quaternions to Euler angles](https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles), we can make a [3D visualization example](/docs/sketch_processing/sketch_processing.pde) in [Processing](https://processing.org/) as shown below. For this Demo VARIANT_DEBUG was used.
![sketch_pic](/docs/pictures/sketch_processing_pic.png) ![sketch_pic](/docs/pictures/sketch_processing_pic.png)

View File

@ -100,8 +100,6 @@ typedef struct{
uint16_t start; uint16_t start;
int16_t cmd1; int16_t cmd1;
int16_t cmd2; int16_t cmd2;
int16_t speedR;
int16_t speedL;
int16_t speedR_meas; int16_t speedR_meas;
int16_t speedL_meas; int16_t speedL_meas;
int16_t batVoltage; int16_t batVoltage;
@ -117,10 +115,10 @@ static uint8_t timeoutFlagSerial = 0; // Timeout Flag for Rx Serial comman
#endif #endif
extern MPU_Data mpu; // holds the MPU-6050 data extern MPU_Data mpu; // holds the MPU-6050 data
ErrorStatus mpuStatus = SUCCESS; // holds the MPU-6050 status: SUCCESS or ERROR ErrorStatus mpuStatus; // holds the MPU-6050 status: SUCCESS or ERROR
FlagStatus sensor1, sensor2; // holds the sensor1 and sensor 2 values GPIO_PinState sensor1, sensor2; // holds the sensor1 and sensor 2 values
FlagStatus sensor1_read, sensor2_read; // holds the instantaneous Read for sensor1 and sensor 2 GPIO_PinState sensor1_read, sensor2_read; // holds the instantaneous Read for sensor1 and sensor 2
static uint32_t main_loop_counter; // main loop counter to perform task squeduling inside main() static uint32_t main_loop_counter; // main loop counter to perform task squeduling inside main()
/* USER CODE END 0 */ /* USER CODE END 0 */
@ -177,6 +175,7 @@ int main(void)
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); // Turn on RED LED HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); // Turn on RED LED
} }
else { else {
mpuStatus = SUCCESS;
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); // Turn on GREEN LED HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); // Turn on GREEN LED
} }
mpu_handle_input('h'); // Print the User Help commands to serial mpu_handle_input('h'); // Print the User Help commands to serial
@ -216,6 +215,8 @@ int main(void)
// Get MPU data. Because the MPU-6050 interrupt pin is not wired we have to check DMP data by pooling periodically // Get MPU data. Because the MPU-6050 interrupt pin is not wired we have to check DMP data by pooling periodically
if (SUCCESS == mpuStatus) { if (SUCCESS == mpuStatus) {
mpu_get_data(); mpu_get_data();
} else if (ERROR == mpuStatus && main_loop_counter % 100 == 0) {
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); // Toggle the Red LED every 100 ms
} }
// Print MPU data to Console // Print MPU data to Console
if (main_loop_counter % 50 == 0) { if (main_loop_counter % 50 == 0) {
@ -228,43 +229,43 @@ int main(void)
sensor2_read = HAL_GPIO_ReadPin(SENSOR2_GPIO_Port, SENSOR2_Pin); sensor2_read = HAL_GPIO_ReadPin(SENSOR2_GPIO_Port, SENSOR2_Pin);
// SENSOR1 // SENSOR1
if (sensor1 == RESET && sensor1_read == SET) { if (sensor1 == GPIO_PIN_RESET && sensor1_read == GPIO_PIN_SET) {
sensor1 = SET; sensor1 = GPIO_PIN_SET;
// Sensor ACTIVE: Do something here (one time task on activation) // Sensor ACTIVE: Do something here (one time task on activation)
HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET);
consoleLog("-- SENSOR 1 Active --\n"); consoleLog("-- SENSOR 1 Active --\n");
} else if(sensor1 == SET && sensor1_read == RESET) { } else if(sensor1 == GPIO_PIN_SET && sensor1_read == GPIO_PIN_RESET) {
sensor1 = RESET; sensor1 = GPIO_PIN_RESET;
HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET);
} }
// SENSOR2 // SENSOR2
if (sensor2 == RESET && sensor2_read == SET) { if (sensor2 == GPIO_PIN_RESET && sensor2_read == GPIO_PIN_SET) {
sensor2 = SET; sensor2 = GPIO_PIN_SET;
// Sensor ACTIVE: Do something here (one time task on activation) // Sensor ACTIVE: Do something here (one time task on activation)
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_SET);
consoleLog("-- SENSOR 2 Active --\n"); consoleLog("-- SENSOR 2 Active --\n");
} else if (sensor2 == SET && sensor2_read == RESET) { } else if (sensor2 == GPIO_PIN_SET && sensor2_read == GPIO_PIN_RESET) {
sensor2 = RESET; sensor2 = GPIO_PIN_RESET;
HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET);
} }
if (sensor1 == SET) { if (sensor1 == GPIO_PIN_SET) {
// Sensor ACTIVE: Do something here (continuous task) // Sensor ACTIVE: Do something here (continuous task)
} }
if (sensor2 == SET) { if (sensor2 == GPIO_PIN_SET) {
// Sensor ACTIVE: Do something here (continuous task) // Sensor ACTIVE: Do something here (continuous task)
} }
// ==================================== SERIAL Tx/Rx Handling ==================================== // ==================================== SERIAL Tx/Rx Handling ====================================
#ifdef SERIAL_CONTROL #ifdef SERIAL_CONTROL
if (main_loop_counter % 50 == 0) { // Transmit Tx data periodically using DMA if (main_loop_counter % 5 == 0) { // Transmit Tx data periodically using DMA
Sideboard.start = (uint16_t)SERIAL_START_FRAME; Sideboard.start = (uint16_t)SERIAL_START_FRAME;
Sideboard.roll = (int16_t)mpu.euler.roll; Sideboard.roll = (int16_t)mpu.euler.roll;
Sideboard.pitch = (int16_t)mpu.euler.pitch; Sideboard.pitch = (int16_t)mpu.euler.pitch;
Sideboard.yaw = (int16_t)mpu.euler.yaw; Sideboard.yaw = (int16_t)mpu.euler.yaw;
Sideboard.sensors = (uint16_t)(sensor1 | (sensor2 << 1)); Sideboard.sensors = (uint16_t)(sensor1 | (sensor2 << 1) | (mpuStatus << 2));
Sideboard.checksum = (uint16_t)(Sideboard.start ^ Sideboard.roll ^ Sideboard.pitch ^ Sideboard.yaw ^ Sideboard.sensors); Sideboard.checksum = (uint16_t)(Sideboard.start ^ Sideboard.roll ^ Sideboard.pitch ^ Sideboard.yaw ^ Sideboard.sensors);
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)&Sideboard, sizeof(Sideboard)); HAL_UART_Transmit_DMA(&huart2, (uint8_t *)&Sideboard, sizeof(Sideboard));
@ -273,14 +274,14 @@ int main(void)
#ifdef SERIAL_FEEDBACK #ifdef SERIAL_FEEDBACK
uint16_t checksum; uint16_t checksum;
checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR ^ NewFeedback.speedL checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas
^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp ^ NewFeedback.cmdLed); ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp ^ NewFeedback.cmdLed);
if (NewFeedback.start == SERIAL_START_FRAME && NewFeedback.checksum == checksum) { if (NewFeedback.start == SERIAL_START_FRAME && NewFeedback.checksum == checksum) {
if (timeoutFlagSerial) { // Check for previous timeout flag if (timeoutFlagSerial) { // Check for previous timeout flag
if (timeoutCntSerial-- <= 0) // Timeout de-qualification if (timeoutCntSerial-- <= 0) // Timeout de-qualification
timeoutFlagSerial = 0; // Timeout flag cleared timeoutFlagSerial = 0; // Timeout flag cleared
} else { } else {
memcpy(&Feedback, &NewFeedback, sizeof(SerialFeedback)); // Copy the new data memcpy(&Feedback, &NewFeedback, sizeof(Feedback)); // Copy the new data
NewFeedback.start = 0xFFFF; // Change the Start Frame for timeout detection in the next cycle NewFeedback.start = 0xFFFF; // Change the Start Frame for timeout detection in the next cycle
timeoutCntSerial = 0; // Reset the timeout counter timeoutCntSerial = 0; // Reset the timeout counter
} }
@ -289,18 +290,15 @@ int main(void)
timeoutFlagSerial = 1; // Timeout detected timeoutFlagSerial = 1; // Timeout detected
timeoutCntSerial = SERIAL_TIMEOUT; // Limit timout counter value timeoutCntSerial = SERIAL_TIMEOUT; // Limit timout counter value
} }
// Check periodically the received Start Frame. If it is NOT OK, most probably we are out-of-sync. Try to re-sync by reseting the DMA // Most probably we are out-of-sync. Try to re-sync by reseting the DMA
if (main_loop_counter % 50 == 0 && NewFeedback.start != SERIAL_START_FRAME && NewFeedback.start != 0xFFFF) { if (main_loop_counter % 150 == 0) {
HAL_UART_DMAStop(&huart2); HAL_UART_DMAStop(&huart2);
HAL_UART_Receive_DMA(&huart2, (uint8_t *)&NewFeedback, sizeof(NewFeedback)); HAL_UART_Receive_DMA(&huart2, (uint8_t *)&NewFeedback, sizeof(NewFeedback));
NewFeedback.start = 0xFFFF; // Change the Start Frame to avoid entering again here if no data is received
} }
} }
if (timeoutFlagSerial) { // In case of timeout bring the system to a Safe State and indicate error if desired if (timeoutFlagSerial && main_loop_counter % 100 == 0) { // In case of timeout bring the system to a Safe State and indicate error if desired
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET); // Turn on Red LED HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin); // Toggle the Yellow LED every 100 ms
} else {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); // Follow the Normal behavior
} }
#endif #endif

View File

@ -3230,9 +3230,7 @@ void mpu_start_self_test(void)
#elif defined (MPU6050) || defined (MPU9150) #elif defined (MPU6050) || defined (MPU9150)
result = mpu_run_self_test(gyro, accel); result = mpu_run_self_test(gyro, accel);
#endif #endif
if (result == 0x7) {
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
consoleLog("Passed!\n");
log_i("accel: %ld %ld %ld\n", log_i("accel: %ld %ld %ld\n",
accel[0], accel[0],
accel[1], accel[1],
@ -3241,8 +3239,10 @@ void mpu_start_self_test(void)
gyro[0], gyro[0],
gyro[1], gyro[1],
gyro[2]); gyro[2]);
/* Test passed. We can trust the gyro data here, so now we need to update calibrated data*/
#endif #endif
if (result == 0x7) {
consoleLog("Passed!\n");
/* Test passed. We can trust the gyro data here, so now we need to update calibrated data*/
#ifdef USE_CAL_HW_REGISTERS #ifdef USE_CAL_HW_REGISTERS
/* /*
@ -3639,10 +3639,10 @@ void mpu_calc_euler_angles(void) {
float yaw, pitch, roll; float yaw, pitch, roll;
// Convert quaternions[q30] to quaternion[float] // Convert quaternions[q30] to quaternion[float]
w = (float)mpu.quat.w / 1073741824; // 1073741824 = 2^30 w = (float)mpu.quat.w / q30; // q30 = 2^30
x = (float)mpu.quat.x / 1073741824; x = (float)mpu.quat.x / q30;
y = (float)mpu.quat.y / 1073741824; y = (float)mpu.quat.y / q30;
z = (float)mpu.quat.z / 1073741824; z = (float)mpu.quat.z / q30;
// Calculate Euler angles: source <https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles> // Calculate Euler angles: source <https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles>
roll = atan2(2*(w*x + y*z), 1 - 2*(x*x + y*y)); // roll (x-axis rotation) roll = atan2(2*(w*x + y*z), 1 - 2*(x*x + y*y)); // roll (x-axis rotation)

View File

@ -69,7 +69,7 @@ void intro_demo_led(uint32_t tDelay)
{ {
int i; int i;
for (i = 0; i < 6; i++) { for (i = 0; i < 3; i++) {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);
HAL_Delay(tDelay); HAL_Delay(tDelay);

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,5 @@
/* /*
hoverboard-sidebboard-hack MPU6050 IMU - 3D Visualization Example hoverboard-sidebboard-hack MPU6050 IMU - 3D Visualization Example. Use with VARIANT_DEBUG.
Copyright (C) 2020-2021 Emanuel FERU Copyright (C) 2020-2021 Emanuel FERU
*/ */
import processing.serial.*; import processing.serial.*;
@ -9,40 +9,32 @@ import java.io.IOException;
Serial myPort; Serial myPort;
float roll, pitch,yaw; float roll, pitch,yaw;
int idx = 0; int idx = 0;
int inBytePrev;
short bufWord; String data="";
String check="";
void setup() { void setup() {
size (1400, 800, P3D); size (1400, 800, P3D);
printArray(Serial.list()); // List all the available serial ports printArray(Serial.list()); // List all the available serial ports
myPort = new Serial(this, "COM5", 38400); // starts the serial communication myPort = new Serial(this, "COM5", 38400); // starts the serial communication
myPort.bufferUntil('\n');
} }
void draw() { void draw() {
while (myPort.available() > 0) { // If no data is received, send 'e' command to read the Euler angles
int inByte = myPort.read(); if(idx != -1 && myPort.available() == 0) {
bufWord = (short)(inBytePrev | (inByte << 8));
idx++; idx++;
if(bufWord == -21846) { // check START_FRAME = 0xAAAA if(idx > 20) {
idx = 0; myPort.write('e');
idx = -1;
} }
if (idx == 2) { } else {
roll = float(bufWord) / 100; idx = -1;
}
if (idx == 4) {
pitch = float(bufWord) / 100;
}
if (idx == 6) {
yaw = float(bufWord) / 100;
}
inBytePrev = inByte;
} }
// println(bufWord); //<>// // Display text
translate(width/2, height/2, 0); //<>//
translate(width/2, height/2, 0);
background(51); background(51);
textSize(22); textSize(22);
text("Roll: " + roll + " Pitch: " + pitch + " Yaw: " + yaw, -200, 300); text("Roll: " + roll + " Pitch: " + pitch + " Yaw: " + yaw, -200, 300);
@ -53,7 +45,6 @@ void draw() {
rotateY(radians(yaw)); rotateY(radians(yaw));
// 3D 0bject // 3D 0bject
// Draw box with text // Draw box with text
fill(0, 76, 153);; // Make board BLUE fill(0, 76, 153);; // Make board BLUE
box (426, 30, 220); box (426, 30, 220);
@ -80,3 +71,21 @@ void draw() {
box (40, 40, 15); // Blue Led connector box (40, 40, 15); // Blue Led connector
} }
// Read data from the Serial Port
void serialEvent (Serial myPort) {
// reads the data from the Serial Port up to the character '\n' and puts it into the String variable "data".
data = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (data != null) {
data = trim(data);
// split the string at " " (character space)
String items[] = split(data, ' ');
if (items.length > 5) {
//--- Roll,Pitch in degrees
roll = float(items[2]) / 100;
pitch = float(items[4]) / 100;
yaw = float(items[6]) / 100;
}
}
}