Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Need help with PWM 1-2ms pulse control (http://www.chiefdelphi.com/forums/showthread.php?t=67397)

Generalx5 30-04-2008 14:51

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.

Mark McLeod 30-04-2008 15:11

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;

tdlrali 30-04-2008 15:14

Re: Need help with PWM 1-2ms pulse control
 
Quote:

Code:

pwm01 = Get_Analog_Value(rc_ana_in01) * 254 / 1023;

That will overflow unless everything is casted as long, like this:

Code:

pwm01 = (char) (((long) Get_Analog_Value(rc_ana_in01)) * 254L / 1023L);

Mark McLeod 30-04-2008 15:17

Re: Need help with PWM 1-2ms pulse control
 
Sorry about that :ahh:
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!

billbo911 30-04-2008 15:47

Re: Need help with PWM 1-2ms pulse control
 
Quote:

Originally Posted by Mark McLeod (Post 744470)
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;

Wouldn't it be just as accurate to do this?

Code:

pwm01 = Get_Analog_Value(rc_ana_in01)/4;

Mark McLeod 30-04-2008 15:50

Re: Need help with PWM 1-2ms pulse control
 
Quote:

Originally Posted by billbo911 (Post 744487)
Wouldn't it be just as accurate to do this?

Code:

pwm01 = Get_Analog_Value(rc_ana_in01)/4;

Yes, but perhaps not as instructive. >>2 ;)

Generalx5 30-04-2008 16:00

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?

Mark McLeod 30-04-2008 16:06

Re: Need help with PWM 1-2ms pulse control
 
Quote:

Originally Posted by Generalx5 (Post 744492)
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?

Nope, not right, so something is odd.
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));
Is the analog input port initialized properly? e.g., Set_Number_of_Analog_Channels(ONE_ANALOG); // one or more

tdlrali 30-04-2008 16:25

Re: Need help with PWM 1-2ms pulse control
 
What's the range of resistance of the pot?

Generalx5 30-04-2008 16:26

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);

Mark McLeod 30-04-2008 16:37

Re: Need help with PWM 1-2ms pulse control
 
1 Attachment(s)
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.

Generalx5 30-04-2008 17:13

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.

Generalx5 30-04-2008 17:48

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.

Mark McLeod 30-04-2008 18:30

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

Nested IFs would look like:
Code:

unsigned int value;
value = Get_Analog_Value(rc_ana_in01);

if (value < 3)
  pwm01 = 0;
else if (value < 6)
  pwm01 = 25;
//etc.

P.S. Be aware that the pot value may jump around a little even if you aren't touching it, so if you're right on the edge, say 3, then you might see the servo twitching between two of your set B speeds.

Alan Anderson 30-04-2008 18:57

Re: Need help with PWM 1-2ms pulse control
 
Quote:

Originally Posted by Generalx5 (Post 744526)
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.

You have not done it correctly. It is not done right. Look at the picture Mark provided. The analog input pin wants you to supply a voltage, but according to your definition you're only supplying a resistance. There is a high-valued resistor connected internally from the analog input pin to +5, so your variable resistance combined with that does end up forming a voltage divider (a maximum 10k ohms gives a maximum output of about 1/50 of full scale). If you want to get the proper results you really need to pay attention to the proper wiring for a potentiometer.


All times are GMT -5. The time now is 18:00.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi