#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#define DEBUG 1
#define MAXANGLE 45
#define ANGLESTEP 10
#define MAXMOTOR 255
#define MOTORSTEP 10
#define QUADFREQ 100
#define STATUSFREQ 10
#define QUADDELAY (1000 / QUADFREQ)
#define STATUSDELAY (1000 / STATUSFREQ)
void flightTask(void);
void statusTask(void);
void controlToggle(void);
void motorToggle(void);
void motorUp(void);
void motorDown(void);
void motorForward(void);
void motorBackward(void);
void motorLeft(void);
void motorRight(void);
void parameterChange(void);
void silent(void);
void printRaw(void);
char PROGMEM motorToggleString[] = "Motor On/Off";
char PROGMEM motorUpString[] = "Up";
char PROGMEM motorDownString[] = "Down";
char PROGMEM motorLeftString[] = "Left";
char PROGMEM motorRightString[] = "Right";
char PROGMEM motorForwardString[] = "Forwards";
char PROGMEM motorBackwardString[] = "Backwards";
char PROGMEM controlToggleString[] = "Toggle PID";
char PROGMEM parameterChangeString[] = "Change PID Params";
char PROGMEM zeroString[] = "Angles to Zero";
char PROGMEM silentString[] = "Toggle Status Output";
char PROGMEM sensorString[] = "Raw Sensor Data";
#define STATE_MOTOR (1 << 0) // 1 -> Motor On
#define STATE_PID (1 << 1) // 1 -> PID enabled
#define STATE_OUTPUT (1 << 2) // 1 -> No Status Output
uint8_t state = 0;
uint8_t speed = 10;
int16_t targetRoll = 0;
int16_t targetPitch = 0;
uint32_t sumFlightTask = 0, countFlightTask = 0;
int main(void) {
for(;;) {
}
return 0;
}
void flightTask(void) {
if (state & STATE_PID) {
} else {
}
if (++countFlightTask >= QUADFREQ) {
countFlightTask = 1;
sumFlightTask = diff;
} else {
sumFlightTask += diff;
}
}
}
void statusTask(void) {
static uint32_t lastDuration = 0;
if (((
getSystemTime() - last) >= STATUSDELAY) && (!(state & STATE_OUTPUT))) {
printf("q%li %li\n", sumFlightTask / countFlightTask, lastDuration);
}
}
void controlToggle(void) {
if (state & STATE_PID) {
state &= ~STATE_PID;
printf("PID Off!\n");
} else {
state |= STATE_PID;
printf("PID On!\n");
}
}
void motorToggle(void) {
if (state & STATE_MOTOR) {
state &= ~STATE_MOTOR;
printf("Motor Off!\n");
} else {
state |= STATE_MOTOR;
printf("Motor On!\n");
}
}
void motorUp(void) {
if (speed <= (MAXMOTOR - MOTORSTEP)) {
if (state & STATE_MOTOR) {
speed += MOTORSTEP;
printf("Throttle up to %i\n", speed);
}
}
}
void motorDown(void) {
if (speed >= MOTORSTEP) {
if (state & STATE_MOTOR) {
speed -= MOTORSTEP;
printf("Throttle down to %i\n", speed);
}
}
}
void motorForward(void) {
if (targetPitch >= (-1 * (MAXANGLE - ANGLESTEP))) {
targetPitch -= ANGLESTEP;
printf("Pitch Forward %i\n", targetPitch);
}
}
void motorBackward(void) {
if (targetPitch <= (MAXANGLE - ANGLESTEP)) {
targetPitch += ANGLESTEP;
printf("Pitch Backwards %i\n", targetPitch);
}
}
void motorLeft(void) {
if (targetRoll <= (MAXANGLE - ANGLESTEP)) {
targetRoll += ANGLESTEP;
printf("Roll Left %i\n", targetRoll);
}
}
void motorRight(void) {
if (targetRoll >= (-1 * (MAXANGLE - ANGLESTEP))) {
targetRoll -= ANGLESTEP;
printf("Roll Right %i\n", targetRoll);
}
}
void parameterChange(void) {
double p, i, d, min, max, iMin, iMax;
int c = scanf("%lf %lf %lf %lf %lf %lf %lf", &p, &i, &d, &min, &max, &iMin, &iMax);
if (c == 7) {
} else {
printf("Only got %i (%lf %lf %lf %lf %lf %lf %lf)!\n", c, p, i, d, min, max, iMin, iMax);
}
}
void silent(void) {
if (state & STATE_OUTPUT) {
state &= ~STATE_OUTPUT;
} else {
state |= STATE_OUTPUT;
}
}
void printRaw(void) {
printf(
"Ax: %f Ay: %f Az: %f\n", v.
x, v.
y, v.
z);
printf(
"Gx: %f Gy: %f Gz: %f\n", v.
x, v.
y, v.
z);
printf(
"Mx: %f My: %f Mz: %f\n", v.
x, v.
y, v.
z);
}