2021-04-13 21:51:43 +02:00
|
|
|
#include <Wire.h>
|
|
|
|
|
2021-04-14 01:54:01 +02:00
|
|
|
int lastCorrectionTime = 0;
|
|
|
|
int lastPrintTime = 0;
|
|
|
|
|
2021-04-14 17:07:16 +02:00
|
|
|
static int fastTimer = 80; //ms
|
|
|
|
static int slowTimer = 800; //ms
|
2021-04-13 20:17:11 +02:00
|
|
|
|
2021-04-13 21:44:53 +02:00
|
|
|
//lqr stuff
|
|
|
|
const uint8_t statesNumber = 4;
|
|
|
|
/** Low pass filter angular Position*/
|
|
|
|
float angularPositionLP = 0;
|
|
|
|
/** Zumo's angular position */
|
|
|
|
float angularPosition = 0;
|
|
|
|
/** Corrected angular position */
|
|
|
|
float correctedAngularPosition = 0;
|
|
|
|
/** Zumo's angular speed */
|
|
|
|
float angularSpeed = 0;
|
|
|
|
/** Motor's angular position */
|
|
|
|
float motorAngularPosition = 0;
|
|
|
|
/** Motor's angular speed */
|
|
|
|
float motorAngularSpeed = 0;
|
2021-04-13 22:09:37 +02:00
|
|
|
|
|
|
|
/** PWM signal applied to the motor's driver 255 is 100% */
|
2021-04-13 21:44:53 +02:00
|
|
|
int32_t speed;
|
2021-04-13 22:09:37 +02:00
|
|
|
int safe_angle;
|
2021-04-13 21:44:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
//gyro stuff
|
2021-04-13 20:17:11 +02:00
|
|
|
float AccX, AccY, AccZ;
|
|
|
|
float GyroX, GyroY, GyroZ;
|
|
|
|
float accAngleX, accAngleY, gyroAngleX, gyroAngleY, gyroAngleZ;
|
|
|
|
float roll, pitch, yaw;
|
|
|
|
float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ;
|
|
|
|
float elapsedTime, currentTime, previousTime;
|
|
|
|
int gyro_x, gyro_y, gyro_z;
|
|
|
|
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
|
|
|
|
boolean set_gyro_angles;
|
|
|
|
|
|
|
|
long acc_x, acc_y, acc_z, acc_total_vector;
|
|
|
|
float angle_roll_acc, angle_pitch_acc;
|
|
|
|
|
|
|
|
float angle_pitch, angle_roll;
|
|
|
|
int angle_pitch_buffer, angle_roll_buffer;
|
|
|
|
float angle_pitch_output, angle_roll_output;
|
|
|
|
|
|
|
|
long loop_timer;
|
|
|
|
int temp;
|
|
|
|
|
|
|
|
//motor stuff
|
2021-04-14 00:20:35 +02:00
|
|
|
#define encoderA1 2
|
|
|
|
#define encoderB1 3
|
|
|
|
|
|
|
|
#define encoderA2 4
|
|
|
|
#define encoderB2 7
|
|
|
|
volatile int countA = 0;
|
2021-04-14 01:55:49 +02:00
|
|
|
volatile int countAold = 0;
|
2021-04-14 00:20:35 +02:00
|
|
|
volatile int countB = 0;
|
|
|
|
|
2021-04-13 20:17:11 +02:00
|
|
|
const int MotorPinA = 12;
|
|
|
|
const int MotorSpeedA = 3;
|
|
|
|
const int MotorBrakeA = 9;
|
|
|
|
|
|
|
|
const int MotorPinB = 13;
|
|
|
|
const int MotorSpeedB = 11;
|
|
|
|
const int MotorBrakeB = 8;
|
|
|
|
|
|
|
|
const int CCW = HIGH;
|
|
|
|
const int CW = LOW;
|
|
|
|
|
|
|
|
volatile byte half_revolutionsA;
|
|
|
|
volatile byte half_revolutionsB;
|
|
|
|
unsigned int rpmA;
|
|
|
|
unsigned int rpmB;
|
|
|
|
unsigned long timeoldA;
|
|
|
|
unsigned long timeoldB;
|
2021-04-14 01:55:49 +02:00
|
|
|
float rpm = 0;
|
|
|
|
float rps = 0;
|
2021-04-13 20:17:11 +02:00
|
|
|
|
|
|
|
void setup() {
|
2021-04-13 21:51:43 +02:00
|
|
|
Wire.begin();
|
2021-04-13 20:17:11 +02:00
|
|
|
Serial.begin(115200);
|
|
|
|
while (!Serial)
|
|
|
|
delay(10); // will pause Zero, Leonardo, etc until serial console opens
|
2021-04-13 21:44:53 +02:00
|
|
|
|
|
|
|
gyro_setup();
|
2021-04-13 20:17:11 +02:00
|
|
|
|
|
|
|
//motor
|
2021-04-14 00:20:35 +02:00
|
|
|
pinMode(encoderA1, INPUT_PULLUP);
|
|
|
|
pinMode(encoderB1, INPUT_PULLUP);
|
|
|
|
pinMode(encoderA2, INPUT_PULLUP);
|
|
|
|
pinMode(encoderB2, INPUT_PULLUP);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(encoderA1), pulseA, RISING);
|
|
|
|
//attachInterrupt(digitalPinToInterrupt(encoderB1), pulseB, RISING);
|
|
|
|
|
2021-04-13 20:17:11 +02:00
|
|
|
pinMode(MotorPinA, OUTPUT);
|
|
|
|
pinMode(MotorSpeedA, OUTPUT);
|
|
|
|
pinMode(MotorBrakeA, OUTPUT);
|
|
|
|
|
|
|
|
pinMode(MotorPinB, OUTPUT);
|
|
|
|
pinMode(MotorSpeedB, OUTPUT);
|
|
|
|
pinMode(MotorBrakeB, OUTPUT);
|
|
|
|
}
|
2021-04-14 01:52:34 +02:00
|
|
|
|
2021-04-13 20:17:11 +02:00
|
|
|
void loop() {
|
2021-04-13 21:44:53 +02:00
|
|
|
gyro_loop();
|
2021-04-13 22:09:37 +02:00
|
|
|
safe_angle = int(round(angle_pitch_output));
|
2021-04-14 01:52:34 +02:00
|
|
|
|
2021-04-14 01:54:01 +02:00
|
|
|
int m = millis();
|
|
|
|
|
2021-04-14 02:49:29 +02:00
|
|
|
if (m - lastCorrectionTime >= fastTimer) { //run this code ever 80ms (12.5hz)
|
2021-04-14 01:54:01 +02:00
|
|
|
lastCorrectionTime = m;
|
|
|
|
getSpeed();
|
|
|
|
setSpeed();
|
|
|
|
}
|
2021-04-14 02:49:29 +02:00
|
|
|
if (m - lastPrintTime >= slowTimer) { //run this code ever 800ms (1.25hz)
|
2021-04-14 01:54:01 +02:00
|
|
|
lastPrintTime = m;
|
|
|
|
printInfo();
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 02:57:38 +02:00
|
|
|
|
2021-04-14 01:55:49 +02:00
|
|
|
void getSpeed() {
|
2021-04-14 02:57:38 +02:00
|
|
|
float alpha = 2.0 / (3.0 + 1.0); //Alpha aka EMA length. 3 length = 3*80ms=0.24s
|
2021-04-14 02:50:15 +02:00
|
|
|
float rpm_const = 1071.43/fastTimer; //60000ms(1min) / ticks per rotation / fasttimer
|
|
|
|
float rps_const = 112.23/fastTimer; //1000ms(1s) / ticks per rotation / fasttimer
|
|
|
|
|
2021-04-14 01:55:49 +02:00
|
|
|
int ticks = countA-countAold;
|
2021-04-14 02:50:15 +02:00
|
|
|
float rpm_new = ticks*rpm_const;
|
|
|
|
float rps_new = ticks*rps_const;
|
|
|
|
rpm = rpm * (1-alpha) + rpm_new * alpha;
|
|
|
|
rps = rps * (1-alpha) + rps_new * alpha;
|
2021-04-14 01:55:49 +02:00
|
|
|
countAold = countA;
|
|
|
|
}
|
2021-04-14 02:57:38 +02:00
|
|
|
|
2021-04-14 01:55:14 +02:00
|
|
|
void printInfo(){
|
|
|
|
Serial.println("");
|
|
|
|
Serial.print("pitch Angle = "); Serial.println(angle_pitch_output);
|
|
|
|
Serial.print("pitch Angle abs = "); Serial.println(abs(safe_angle));
|
|
|
|
Serial.print("pitch Angle measured = "); Serial.println(angle_pitch);
|
|
|
|
Serial.print("Position: "); Serial.println(countA);
|
2021-04-14 17:38:24 +02:00
|
|
|
Serial.print("Position (m): "); Serial.println(countA/174.76); //r*2pi
|
2021-04-14 01:55:14 +02:00
|
|
|
Serial.print("Full Rotations: "); Serial.println(countA/56.0); //ca. 56 tick per rotation
|
|
|
|
Serial.print("Rads rotated: "); Serial.println(countA/8.91); //ca. 56 tick per rotation, 6.26 rads per rotation
|
|
|
|
Serial.print("RPM: "); Serial.println(rpm); //ca. 56 tick per rotation
|
|
|
|
Serial.print("Rads per second: "); Serial.println(rps); //ca. 56 tick per rotation, 6.26 rads per rotation
|
2021-04-13 22:09:37 +02:00
|
|
|
|
2021-04-14 01:55:14 +02:00
|
|
|
//Serial.print("A: "); Serial.println(countA);
|
|
|
|
//Serial.print("B: "); Serial.println(countB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSpeed(){
|
2021-04-13 22:29:15 +02:00
|
|
|
if(abs(safe_angle)<50 ){
|
|
|
|
speed = 8*safe_angle;
|
|
|
|
if(speed<0){
|
2021-04-13 20:17:11 +02:00
|
|
|
digitalWrite(MotorPinB, CW);
|
|
|
|
digitalWrite(MotorPinA, CCW);
|
2021-04-13 22:29:15 +02:00
|
|
|
speed -= 30;
|
2021-04-13 20:17:11 +02:00
|
|
|
}
|
2021-04-13 22:29:15 +02:00
|
|
|
else if(speed>0){
|
2021-04-13 20:17:11 +02:00
|
|
|
digitalWrite(MotorPinB, CCW);
|
|
|
|
digitalWrite(MotorPinA, CW);
|
2021-04-13 22:29:15 +02:00
|
|
|
speed += 30;
|
2021-04-13 20:17:11 +02:00
|
|
|
}
|
2021-04-13 22:29:15 +02:00
|
|
|
else {
|
|
|
|
speed = 0;
|
|
|
|
}
|
|
|
|
speed = abs(speed);
|
|
|
|
speed = constrain(speed, 0, 249);
|
2021-04-13 22:09:37 +02:00
|
|
|
analogWrite(MotorSpeedB, speed); //Wheel close to connections
|
|
|
|
analogWrite(MotorSpeedA, speed); //First experiment wheel
|
2021-04-13 20:17:11 +02:00
|
|
|
}
|
|
|
|
else{
|
2021-04-13 22:09:37 +02:00
|
|
|
speed = 0;
|
|
|
|
analogWrite(MotorSpeedB, speed);
|
|
|
|
analogWrite(MotorSpeedA, speed);
|
2021-04-13 20:17:11 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 00:20:35 +02:00
|
|
|
int directionA(){
|
|
|
|
if(digitalRead(encoderA2) == HIGH){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 20:17:11 +02:00
|
|
|
|
2021-04-14 00:20:35 +02:00
|
|
|
int directionB(){
|
|
|
|
if(digitalRead(encoderB2) == HIGH){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pulseA(){
|
|
|
|
countA += directionA();
|
|
|
|
}
|
|
|
|
|
|
|
|
void pulseB(){
|
|
|
|
countB += directionB();
|
|
|
|
}
|