View Full Version : Using a switch to control a PWM
Max Lobovsky
25-02-2004, 10:07
I do know C but i am not directly involved with programming, so i apologize if i don't know some obvious things. We are trying to have two switches move a servo (PWM controlled) between two different values. I assumed this would be as simple as checking the status of the button every iteration of the loop and then assigning a certain value to the PWM via an if statement.
if(p1_sw_some_switch == 1){
pwmxxx=some_value
}
if(p1_sw_some_other_switch == 1) {
pwmxxx=some_other_value
This appears to not be working (ie buttons do not move the servo at all, do the servo does appear to twitch when the OI or the RC is turned on) so my first thought was that maybe the switch takes values other than 0 and 1 like maybe 0 and 255, etc. Any help would be much appreciated
Joe Ross
25-02-2004, 10:39
your code is correct, do you have the backup battery installed?
You may want to do something for you other conditions, though, such as if neither button is pressed or if both buttons are pressed.
Mark McLeod
25-02-2004, 10:42
if(p1_sw_some_switch == 1){
pwmxxx=some_value
}
if(p1_sw_some_other_switch == 1) {
pwmxxx=some_other_value
You have the general idea, however, the joystick buttons have a value of "0" by default, and switch to "1" when pressed. You may be confusing your test by having the later switch override the first switch test.
Try a simpler test like:
if (p2_sw_trig)
pwmxxx = 254;
else
pwmxxx = 0;
The 7.2v backup battery powers the PWM center pin.
[edit] Joe Ross beat me to an answer. Hey, Ken W. my typing must be slowing down!;)
[edit 2] deltacoder corrected my reversal of the joystick button values. (I also get left and right confused). Thanks delta!
Max Lobovsky
25-02-2004, 10:52
Try a simpler test like:
if (p2_sw_trig)
pwmxxx = 254;
else
pwmxxx = 0;
The problem with that is that this is for a transmission shifter so i want one button press to leave the transmission in a certain gear. But yeah, i should defenitley put initialization values and tell drivers not to press both buttons simultaneously. That servo will go nuts...
Phil_Lutz
25-02-2004, 11:05
If the servo is "twitching" just on the startup of the code then you are okay.
This is just the servo going to it's initial position.
If the Servo is always twitching it might be a low backup battery.
For a transmission shifter you are best to have it on one switch.
0=low gear, 1=high gear
Also, make sure to stop the motorfor a few loops before shifting, and start the motors back up (to selected speed) after th3e servo has had a chance to move.
The psuedocode would look like this
// Motor speed 160 In Low Gear
Shift button changed // have static store last value
Store Motor Speed // in static for later use
Stop Motor // 127, neutral
Move Servo // 0-254 or 254-0
count loops for 10 (.2 secs, servo moves full throw in .11)
Start Motor
// Shift on the fly, You got to love it
Setup #defines to track the various points in the shift and switch/case through the shift process.
Hope this helps.
Phil
deltacoder1020
25-02-2004, 11:28
You have the general idea, however, the joystick buttons have a value of "1" by default, and switch to "0" when pressed. You may be confusing your test by having the later switch override the first switch test.
Try a simpler test like:
if (p2_sw_trig)
pwmxxx = 254;
else
pwmxxx = 0;
0 does not evaluate to true - it is the only integer that evaluates to false. the joystick buttons have a value of 0 by default, not 1.
Max Lobovsky
25-02-2004, 11:32
If the servo is "twitching" just on the startup of the code then you are okay.
This is just the servo going to it's initial position.
If the Servo is always twitching it might be a low backup battery.
For a transmission shifter you are best to have it on one switch.
0=low gear, 1=high gear
Also, make sure to stop the motorfor a few loops before shifting, and start the motors back up (to selected speed) after th3e servo has had a chance to move.
The psuedocode would look like this
// Motor speed 160 In Low Gear
Shift button changed // have static store last value
Store Motor Speed // in static for later use
Stop Motor // 127, neutral
Move Servo // 0-254 or 254-0
count loops for 10 (.2 secs, servo moves full throw in .11)
Start Motor
// Shift on the fly, You got to love it
Setup #defines to track the various points in the shift and switch/case through the shift process.
Hope this helps.
Phil
That would be ideal, except im not sure that the motors will be still and if they are moving, even if they are being backdrive, it isnt a good idea to shift. I'm afraid if you put something in like this then someone may try to shift while the robot is being pushed or maybe it will keep rolling for longer than expected, etc. Ideally, you could have the shifter only work if an encoder reports no rotation, but we dont have time for an encoder, so i think we will rely on drivers watching the robot and being smart about shifting. (kinda like not shifting without pressing the clutch...)
scottm87
25-02-2004, 12:14
I haven't used the servos w/ the new control system, but when I have worked with servo's in the past, twitching can also be caused by pushing the servo out of the position you are trying to set it to. The internals of the servo have a potentiometer and simple control circuitry to move the servo to a cooresponding value based on the pot. If you try to move it out of the position, it will try to move back.
Max Lobovsky
25-02-2004, 17:13
your code is correct, do you have the backup battery installed?
You may want to do something for you other conditions, though, such as if neither button is pressed or if both buttons are pressed.
After trying countless things, we still cannot get it to work. We have the latest default and Master code. We have even tried reading the values of the relays assigned the values of the switches we want to use. We just cannot seem to set a value to a PWM on a button press.
Astronouth7303
25-02-2004, 17:15
Does it work with a Victor?
Max Lobovsky
25-02-2004, 17:16
wow, i feel really really dumb, its because we were setting something to 255 not 254, isnt it?
Astronouth7303
25-02-2004, 17:20
wow, i feel really really dumb, its because we were setting something to 255 not 254, isnt it?
That might be it. Don't worry, I would probably make that mistake if I actually had an oportunity...
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.