I'm working on a quad rotor project using the ChibiOS system. You can see the full source here: https://github.com/dancollins/Quad-Roto ... /QuadRotor
The problem is in this piece of code:
Code: Select all
static msg_t ControllerThread(void *arg) { // Controller thread
(void)arg; // Don't need arguments...
systime_t time = chTimeNow();
int32_t pitch_angle_accum = 0;
int32_t roll_angle_accum = 0;
while (1) {
accel_read();
gyro_read();
//IMUupdate(gyro.x * 0.0012141420883438813, gyro.y * 0.0012141420883438813, gyro.z * 0.0012141420883438813, accel.x, accel.y, accel.z);
float norm_x = 2*(q0*q2 - q1*q3);
float norm_y = 2*(q0*q1 + q2*q3);
float norm_z = (1 - 2*(q1*q1 + q2*q2));
/*float elev = acosf(norm_z);
float azim = atan2f(norm_y, norm_x);
/*int16_t pitch = elev * sinf(azim) / M_PI * 32767;
int16_t roll = elev * cosf(azim) / M_PI * 32767;
int16_t yaw = 0;
/*int16_t pitch_angle_target = 0; // TODO: Fix
int16_t roll_angle_target = 0; // TODO: Fix
int16_t pitch_angle_error = pitch_angle_target - pitch;
int16_t roll_angle_error = roll_angle_target - roll;
// 25 deg max error.
pitch_angle_error = saturate(pitch_angle_error, 4551);
roll_angle_error = saturate(roll_angle_error, 4551);
pitch_angle_accum += pitch_angle_error;
roll_angle_accum += roll_angle_error;
// 20 deg s max error.
pitch_angle_accum = saturate(pitch_angle_accum, 364088);
roll_angle_accum = saturate(roll_angle_accum, 364088);
int32_t pitch_rate_target = (pitch_angle_error * 2 * 65536 + pitch_angle_accum * 98) >> 16;
int32_t roll_rate_target = (roll_angle_error * 2 * 65536 + roll_angle_accum * 98) >> 16;
int16_t pitch_rate_comp = ((pitch_rate_target - (gyro.x * 4000 / 360)) * 6 * 36) >> 16;
int16_t roll_rate_comp = ((roll_rate_target - (gyro.y * 4000 / 360)) * 6 * 36) >> 16;
pitch_rate_comp = saturate(pitch_rate_comp, 250);
roll_rate_comp = saturate(roll_rate_comp, 250);
motor_set(0, pitch_rate_comp, roll_rate_comp, 0);*/
time += MS2ST(100);
chThdSleepUntil(time);
}
return 0;
}How the code is commented above results in successful PWM (the motors are continually refreshed). However, removing the first '/*' (commenting out the elev and azim) results in the PWM's refresh loop no long running. Same goes if the IMUUpdate function is uncommented.
math.h is correctly included, and linked (using the "-lm" flag), and the functions do not generate any warnings in the compiler. However, they seem to prevent the PWM working. The system does not hang, as the blinking LED thread still runs correctly.
Any advice would be much appreciated.
Cheers,
MM