I can flesh out CyberWolf_22’s answer with a more long-winded answer. He snuck in while I was still typing. (Young whipper-snapper! ) His answer I assume is correct as it does work on the robot. One thing I’ve learned is to know what does or does not need fixing.
There are some twists in the code which should be cleaned up, and actually Kevin Watson (of kevin.org) suggested removing Default_Routine() because of conflicts and using a modified version to use with his code. I’ll assume you at least understand the C commands.
p1_x = 255 - p1_y;
p1_y = 255 - pwm05;
Why x and y is switching? Dunno. My guess is when this code was written the joystick controls were different. I rewrote this so x was x and y was y. You can test it on your own robot by switching it back. The “255 -” part just reverses the number from 0~255, to 255~0. Again, you can test this. Nothing like real-life testing to prove things.
Why pwm05? This is the sneaky part. Look above where all the pwm’s get mapped from various joystick controls. pwm05 is set to p1_x. If you use
p1_x = 255 - p1_y;
p1_y = 255 - p1_x;
then p1_y will actually be set to 255 - ( 255 - p1_y ), not what is intended. The programmer just sneaked the number from another variable.
Why they mapped out all the pwms like that probably goes back to the dark ages of this code. There is a lot of code in Default_Routine() that is, well, default code, in case you didn’t customize it for your own use. I commented most of it out and just used a simplified version of the above.
The second part,
pwm13 = pwm14 = Limit_Mix(2000 + p1_y + p1_x - 127);
pwm15 = pwm16 = Limit_Mix(2000 + p1_y - p1_x + 127);
is one of those things you just have to trust. What, you thought I knew the answer? Like your senior programming friend, I know it works so why try to fix it?
Well, actually, back in January I did have to figure it out, and you can do the same. I made a simple spreadsheet that plugged in a range of p1_x and p1_y. Sometimes you just have to do the math. All I know is that it mixes the forward-backward of the joystick with the left-right to get how much power the two left and two right motors get.
Limit_Mix() is just a fancy function to limit the range of numbers to 0~255. The adding of 2000 just makes it easier for C variables, shifting the number line to a positive vaule, and is subtracted out when returned.
One thing that spreadsheet did show me was the secret of reversing the motors on one side, which is why I made it. If your robot is a “standard” built one with four motors, two on left and two on right, then two of your motors are backwards. That is, they spin in the opposite direction to the other side’s motors to make the robot go forward. In past years we’ve (meaning the robot builders – they don’t want me building things!) just reversed the wires on those two motors. Red to black instead of red to red, which is a big no-no. This year they decided it was a programming problem (funny how it always ends up a programming problem), and I knew there was a simple solution. I did the spreadsheet to test out different combinations and the answer is:
pwm13 = pwm14 = Limit_Mix( 2000 + p1_y + p1_x - 127);
pwm15 = pwm16 = Limit_Mix( 2000 + (256 - p1_y) - (256 - p1_x) +127);
Andy, you have the programmer’s mindset, posting your question at 1:24 AM – the best programming is done after midnight!
Roger.