Académique Documents
Professionnel Documents
Culture Documents
#include
#include
#include
#include
#include
<Wire.h>
"IMU.h"
"tools.h"
"filters.h"
"Encoders.h"
"mario.h"
#define
#define
#define
#define
MPW_R_PIN
MDR_R_PIN
MPW_L_PIN
MDR_L_PIN
10
12
11
13
#define
#define
#define
#define
EN_R0_PIN
EN_R1_PIN
EN_L0_PIN
EN_L1_PIN
A0
A1
A2
A3
//ENCODER myEncoder;
//PID_CONTROL PID_MTR_R;
//PID_CONTROL PID_MTR_L;
PID_CONTROL PID_MTR_pitch(50, 0, 5);
PID_CONTROL PID_MTR_fall(10, 0, 2);
PID_CONTROL PID_MTR_yaw(3, 0, 1);
PID_CONTROL PID_MTR_twist(1, 0, 0);
TIMER_MS arming_timer;
double
FILTER
double
double
FILTER
desire_tilt;
pitchFilter;
estSpeedErr = 0;
estSpeedErr_old = 0;
estSpeedFilter;
//Mforword = constrain_map_255(forword);
double angleDiff = yaw - direction_y;
angleDiff = constrain_angle_deg(angleDiff);
double turn = PID_MTR_yaw.output(0, angleDiff, dt) + PID_MTR_twist.output(0,
rotout * 500, dt);
//
//Speed estimation
estSpeedErr += //estSpeedFilter.fwrdAvg_sigma
(forword - speed_x);
//
estSpeedErr_old = estSpeedErr;
}
}
double constrain_angle_deg(double angle) {
angle = (angle > 180) ? (angle - 360) : ((angle < -180) ? (angle + 360) : angl
e);
return angle;
}
double constrain_map_255(double val) {
double in_min, in_max;
double out_min, out_max;
if (val < 0)
{
in_min = val;
in_max = 0;
out_min = (val < -255) ? -255 : val;
out_max = 0;
}
else
{
in_min = 0;
in_max = val;
out_min = 0;
out_max = (val > 255) ? 255 : val;
}
val = map(val, in_min, in_max, out_min, out_max);
return val;
}
void toggle_arming() {
armed = !armed;
anlgeCorrect = 0;
direction_y = yaw;
Serial.println((armed) ? "Armed" : "Disarmed");
}
void serialRead() {
if (Serial.available() > 0) {
// read the incoming byte:
char incomingByte = Serial.read();
switch (incomingByte) {
case 'w':
speed_x = 45;
Serial.println("forward");
break;
case 's':
speed_x = -45;
Serial.println("backward");
break;
case 'd':
direction_y -= 45;
direction_y = (direction_y >= 360) ? direction_y - 360 : ((direction_y <
0) ? direction_y + 360 : direction_y);
Serial.println("turn left");
break;
case 'a':
direction_y += 45;
direction_y = (direction_y >= 360) ? direction_y - 360 : ((direction_y <
0) ? direction_y + 360 : direction_y);
Serial.println("turn right");
break;
case '-':
readOption();
break;
case ' ':
speed_x = 0;
break;
}
}
}
void readOption() {
if (Serial.available() > 1) {
char opt = Serial.read();
char sp = Serial.read();
if (sp != ' ') {
Serial.println("Error, can't read option");
return;
}
switch (opt) {
case 'P':
Serial.print("Set Kp:");
Serial.print(PID_MTR_pitch.Kp = readValue());
break;
case 'I':
Serial.print("Set Ki:");
//Serial.print(PID_MTR_pitch.Ki=readValue());
break;
case 'D':
Serial.print("Set Kd:");
Serial.print(PID_MTR_pitch.Kd = readValue());
break;
case 'p':
Serial.print("Set Kp:");
Serial.print(angCor_Kp = readValue());
break;
case 'i':
Serial.print("Set Ki:");
//Serial.print(PID_MTR_pitch.Ki=readValue());
break;
case 'd':
Serial.print("Set Kd:");
Serial.print(angCor_Kd = readValue());
break;
case 'b':
Serial.print("Bat Lvl: ");
Serial.print(float(analogRead(A0)) / 1024.0f * 5);
break;
case 'N':
AutoMode = !AutoMode;
Serial.print("AutoMode:");
Serial.print(AutoMode ? "No" : "Off");
break;
case 'm':
playMario = !playMario;
playTime = millis();
Serial.print("Mario:");
Serial.print(playMario ? "No" : "Off");
break;
default:
Serial.print("Option not fond");
}
Serial.println();
}
else Serial.println("Error, can't read option");
}
float readValue() {
String val = "";
if (Serial.available() > 0) {
while (Serial.available() > 0) {
char inChar = Serial.read();
if (inChar == ' ') break;
val += inChar;
}
//val.trim();
//Serial.print(val.toFloat());
return val.toFloat();
}
else Serial.println("Error, can't read value");
}
void motor_power(int PW_R, int PW_L)
{
if (!armed) {
analogWrite(MPW_R_PIN, 0);
analogWrite(MPW_L_PIN, 0);
return;
}
digitalWrite(MDR_R_PIN, (PW_R > 0) ? LOW : HIGH);
digitalWrite(MDR_L_PIN, (PW_L > 0) ? LOW : HIGH);
//
//
//
//
//
//
//
//
//
PW_R = abs(PW_R);
PW_L = abs(PW_L);
if (int max = max(PW_L, PW_R) > 255) {
PW_R = map(PW_R, 0, max, 0, 255);
PW_R = map(PW_R, 0, max, 0, 255);
}
analogWrite(MPW_R_PIN, PW_R);
analogWrite(MPW_L_PIN, PW_L);
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
US_readed = false;
Sonar_time = micros();
}
void US_echo() {
if (US_readed) return;
unsigned long dt = micros() - Sonar_time;
US_read = (dt <= echoTimeOut) ? (double)dt : (double)echoTimeOut;
US_read *= 0.17;
US_readed = true;
}