Detecting a Change In Sign

Hello everyone,

This is my first post, I have thoroughly searched for a similar topic but haven't found one. Please redirect me to a forum topic if one exists.

As part of a project I am developing a reaction wheel (RW) which is used to change the attitude of a satellite in one axis (a meccano test bed at this stage). The point is to create a PID controller that will attempt to maintain the attitude at 0 degree yaw angle. I am using an MPU6050 to detect the yaw angle and using this angle (converted to degrees) as an input for my PID controller. The MPU displays the yaw as an angle from 0 to 180 degrees and 0 to -180 degrees and my RW needs to spin either CW or CCW depending on the sign of yaw. Because the RW spins at high angular velocities I need to use regenerative braking when I have to reverse the direction of rotation.

So basically I need the braking to activate only when the satellite overshoots the 0 Setpoint and the yaw angle changes from positive to negative and vice a versa. Therefore I need the Arduino to detect when there is a change in sign.

Thank you XD

p.s I can post the code I currently have although I am still at the "drawing board" stage for this braking sequence upon yaw sign change and thought it wouldn't be that relevant.

Each time you read the value save it. Next time you read it compare it with the previous value.

if previous value greater than zero and current value less than zero the reading has gone from positive to negative
if current value greater than zero and previous value less than zero the reading has gone from negative to positive

I strongly suspect that there is a smarter way to do this ....

If you want to detect a change in sign, multiply the previous and current values together and if the result is negative the sign has changed. Or for a more efficient implementation but taking more keystrokes to type in, test for previous value negative and new value positive or vice versa in a compound if statement.

Just from your problem description I wouldn't expect to need to detect changes in sign explicitly - I'd expect the control algorithm to produce a torque requirement and have an algorithm to generate that torque by applying power or braking based on the current speed and direction of the mass.