OCCRA
Go to Post We use the engines of our imagination on the robot all the time. FIRST being an engine of change after all. - Mark McLeod [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Events   CD-Media   CD-Spy   FRC-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 04-30-2008, 01:51 PM
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
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   Spotlight this post!  
Unread 04-30-2008, 02:11 PM
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Itinerant Programmer
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: 7,346
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

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;
__________________
Deduction is limited by knowledge, and knowledge is limited by preconceptions
358 Beta notes & results

Last edited by Mark McLeod : 04-30-2008 at 02:17 PM.
  #3   Spotlight this post!  
Unread 04-30-2008, 02:14 PM
tdlrali tdlrali is offline
Registered User
FRC #0469 (Las Guerrillas)
Team Role: Programmer
 
Join Date: Sep 2006
Rookie Year: 2006
Location: MI
Posts: 377
tdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud of
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);
  #4   Spotlight this post!  
Unread 04-30-2008, 02:17 PM
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Itinerant Programmer
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: 7,346
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

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!
__________________
Deduction is limited by knowledge, and knowledge is limited by preconceptions
358 Beta notes & results

Last edited by Mark McLeod : 04-30-2008 at 02:21 PM.
  #5   Spotlight this post!  
Unread 04-30-2008, 02:47 PM
billbo911's Avatar
billbo911 billbo911 is offline
Registered User
AKA: That's "Mr. Bill"
FRC #2073 (EagleForce)
Team Role: Mentor
 
Join Date: Mar 2005
Rookie Year: 2005
Location: Elk Grove, Ca.
Posts: 1,873
billbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond repute
Re: Need help with PWM 1-2ms pulse control

Quote:
Originally Posted by Mark McLeod View Post
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;
__________________
So long and thanks for all the fish!
CalGames 2009 Semi Finalist, CalGames 2009 Autonomous Champion Award winner
Sacramento 2010 Creativity in Design winner, Sacramento 2010 Quarter finalist, 2010 Madtown Creative Design Award
2011 Sacramento Finalist, 2011 Madtown Engineering Inspiration Award
2012 Sacramento Semi-Finals, 2012 Sacramento Innovation in Control Award, 2012 SVR Judges Award.
2012 CalGames Autonomous Challenge Award winner ($$$).
2013 Pow3rhou53 Pwnage Gracious Professionalism Award.
2014 2X Rockwell Automation: Innovation in Control Award (CVR and SAC). Curie Division, Gracious Professionalism Award.
2014 Capital City Classic Winner AND Runner Up.
  #6   Spotlight this post!  
Unread 04-30-2008, 02:50 PM
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Itinerant Programmer
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: 7,346
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 billbo911 View Post
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
__________________
Deduction is limited by knowledge, and knowledge is limited by preconceptions
358 Beta notes & results

Last edited by Mark McLeod : 04-30-2008 at 02:52 PM.
  #7   Spotlight this post!  
Unread 04-30-2008, 03:00 PM
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
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   Spotlight this post!  
Unread 04-30-2008, 03:06 PM
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Itinerant Programmer
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: 7,346
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
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
__________________
Deduction is limited by knowledge, and knowledge is limited by preconceptions
358 Beta notes & results

Last edited by Mark McLeod : 04-30-2008 at 03:12 PM.
  #9   Spotlight this post!  
Unread 04-30-2008, 03:25 PM
tdlrali tdlrali is offline
Registered User
FRC #0469 (Las Guerrillas)
Team Role: Programmer
 
Join Date: Sep 2006
Rookie Year: 2006
Location: MI
Posts: 377
tdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud of
Re: Need help with PWM 1-2ms pulse control

What's the range of resistance of the pot?
  #10   Spotlight this post!  
Unread 04-30-2008, 03:26 PM
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
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 : 04-30-2008 at 03:28 PM. Reason: Yes, the ini file is set up with 2 analog inputs. Default
  #11   Spotlight this post!  
Unread 04-30-2008, 03:37 PM
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Itinerant Programmer
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: 7,346
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

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.
Attached Thumbnails
Click image for larger version

Name:	EDUanalog.jpg
Views:	69
Size:	27.4 KB
ID:	6623  
__________________
Deduction is limited by knowledge, and knowledge is limited by preconceptions
358 Beta notes & results

Last edited by Mark McLeod : 04-30-2008 at 03:58 PM.
  #12   Spotlight this post!  
Unread 04-30-2008, 04:13 PM
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
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   Spotlight this post!  
Unread 04-30-2008, 04:48 PM
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
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 : 04-30-2008 at 04:52 PM.
  #14   Spotlight this post!  
Unread 04-30-2008, 05:30 PM
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Itinerant Programmer
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: 7,346
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

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.
__________________
Deduction is limited by knowledge, and knowledge is limited by preconceptions
358 Beta notes & results

Last edited by Mark McLeod : 04-30-2008 at 07:43 PM. Reason: Ben Franklin
  #15   Spotlight this post!  
Unread 04-30-2008, 05:57 PM
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 7,847
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Send a message via AIM to Alan Anderson
Re: Need help with PWM 1-2ms pulse control

Quote:
Originally Posted by Generalx5 View Post
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.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with operator control d25inferno Programming 1 02-18-2008 10:36 AM
we need help with PWM problem in autonomous steve d Programming 7 02-03-2008 11:46 AM
Help with automatic relay control DRH2o Programming 5 01-23-2008 05:13 PM
We need help with joystick control Team ICE #1611 Control System 4 01-24-2005 02:57 PM
hey need some help with writing a code please help me here magical hands Programming 9 01-01-2004 08:46 PM


All times are GMT -5. The time now is 01:41 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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