Our robot is using a four wheel, four motor omni drive system. Our code gives good output except for sometimes it swaps outputs. Instead of giving a 250, it gives a 4.
drive_ly and drive_lx are our drive joystick.
motor_fl_1 and all the others are the variables we send to the motors. fl=front left.
pwm_drive_fl are the actual motors.
Heres the code:
if ( (drive_ly + drive_lx - 127) > 254 )
motor_fl_1 = 254;
else if ( ( drive_ly + drive_lx - 127) < 0 )
motor_fl_1 = 0;
else motor_fl_1 = drive_ly + drive_lx - 127;
if ( ( 127 - drive_ly + drive_lx ) > 254 )
motor_fr_1 = 254;
else if ( ( 127 - drive_ly + drive_lx ) < 0 )
motor_fr_1 = 0;
else motor_fr_1 = 127 - drive_ly + drive_lx;
if ( ( 382 - drive_ly - drive_lx ) > 254 )
motor_br_1 = 254;
else if ( ( 382 - drive_ly - drive_lx ) < 0 )
motor_br_1 = 0;
else motor_br_1 = 382 - drive_ly - drive_lx;
if ( ( 127 + drive_ly - drive_lx ) > 254 )
motor_bl_1 = 254;
else if ( ( 127 + drive_ly - drive_lx ) < 0 )
motor_bl_1 = 0;
else motor_bl_1 = 127 + drive_ly - drive_lx;
if ( (motor_fl_1 < 137) && ( motor_fl_1 > 117 ) )
pwm_drive_fl = 127;
else pwm_drive_fl = motor_fl_1;
if ( (motor_fr_1 < 137) && ( motor_fr_1 > 117 ) )
pwm_drive_fr = 127;
else pwm_drive_fr = motor_fr_1;
if ( (motor_br_1 < 137) && ( motor_br_1 > 117 ) )
pwm_drive_br = 127;
else pwm_drive_br = motor_br_1;
if ( (motor_bl_1 < 137) && ( motor_bl_1 > 117 ) )
pwm_drive_bl = 127;
else pwm_drive_bl = motor_bl_1;
we’re pretty sure its an overflow error, but this code should make up for that.
can anybody help please?
Anytime you add two chars together you risk overflow. Try making all your variables ints. Overflow is still possible, but only when you’re working with numbers a lot larger than joystick and motor values.
yea that was our first idea too.
we made everything signed ints in order to avoid this issue. but somehow when we tried to limit mix them into the 0-254 range, the limit mix failed or something.
but no matter what happened it still didnt work.
could it be that we never casted them back into unsigned chars before assigning them to the pwm? because when i tried that we got pure 254 for all four outputs.
thanks for the help by the way. much appreciated.
First uses this:
pwm13 = pwm14 = Limit_Mix(2000 + p1_y + p1_x - 127);
pwm15 = pwm16 = Limit_Mix(2000 + p1_y - p1_x + 127);
They Add 2000 to prevent a negative value and when they call LIMIT_MIX ()
It evaluates the result and subtract out the 2000. I think your code could be changed to
motor_fl_1 = Limit_Mix(2000 + drive_ly + drive_lx - 127);
motor_fr_1 = Limit_Mix(2000 + 127 - drive_ly + drive_lx);
motor_br_1 = Limit_Mix(2000 + 382 - drive_ly - drive_lx);
motor_bl_1 = Limit_Mix(2000+ 127 + drive_ly - drive_lx);
if ( (motor_fl_1 < 137) && ( motor_fl_1 > 117 ) )
pwm_drive_fl = 127;
else pwm_drive_fl = motor_fl_1;
if ( (motor_fr_1 < 137) && ( motor_fr_1 > 117 ) )
pwm_drive_fr = 127;
else pwm_drive_fr = motor_fr_1;
if ( (motor_br_1 < 137) && ( motor_br_1 > 117 ) )
pwm_drive_br = 127;
else pwm_drive_br = motor_br_1;
if ( (motor_bl_1 < 137) && ( motor_bl_1 > 117 ) )
pwm_drive_bl = 127;
else pwm_drive_bl = motor_bl_1;
This should prevent any over runs even if your joystick is doing something weird. Are you using a chicket?
Although I must admit I don’t understand the “382 - drive_ly - drive_lx” but I’ll assume you know the formula to be correct and that the problem you’re having is from a violation in a positive integer math problem.
Hope that helps
Steve
thanks for that code. and yes we are using a chicklet to enable us to use an xbox 360 controller. does that make a difference?
and we tried using the default limit mixes, they cased the same problem, which is why i wrote my own up in that code. we suspected that somewhere there was a datatype conversion that messed us up, so we tried to eliminate all of them.
Chicklet can give weird outputs if the calibration is off or if the battery goes low.
I had that problem once and have made sure the battery we use is fully charged whenever we use it.
Try using one of the joysticks as a test to see if the problem occurred in the code or just when the Chicklet and Xbox controller is being used. Since you are having the problem occur with default code and the Chicklet/Xbox, that’s almost certainly the problem. Default is pretty solid. If the numbers are inverting in default code with that 250 to 4 flip, it’s probably happening because the system is getting that information in to begin with.
Good luck
Steve