Browse Source

optimum resolution for current serialfeedback

master
interfisch 7 months ago
parent
commit
b9bf849330
3 changed files with 29 additions and 20 deletions
  1. +19
    -12
      02_Arduino/controller_stm_PotiTest_NewFoc/controller_stm_PotiTest_NewFoc.ino
  2. +3
    -3
      Inc/config.h
  3. +7
    -5
      Src/main.c

+ 19
- 12
02_Arduino/controller_stm_PotiTest_NewFoc/controller_stm_PotiTest_NewFoc.ino View File

@ -36,6 +36,8 @@
#define SERIAL_BAUD 115200 // [-] Baud rate for built-in Serial (used for the Serial Monitor)
#define START_FRAME 0xAAAA // [-] Start frme definition for reliable serial communication
#define A2BIT_CONV 50
//#define DEBUG_RX // [-] Debug received data. Prints all bytes to serial (comment-out to disable)
#define MAXADCVALUE 4095
@ -76,7 +78,7 @@ typedef struct{
int16_t boardTemp;
int16_t curL_DC;
int16_t curR_DC;
int16_t checksum;
uint16_t checksum;
} SerialFeedback;
SerialFeedback Feedback;
SerialFeedback NewFeedback;
@ -87,10 +89,9 @@ void setup()
Serial.begin(115200); //Debug and Program. A9=TX1, A10=RX1 (3v3 level)
Serial2.begin(19200); //control. B10=TX3, B11=RX3 (Serial2 is Usart 3)
Serial1.begin(19200); //control. A2=TX2, A3=RX2 (Serial1 is Usart 2)
Serial2.begin(SERIAL_CONTROL_BAUD); //control. B10=TX3, B11=RX3 (Serial2 is Usart 3)
Serial1.begin(SERIAL_CONTROL_BAUD); //control. A2=TX2, A3=RX2 (Serial1 is Usart 2)
Serial1.begin(SERIAL_CONTROL_BAUD);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Initialized");
@ -141,8 +142,8 @@ void ReceiveSerial1()
// Check if we reached the end of the package
if (idx == sizeof(SerialFeedback)) {
uint16_t checksum;
checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedR ^ NewFeedback.speedL
^ NewFeedback.speedR_meas ^ NewFeedback.speedL_meas ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp ^ NewFeedback.curL_DC ^ NewFeedback.curR_DC);
checksum = (uint16_t)(NewFeedback.start ^ NewFeedback.cmd1 ^ NewFeedback.cmd2 ^ NewFeedback.speedL ^ NewFeedback.speedR
^ NewFeedback.speedL_meas^ NewFeedback.speedR_meas ^ NewFeedback.batVoltage ^ NewFeedback.boardTemp ^ NewFeedback.curL_DC ^ NewFeedback.curR_DC);
// Check validity of the new data
if (NewFeedback.start == START_FRAME && checksum == NewFeedback.checksum) {
@ -152,16 +153,22 @@ void ReceiveSerial1()
Serial.print(millis()-last_validReceive); Serial.print("ms ");
// Print data to built-in Serial
Serial.print("1: "); Serial.print(Feedback.cmd1);
m">/*Serial.print("1: "); Serial.print(Feedback.cmd1);
Serial.print(" 2: "); Serial.print(Feedback.cmd2);
Serial.print(" 3: "); Serial.print(Feedback.speedR);
Serial.print(" 4: "); Serial.print(Feedback.speedL);
Serial.print(" 5: "); Serial.print(Feedback.speedR_meas);
Serial.print(" 6: "); Serial.print(Feedback.speedL_meas);
Serial.print(" 3: "); Serial.print(Feedback.speedL);
Serial.print(" 4: "); Serial.print(Feedback.speedR);
Serial.print(" 5: "); Serial.print(Feedback.speedL_meas);
Serial.print(" 6: "); Serial.print(Feedback.speedR_meas);
Serial.print(" 7: "); Serial.print(Feedback.batVoltage);
Serial.print(" 8: "); Serial.print(Feedback.boardTemp);
Serial.print(" 9: "); Serial.print(Feedback.curL_DC); //in mA (100mA resolution), in hoverbrett negative sign for forward
Serial.print(" 10: "); Serial.println(Feedback.curR_DC); //in mA (100mA resolution), in hoverbrett negative sign for forward
*/
Serial.print(" LS: "); Serial.print(Feedback.speedL_meas);
Serial.print(" RS: "); Serial.print(Feedback.speedR_meas);
Serial.print(" LC: "); Serial.print(Feedback.curL_DC*1.0/A2BIT_CONV); //in A, in hoverbrett negative sign for forward
Serial.print(" RC: "); Serial.println(Feedback.curR_DC*1.0/A2BIT_CONV); //in A, in hoverbrett negative sign for forward
last_validReceive=millis();
} else {
@ -188,7 +195,7 @@ void loop() {
int16_t speedvalue=constrain(analogRead(PIN_POTI)*1.0/MAXADCVALUE*1000, 0, 1000);
SendSerial1(speedvalue,speedvalue);
Serial.print("Send "); Serial.print(millis()); Serial.print("ms, steer=0"); Serial.print(", speed="); Serial.println(speedvalue);
1">//Serial.print("Send "); Serial.print(millis()); Serial.print("ms, steer=0"); Serial.print(", speed="); Serial.println(speedvalue);
last_send = millis();

+ 3
- 3
Inc/config.h View File

@ -2,13 +2,13 @@
#include "stm32f1xx_hal.h"
//Select the calibration values for the different pcbs
#define BOBBYCAR_PCB_FRONT //Green PCB. black programming cable. outer power button
//#define BOBBYCAR_PCB_FRONT //Green PCB. black programming cable. outer power button
//#define BOBBYCAR_PCB_REAR //Blue PCB. with XT60-U, beige programming cable. inner power button
//#define HOVERBRETT //Green PCB in Hoverbrett
#define HOVERBRETT //Green PCB in Hoverbrett
// ############################### DO-NOT-TOUCH SETTINGS ###############################
#define N_POLEPAIRS 15 //for wheel angle calculation
//#define N_POLEPAIRS 15 //for wheel angle calculation
#define PWM_FREQ 16000 // PWM frequency in Hz
#define DEAD_TIME 32 // PWM deadtime

+ 7
- 5
Src/main.c View File

@ -89,6 +89,8 @@ typedef struct{
int16_t speedR;
int16_t speedL_meas;
int16_t speedR_meas;
//int16_t angleL_meas;
//int16_t angleR_meas;
int16_t batVoltage;
int16_t boardTemp;
int16_t curL_DC;
@ -488,13 +490,13 @@ int main(void) {
Feedback.speedL_meas = (int16_t)rtY_Left.n_mot;
Feedback.speedR_meas = (int16_t)rtY_Right.n_mot;
//Feedback.angleL_meas = (int16_t)rtY_Left.a_elecAngle/N_POLEPAIRS;
//Feedback.angleR_meas = (int16_t)rtY_Right.a_elecAngle/N_POLEPAIRS;
//Feedback.angleR_meas = (int16_t)rtY_Right.a_elecAngle/N_POLEPAIRS; //rtY_Right.a_elecAngle/N_POLEPAIRS goes from 0 to ca. 24
Feedback.batVoltage = (int16_t)(batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC);
Feedback.boardTemp = (int16_t)board_temp_deg_c;
Feedback.curL_DC = (int16_t)curL_DC*10/A2BIT_CONV*100; //in milli Amperes, 100mA resolution
Feedback.curR_DC = (int16_t)curR_DC*10/A2BIT_CONV*100;
Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR ^ Feedback.speedL
^ Feedback.speedR_meas ^ Feedback.speedL_meas ^ Feedback.batVoltage ^ Feedback.boardTemp ^ Feedback.curL_DC ^Feedback.curR_DC);
Feedback.curL_DC = (int16_t)curL_DC; //divide by A2BIT_CONV to get current in amperes
Feedback.curR_DC = (int16_t)curR_DC;
Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedL ^ Feedback.speedR
^ Feedback.speedL_meas ^ Feedback.speedR_meas ^ Feedback.batVoltage ^ Feedback.boardTemp ^ Feedback.curL_DC ^Feedback.curR_DC);
UART_DMA_CHANNEL->CCR &= ~DMA_CCR_EN;
UART_DMA_CHANNEL->CNDTR = sizeof(Feedback);

Loading…
Cancel
Save