|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Need help with PWM 1-2ms pulse control
Hey everyone,
How do I make my analog control the PWM outputs? I have a sliding pot that I wil be using, and I want the pwm to pulse 1.5ms in neutral when the slider is near the middle, all the way up for 255 and bottom with a 0. Anywhere in between becomes variable speed. Im new to programming, so any help would be awesome. I just want to be able to make my servo adjust to my analog input. Much like a RC transmitter and receiver. P.S. I am using the old smaller IFI Mini robot controller. |
|
#2
|
|||||
|
|||||
|
Re: Need help with PWM 1-2ms pulse control
The old EDU robot controller has analog inputs with values from 0 to 1023.
The pwm outputs are sent values from 0 to 254. So just scale the input to match the output like so Code:
pwm01 = (long) Get_Analog_Value(rc_ana_in01) * 254 / 1023; Last edited by Mark McLeod : 30-04-2008 at 15:17. |
|
#3
|
|||
|
|||
|
Re: Need help with PWM 1-2ms pulse control
Quote:
Code:
pwm01 = (char) (((long) Get_Analog_Value(rc_ana_in01)) * 254L / 1023L); |
|
#4
|
|||||
|
|||||
|
Re: Need help with PWM 1-2ms pulse control
Sorry about that
I corrected my post, but took the simplest route to make the calculation happen as a long. Implied typecasts occur for the other parts. Explicit typecasting all around is safer though. Thanks! Last edited by Mark McLeod : 30-04-2008 at 15:21. |
|
#5
|
||||
|
||||
|
Re: Need help with PWM 1-2ms pulse control
Quote:
Code:
pwm01 = Get_Analog_Value(rc_ana_in01)/4; |
|
#6
|
|||||
|
|||||
|
Re: Need help with PWM 1-2ms pulse control
Quote:
![]() Last edited by Mark McLeod : 30-04-2008 at 15:52. |
|
#7
|
||||
|
||||
|
Re: Need help with PWM 1-2ms pulse control
Um...im getting an analog value from 0 - 22. or 0-23, is this right? I thought it was supposed to be from 0 to 254?
|
|
#8
|
|||||
|
|||||
|
Re: Need help with PWM 1-2ms pulse control
Quote:
The direct analog inputs on the EDU controller have 10-bit resolution, so you should see 0-1023 returned as values. If you are talking about the analog input from a radio transmitter joystick, then that value is indeed 0-254. How exactly are you printf-ing the value? There may be a simple error there. At least that's where I'd check first. I assume it looks something similar to: Code:
printf("Analog input = %d\r\n", (int) Get_Analog_Value(rc_ana_in01));
Last edited by Mark McLeod : 30-04-2008 at 16:12. |
|
#9
|
|||
|
|||
|
Re: Need help with PWM 1-2ms pulse control
What's the range of resistance of the pot?
|
|
#10
|
||||
|
||||
|
Re: Need help with PWM 1-2ms pulse control
I did mine like this:
printf("Analog In = %d\n", Get_Analog_Value(rc_ana_in01)); But thats right though, the readings im getting are still 0-22. could it be that I've got the pot hooked up wrong? I have it connected to ground and signal. Its a 10k pot, and I tested it on the multimeter. Could it be something in the user_initialization? Im using the EDU Default code right now to debug this problem. Set_Number_of_Analog_Channels(TWO_ANALOG); Last edited by Generalx5 : 30-04-2008 at 16:28. Reason: Yes, the ini file is set up with 2 analog inputs. Default |
|
#11
|
|||||
|
|||||
|
Re: Need help with PWM 1-2ms pulse control
Here's how the analog should be hooked up, so that's one problem.
This is from the IFI EDU guide: http://www.ifirobotics.com/docs/lega...2004-mar-1.pdf Pots are hooked up the way you have yours on the FRC Operator Interface, but not on the robot controller, so it can be confusing. P.S. Minor notes: I believe the 2004 EDU version of printf expected signed ints as inputs. Get_Analog_Value is defined as an unsigned int, so as a matter of principle I'd typecast it when printing it, however, with such a low value you won't see any difference. Felix is refering to the fact that the EDU analog inputs are optimized for 100k pots, but while using a 10k pot would affect your full range somewhat, you shouldn't really notice. Last edited by Mark McLeod : 30-04-2008 at 16:58. |
|
#12
|
||||
|
||||
|
Re: Need help with PWM 1-2ms pulse control
Yeap, I have it done correctly, With the analog input, the signal pin is always logic high, and all I did was plug in a var pot from ground to the signal pin. Is as if you plugged in a solid resistor in its place.
Im sure its done right, as I have used this pot on other robots. So what could be the problem? Is it maybe because Im using V7.5 MPLAB? Could that be it? I tried 7.2 but it wouldnt open any projects. |
|
#13
|
||||
|
||||
|
Re: Need help with PWM 1-2ms pulse control
Well. Forget about that, I dont know why, but im leaving it as:
pwm01 = (long) Get_Analog_Value(rc_ana_in01) * 254 / 22; This actually works out to 0 being full reverse, and 254 being full forward, and 127 bing neutral. My analog inputs read 0 - 22 for some reason. Thats the easy part, heres the harder part. What do I need to do to make the PWM outputs respond like this: ![]() So set A and Set B represents the PWM values, it can be either one as a result of a simple switch. And according to the adjustments on the pot, the speed on the servo varies in equal magnitude in either direction. If the pot is moved towards the top, The servo can be spining real fast depending on the switch. if the pot is near the bottom but not dead bottom. The servos can spin slowly in either direction, again, depending on the switch. Has anyone attemped this before? Im totally clueless to where to start with this. Last edited by Generalx5 : 30-04-2008 at 17:52. |
|
#14
|
|||||
|
|||||
|
Re: Need help with PWM 1-2ms pulse control
Normally the signal pin measures power coming in from the +5v power pin not flowing out to ground. (Taking Ben Franklin's view of electricity)
As long as you understand that your description of how you wired the pot is incorrect for normal operation, but you're happy with it then there's no problem. You've just wired it as a voltage divider that's all. I just don't want anyone else reading this to get the wrong idea though. If you want only the seven specific values in Set A or B then you'd use a lookup table or nested If statements. If you want variable speeds then just map 0-22 to the corresponding range of pwm values, e.g., 0-22 mapped to 0-127 for Set B. For a lookup table, you create an array with one entry for each of the possible 23 values that your pot could be. Then you pull values from the array using the analog input as an index. Code:
static unsigned int pwm_setB[23]={0,0,0,25,25,25,47,47,47,64,64,64,80,80,80,100,100,100,127,127,127};
pwm01=pwm_setB[Get_Analog_Value(rc_ana_in01)]; // If Set B
Code:
unsigned int value; value = Get_Analog_Value(rc_ana_in01); if (value < 3) pwm01 = 0; else if (value < 6) pwm01 = 25; //etc. Last edited by Mark McLeod : 30-04-2008 at 20:43. Reason: Ben Franklin |
|
#15
|
|||||
|
|||||
|
Re: Need help with PWM 1-2ms pulse control
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help with operator control | d25inferno | Programming | 1 | 18-02-2008 11:36 |
| we need help with PWM problem in autonomous | steve d | Programming | 7 | 03-02-2008 12:46 |
| Help with automatic relay control | DRH2o | Programming | 5 | 23-01-2008 18:13 |
| We need help with joystick control | Team ICE #1611 | Control System | 4 | 24-01-2005 15:57 |
| hey need some help with writing a code please help me here | magical hands | Programming | 9 | 01-01-2004 21:46 |