# Control Problems

We have our bot set up for one-joystick drive, with the two-motor transmissions hooked up to PWMs 13-16. here’s the set-up:

(right side)
PWM 13 = PWM 14 = Limit_Mix(2000 + p1_y + p1_x - 127)

(left side)
PWM 15 = PWM 16 = Limit_Mix(2000 _ p1_y + p1_x + 127)

The wheels go forwards and backwards correctly, but turns like a boat (turns left when you push the joystick right; turns right when you push the joystick left).

Any idea why this isn’t working??

2000 _ p1_y

Isn’t that supposed to be a - not underscore

2000 - p1_y

It sounds like the left and right sides are swapped.

Whoops, yeah. Guess I got a little shift-happy when I typed that out. In the code it’s a -.

I figured that much, but when I tried to mess with the terms, switching the + and -, I only succeeded in making the whole control scheme inverted (how ironic). By that I mean that if you turned the joystick upside down, it worked perfectly. Then I figured that if the controls were inverted, everything would work if I made everything the opposite of what it was, so it looked something like:

``````
PWM 13 = PWM 14 = Limit_Mix(-(2000 + p1_y + p1_x - 127));
PWM 15 = PWM 16 = Limit_Mix(-(2000 - p1_y + p1_x + 127));

``````

That isn’t the correct placement of the +'s and -'s, but you get the idea. Unfortunately, that just made the wheels spin out of control, unable to be feathered. Any idea what I could be doing wrong? I was thinking of just reversing the polarity of the motors on one side so the code would be the same for both sides, but I’d rather mess with the code than screw with the wiring.

It’s terribly important that those 2000 terms stay positive. Think it through, though. If p1_x controls the left and right turning, and your turning is backwards, wouldn’t it make sense to make make p1_x the opposite of what it is in each of those formulae?

When I encounter problems like this where I’m not sure if my logic or formulas are working I create some test cases on paper with real numbers and see what happens. When I do the math on the original code it appears to me that the steering is reversed - based on the comments! But I don’t have a working robot in front of me to verify.

You went to far in modifying the code. If this original doesn’t work:

``````  pwm13 = pwm14 = Limit_Mix(2000 + p1_y + p1_x - 127);
pwm15 = pwm16 = Limit_Mix(2000 + p1_y - p1_x + 127);
``````

then either swap the pwm vars between formulas or change just the way the x and y values are mixed like this:

``````  pwm13 = pwm14 = Limit_Mix(2000 + p1_y - p1_x + 127);
pwm15 = pwm16 = Limit_Mix(2000 + p1_y + p1_x - 127);
``````

Before I left the shop today I copied down the X and Y values given when I moved the joystick in all directions, and I was going to do the math on it tomorrow to see what was going wrong (or something, I’m not really sure what I was looking for). For some reason I think I messed with pairs of terms, instead of individual terms, which probably explains the problem. Though, when I originally switched the signs of the whole Limit_Mix operation, I did leave the 2000 positive, but changed everything else, and got the same result, or something similar.

Thanks for the suggestion, I always overlook the ‘simple’ solutions. I’ll try that tomorrow and see if it works.

I wouldn’t expect 2000 - p1_y to work.

Not to bypass the theory and the important lesson of completely understanding it, but couldn’t you simply swap the pwm outputs for the left and right side and move on?

Just say “no” to PWMs 13-16. (Are you using open-loop driving?)

Yes, like Rick said PWMs 13-16 can be glitchy, especially in combination with interrupts.

See the Motors and the High Port Numbers post, but to summarize it in the words of M. McLeod:

Good luck to all,
Robinson

I did that, but since the motors are facing opposite directions, one side went forward while the other went back. So pushing the stick forward made it turn left, pushing it to the right made it go straight, and so on. Ugh, this is so annoying.

If anything is moving in the wrong direction, you could just subtract the Limit_Mix result from 254. (To reverse direction)

for the right motor I use
(254-(Limit_Mix(2000 + p1_y + p1_x - 127)));

for the left motor I use
(Limit_Mix(2000 + p1_y - p1_x + 127));

I’ve used this setup for all of my team’s robots.

not in this case. His foward and reverse was correct. Left and right were wrong.

if he inverted the PWMs to the motors then forward would go backward.

I was replying to his previous post where his forward and reverse were not correct.

IN RESPONSE TO:
“When I encounter problems like this where I’m not sure if my logic or formulas are working I create some test cases on paper with real numbers and see what happens. When I do the math on the original code it appears to me that the steering is reversed - based on the comments! But I don’t have a working robot in front of me to verify.”

Hi my name is Holly, I am a mentor for team 1228, (the team with this problem) and I have taken your advice and plugged these numbers into the formula, and it seems like the formula creates the backward turning that we are experiencing…
If I put in a value for full power to the right (x axis=255) and neutral on fwd/rev (y axis=127). THe values that the program returns are (right motors=full power=255) BUT…that would make the robot go into a LEFT spin (correct)? Because the right wheels are in full forward motion?

If I put in a value for full power to the right (x axis=255)…

I believe that the OI for the Joysticks generate around 255 when pressed to the left, not right. A simple print statement or assigning the user_byte to the p1_x value should confirm this quite quickly.

If the wiring is done such that the motors spin the opposite way from what we want with the Joystick centered left to right, we change the motor wiring. Keeping 254 meaning forward on both sides just makes more sense when debugging.

In looking through this thread it appears to me that in inverted’s posts there are signs wrong on the p1_x term. One of the choices presented by Keith Watson in his second post should be the correct solution. It matches what we’ve done for years.

It sounds like your y-axis is correct (forward is forward, back is back), but your x-axis, which controls the turn, needs to be reversed.

Prior to the mixing equation you can use p1_x = 254 - p1_x to reverse the direction of your turn.

e.g.,

``````p1_x = 254 - p1_x;
pwm13 = pwm14 = Limit_Mix(2000 + p1_y + p1_x - 127);
pwm15 = pwm16 = Limit_Mix(2000 + p1_y - p1_x + 127);
``````

Just tried it, and that works for me, too. Thanks a lot!

One more quick question. We have a motor on the bot that we don’t need on all the time, just to save battery power. I wired a toggle switch so that when it’s turned on, it shows up as the trigger button of a joystick being held on. Is there any way I can set a PWM

to be equal to the y-axis of a joystick, ONLY when the switch is on, and then neutral when it’s off? Here’s the idea I had:

``````
int my_y;

pwm02 = my_y;
if (p3_sw_trig==1){
my_y = p2_y;
}
else {
my_y == 127;
}

``````

Chances are it’s completely wrong, but…

That is the right idea. Just change the red thing from comparison to assignment.