34 #include <avr/pgmspace.h>
62 #define QUADDELAY (1000 / QUADFREQ)
63 #define STATUSDELAY (1000 / STATUSFREQ)
65 void flightTask(
void);
66 void statusTask(
void);
67 void controlToggle(
void);
68 void motorToggle(
void);
71 void motorForward(
void);
72 void motorBackward(
void);
74 void motorRight(
void);
75 void parameterChange(
void);
79 char PROGMEM motorToggleString[] =
"Motor On/Off";
80 char PROGMEM motorUpString[] =
"Up";
81 char PROGMEM motorDownString[] =
"Down";
82 char PROGMEM motorLeftString[] =
"Left";
83 char PROGMEM motorRightString[] =
"Right";
84 char PROGMEM motorForwardString[] =
"Forwards";
85 char PROGMEM motorBackwardString[] =
"Backwards";
86 char PROGMEM controlToggleString[] =
"Toggle PID";
87 char PROGMEM parameterChangeString[] =
"Change PID Params";
88 char PROGMEM zeroString[] =
"Angles to Zero";
89 char PROGMEM silentString[] =
"Toggle Status Output";
90 char PROGMEM sensorString[] =
"Raw Sensor Data";
92 #define STATE_MOTOR (1 << 0) // 1 -> Motor On
93 #define STATE_PID (1 << 1) // 1 -> PID enabled
94 #define STATE_OUTPUT (1 << 2) // 1 -> No Status Output
98 int16_t targetRoll = 0;
99 int16_t targetPitch = 0;
101 uint32_t sumFlightTask = 0, countFlightTask = 0;
139 void flightTask(
void) {
145 if (state & STATE_PID) {
154 if (++countFlightTask >= QUADFREQ) {
156 sumFlightTask = diff;
158 sumFlightTask += diff;
163 void statusTask(
void) {
165 static uint32_t lastDuration = 0;
166 if (((
getSystemTime() - last) >= STATUSDELAY) && (!(state & STATE_OUTPUT))) {
169 printf(
"q%li %li\n", sumFlightTask / countFlightTask, lastDuration);
183 void controlToggle(
void) {
184 if (state & STATE_PID) {
186 printf(
"PID Off!\n");
193 void motorToggle(
void) {
194 if (state & STATE_MOTOR) {
195 state &= ~STATE_MOTOR;
197 printf(
"Motor Off!\n");
199 state |= STATE_MOTOR;
201 printf(
"Motor On!\n");
206 if (speed <= (MAXMOTOR - MOTORSTEP)) {
207 if (state & STATE_MOTOR) {
210 printf(
"Throttle up to %i\n", speed);
215 void motorDown(
void) {
216 if (speed >= MOTORSTEP) {
217 if (state & STATE_MOTOR) {
220 printf(
"Throttle down to %i\n", speed);
225 void motorForward(
void) {
226 if (targetPitch >= (-1 * (MAXANGLE - ANGLESTEP))) {
227 targetPitch -= ANGLESTEP;
229 printf(
"Pitch Forward %i\n", targetPitch);
233 void motorBackward(
void) {
234 if (targetPitch <= (MAXANGLE - ANGLESTEP)) {
235 targetPitch += ANGLESTEP;
237 printf(
"Pitch Backwards %i\n", targetPitch);
241 void motorLeft(
void) {
242 if (targetRoll <= (MAXANGLE - ANGLESTEP)) {
243 targetRoll += ANGLESTEP;
245 printf(
"Roll Left %i\n", targetRoll);
249 void motorRight(
void) {
250 if (targetRoll >= (-1 * (MAXANGLE - ANGLESTEP))) {
251 targetRoll -= ANGLESTEP;
253 printf(
"Roll Right %i\n", targetRoll);
257 void parameterChange(
void) {
258 double p, i, d, min, max, iMin, iMax;
259 int c = scanf(
"%lf %lf %lf %lf %lf %lf %lf", &p, &i, &d, &min, &max, &iMin, &iMax);
264 printf(
"Only got %i (%lf %lf %lf %lf %lf %lf %lf)!\n", c, p, i, d, min, max, iMin, iMax);
269 if (state & STATE_OUTPUT) {
271 state &= ~STATE_OUTPUT;
274 state |= STATE_OUTPUT;
278 void printRaw(
void) {
281 printf(
"Ax: %f Ay: %f Az: %f\n", v.
x, v.
y, v.
z);
283 printf(
"Gx: %f Gy: %f Gz: %f\n", v.
x, v.
y, v.
z);
285 printf(
"Mx: %f My: %f Mz: %f\n", v.
x, v.
y, v.
z);