View Single Post
  #18   Spotlight this post!  
Unread 01-05-2008, 09:10
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,809
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Need help with PWM 1-2ms pulse control

Quote:
Originally Posted by Generalx5 View Post
....now into the problem with the casting Set A and Set B. Im not sure what you mean by casting 0 - 126 to Set A and casting 127 - 254 to Set B.

Do you mean that When the switch is set to A side, it will only activate PWM pulses between 0 - 126? and the same with side B ( 127-254)?

How should I seperate the pulses into two groups? Ive only been able to get 1 side of it working. I doubled the numerical value of the pot and that gave me the ability to control PWM pulses between 0 - 127. So it would look something like this:

Before pwm01 = (long) Get_Analog_Value(rc_ana_in01) * 254 / 1024;

After pwm01 = (long) Get_Analog_Value(rc_ana_in01) * 254 / 2048;
I think you're a programmer now and an electrician, you're just learning more stuff.

James' suggestions get into some interesting low-level programming tricks that you probably want to bypass for now.
The same with the other math shortcuts suggested. Understand how you map from one range to another then you can play with shortcuts. (Nobody likes long division...)

You already mapped one range into another when you took the analog 0-1023 and mapped it into the range 0-254.
It's the same for going to the range 0-127 or 127-254 or any other range of data.

It's just a ratio: analog input / analog full range = pwm output / pwm full range
or
input/1024 = pwm01 / 254

With a little math manipulation that ratio can be expressed as:
pwm01 = (analog input) * 254 / 1024
where the 254 is your full pwm range (the range you want to end up at) and 1024 is the full analog input range (the range you start with).
That's why replacing 1024 with 22 worked for you earlier. Your full range then was just 22.

Instead of your answer:
After pwm01 = (long) Get_Analog_Value(rc_ana_in01) * 254 / 2048;

Keep the ranges you are working with (starting with a range of 1024 and ending up with a range of 127) and you'll get a more understandable result:
After pwm01 = (long) Get_Analog_Value(rc_ana_in01) * 127 / 1024;

----------------
Here's the curve ball:
All that works fine for ranges starting at zero, and it's not too hard to work the math around to a range of 127-254 by just adding 127. There is an additional robotics trick here though that throws a wrench into this second range, because 127, in the middle, is neutral.

You should think about how you want your servo to behave when you switch between the two Sets. By just shifting the range up by adding 127 your servo will be at rest when the pot is all the way up, but at full speed when you throw the switch.
You might want to think about having the pot be the same for both sets when it's in one position. That just means inverting the second Set range and making it 254-127 instead. Mapping an analog input of 0 to be 127 for both Sets will have your servo stopped for both Sets when the pot is all the way down, and at full speed in both when it's all the way up.

Care to take a guess on how to invert the range 127-254 to make it 254-127 instead?
You could instead invert 0-127 to make it 127-0 if that's easier to think about. Then add 127 to that.

-------------------------------------
Switches:
Using the default EDU code the first two ports (1 & 2) are analog inputs, the others are mixed digital INPUTS (6,8,10,12,14,16) or OUTPUTS (all the others). You can change these if you want where you saw the TWO_ANALOG setup before.
Make sure you use an INPUT and just hook a switch between the signal and ground (DO NOT use the center +5v power pin).
It'll appear in the code with a value of 0 or 1 and the code to check it looks like:
Code:
pwm01 = (long) Get_Analog_Value(rc_ana_in01) * 127 / 1024;  // It's going to end up in a range of 127 for either Set A or B
 
if (rc_dig_in06 == 1)
{
    // Invert the value of pwm01 and move the range to 254-127
}
else
{
    // Leave the range as it is
}
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 02-05-2008 at 08:35. Reason: Fiddling