-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Variant
USART
Control type
FOC
Control mode
Torque
What can we do to make the firmware better?
Hi. I wanted to build a Onewheel. I didn't find a Onewheel mode so I used UART + MPU6050 (on arduino). Because I'm not a very good programmer in VSC. Has anyone else had this idea? Is there anything I should be careful about?
I configured the right UART because it has a 5V tolerance
`#define HOVER_SERIAL_BAUD 115200
#define SERIAL_BAUD 115200
#define START_FRAME 0xABCD
#define ANGLE_MAX 15
#include <Wire.h>
#include <MPU6050.h>
#include <KalmanFilter.h>
#include <SoftwareSerial.h>
SoftwareSerial HoverSerial(3, 2);
MPU6050 mpu;
KalmanFilter kalmanX(0.001, 0.003, 0.03);
KalmanFilter kalmanY(0.001, 0.003, 0.03);
float accPitch = 0;
float kalPitch = 0;
int kal;
bool buttonState;
bool enable;
typedef struct {
uint16_t start;
int16_t steer;
int16_t speed;
uint16_t checksum;
}
SerialCommand;
SerialCommand Command;
void setup() {
Serial.begin(SERIAL_BAUD);
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) {
delay(500);
}
HoverSerial.begin(HOVER_SERIAL_BAUD);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(10, INPUT);
}
void Send(int16_t uSteer, int16_t uSpeed) {
Command.start = (uint16_t)START_FRAME;
Command.steer = (int16_t)uSteer;
Command.speed = (int16_t)uSpeed;
Command.checksum = (uint16_t)(Command.start ^ Command.steer ^ Command.speed);
HoverSerial.write((uint8_t *)&Command, sizeof(Command));
}
void loop(void) {
Vector acc = mpu.readNormalizeAccel();
Vector gyr = mpu.readNormalizeGyro();
accPitch = -(atan2(acc.XAxis, sqrt(acc.YAxis * acc.YAxis + acc.ZAxis * acc.ZAxis)) * 180.0) / M_PI;
kalPitch = kalmanY.update(accPitch, gyr.YAxis);
Serial.print(" angle: ");
Serial.print(kalPitch);
if (kalPitch >= ANGLE_MAX) kalPitch = ANGLE_MAX;
if (kalPitch <= -ANGLE_MAX) kalPitch = -ANGLE_MAX;
kal = kalPitch * 5;
Serial.print(" speed: ");
Serial.print(kal);
buttonState = digitalRead(10);
if (buttonState == LOW) {
digitalWrite(LED_BUILTIN, HIGH);
if ((kalPitch < 1) && (kalPitch > -1)) enable = 1;
if (enable == 1) {
Serial.print(" Motor ");
Send(0, kalPitch);
}
} else {
digitalWrite(LED_BUILTIN, LOW);
enable = 0;
}
Serial.println();
}`
Pin 10 is footswitch. The engine will only start after the board is level. The maximum angle of the board is 15'. The "mnoznik" is a (angle * mnoznik) = speed. Many values are still to be tuned during testing.
Describe suggestions or alternatives you have considered
I'm waiting for constructive criticism.