![]() |
Default code question
I was going through the default code today and I had passed this particular segment and wondered exactly how it works. to let you all know I am a new programmer and our current programmer is a senior and doesn't explain anything to me.
Code:
p1_x = 255 - p1_y;Code:
unsigned char Limit_Mix (int intermediate_value) |
Re: Default code question
The basic idea is that it takes the p1_x value and adds it to p1_y for one side of the robots motors and subtracts it from p1_y for the other set of motors.
This makes it so that a single joystick can provide control of a tank steered robot. The limit_mix function stops the values from going out of range of an ordinary PWM value of 0-254. Take for example if the y axis is 0 and you move the x axis. You will be adding and subtracting from zero so each set of motors PWM value will have the same magnitude away from neutral, 127, but in opposite directions so the robot will spin in place. If you move the y axis while the x axis is non zero the robot will begin to arc because the y axis is added directly to both sides of the robot evenly. I hope that helps. |
Re: Default code question
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. Code:
p1_x = 255 - p1_y;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 Code:
p1_x = 255 - p1_y;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, Code:
pwm13 = pwm14 = Limit_Mix(2000 + p1_y + p1_x - 127);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: Code:
pwm13 = pwm14 = Limit_Mix( 2000 + p1_y + p1_x - 127);Roger. |
Re: Default code question
The two equations that combine the X and Y values look confusing because they're simplified for space. There's actually some theory behind it. The un-simplified version looks like this.
Code:
pwm13 = pwm14 = Limit_Mix(2000 + (p1_y - 127) + (p1_x - 127) + 127);1: subtract 127 from the joystick values, so that the range is -127 to 127, rather than 0 to 254. 2: The theory behind the equation is that as Y changes, the motor outputs increase or decrease with each other. As X changes, the motor outputs become farther apart. Think about it. If Y increases, increase the output to both motors, to make them go forward. If X increases (left turn) you put one wheel forward, and the other in reverse. Thus, the basic idea of step 2 is that one wheel is Y + X, and the other is Y - X. 3: Add 127 to the final result, to make the range 0 to 254 once again. And as for the one reversed motor, we usually just wire it in reverse, rather than make the code more complicated. For one, our main programming mentor would (bleep) about it if the code wasn't exactly perfect, and it makes it that much easier to understand months later. |
Re: Default code question
Thanks for the explaination, Jake. I probably would of figured it out eventually, and may try again to work it out in my mind.
Quote:
Besides, that's what comments are for. Months later? Try years. Or just an hour later. Code:
//Put modified joystick numbers into the four motors |
| All times are GMT -5. The time now is 21:07. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi