Go to Post Free time? You mean the time it takes the code to build? - axiomofdarkness [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 05-05-2016, 16:57
team-4480's Avatar
team-4480 team-4480 is offline
Debug? What's that?
FRC #4480
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Minnesooota
Posts: 222
team-4480 will become famous soon enoughteam-4480 will become famous soon enough
Set Motor position with Encoder?

Hi,

I tried searching this problem all over and was surprised that I couldn't find anything. Maybe I had the wrong keywords or something....

Anyways, I want to be able to control our PG71 with the included encoder with servo like control. On a servo, you can set it to go to angle 162 and it would be really accurate. I would use a servo, but we need a lot of torque and the servos that are legal are not even close to powerful enough. So basically, I want to be able to say to the motor, go to position 5 and it would go there accurately each time.

From researching this, I did find in FTC they have a "setTargetPosition" for their encoders, but I couldn't find any information on the FRC documentation of a similar function. Any help would be greatly appreciated!
__________________
#Python4Life
Reply With Quote
  #2   Spotlight this post!  
Unread 05-05-2016, 17:10
aphelps231's Avatar
aphelps231 aphelps231 is offline
Andrew Phelps
FRC #1318 (Issaquah Robotics Society)
Team Role: Mechanical
 
Join Date: Mar 2014
Rookie Year: 2012
Location: United States
Posts: 77
aphelps231 has a spectacular aura aboutaphelps231 has a spectacular aura about
Re: Set Motor position with Encoder?

You can't do this without the use of some other kind of sensor, as the encoder value resets to zero when the code is rebooted.

What you could do, and many teams did in the past with Recycle Rush elevators, is use a limit switch that resets the encoder count to zero (wherever the arm is when it hits the switch would theoretically be zero degrees), and use closed loop control to move and hold the arm at a certain number of encoder ticks.

Or if you're always going to be booting the robot up with the arm at zero degrees, you wouldn't even need a limit switch, just a way to reposition it to the same place every time you want to boot or reboot the robot.
__________________

team1318.org

2016: Flight Director, Alliance/Match Strategy
2015: Mechanical Lead
2013-2014: Fabrication

2016: 2x district finalists, Quality, DCMP Entrepreneurship Awards
2015: 3x district winners, Innovation in Control, Creativity, Team Spirit awards
2014: Galileo Division finalists, PNW District Championship winners, 1x district winners, District Chairman's, Innovation in Control, Entrpreneurship awards
2013: Entrepreneurship award
Reply With Quote
  #3   Spotlight this post!  
Unread 05-05-2016, 17:14
Knufire Knufire is offline
Rose-Hulman Institute of Technology
no team
Team Role: College Student
 
Join Date: Sep 2009
Rookie Year: 2010
Location: Terre Haute, IN
Posts: 740
Knufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond repute
Re: Set Motor position with Encoder?

Quote:
Originally Posted by team-4480 View Post
Hi,

I tried searching this problem all over and was surprised that I couldn't find anything. Maybe I had the wrong keywords or something....

Anyways, I want to be able to control our PG71 with the included encoder with servo like control. On a servo, you can set it to go to angle 162 and it would be really accurate. I would use a servo, but we need a lot of torque and the servos that are legal are not even close to powerful enough. So basically, I want to be able to say to the motor, go to position 5 and it would go there accurately each time.

From researching this, I did find in FTC they have a "setTargetPosition" for their encoders, but I couldn't find any information on the FRC documentation of a similar function. Any help would be greatly appreciated!

There are two sides to this, the sensor side and the programming side.

Sensor side
---------------
You won't be able to do this solely with the PG71. The encoder on the PG71 is an incremental encoder, it only measures how much the shaft has moved since the robot has turned on. You can use this to tell the motor "turn X degrees", but you cannot tell the motor to "go to angle Y" because it has no sense of an absolute 0. 0 is wherever it was when the robot turned on.

In order to resolve this, you have two options. The first is to add some sort of switch somewhere along the mechanisms path. This way, you know that whenever the arm hits the switch, it's at x angle. Now since you know that specific angle, and how much it has moved from that angle, you know exactly where the arm is at.

The other solution would be to use an absolute encoder, such as http://www.andymark.com/product-p/am-2899.htm. This type of encoder keeps the same zero position, even though robot reboots. If you turn the robot off with the arm at 60 degrees, when you turn the robot back on, the sensor will still report 60 degrees (instead of zero like the built-in PG71 encoder would).

Programming side
-----------------------
You will now need to use the encoder in some sort of feedback loop to control the mechanism. One of the simplest forms of this is a bang-bang controller. Here's some psuedocode.

Code:
if (armAngle > target) {
   setMotor(-1);
}
else if (armAngle < target) {
   setMotor(1); 
}
else {
   setMotor(0); 
}
Now obviously this would cause the arm to rapidly oscillate around the target value. However, you can see the concept; we are choosing what value to send the motor based on the value of the encoder. The most common type of algorithm to use to control the position of something is a PID loop. This is what the servo is doing internally to control it's position. PID loops look something like this.

Code:
//These are constants
Kp = xxx.xxx;
Ki = yyy.yyy;
Kd = zzz.zzz;

lastError = 0;
sumOfErrors = 0; 

while (error > tolerance) {
   error = position - target; 
   P = Kp * error;
   I = Ki * sumOfErrors;
   D = Kd * (error - lastError); 

   setMotor(P + I + D); 

   lastError = error; 
   sumOfErrors = sumOfErrors + error; 
}
There should be a PID function built into the WPI library. This doesn't really work straight out of the box, you'll need to tune those initial constants at the beginning, as they will be unique to the mechanical system. Some of them might be zero as well (just running P, PI, PD, etc).
__________________
Team 469: 2010 - 2013
Team 5188: 2014 - 2016
NAR (VEX U): 2014 - Present

Last edited by Knufire : 05-05-2016 at 17:16.
Reply With Quote
  #4   Spotlight this post!  
Unread 05-05-2016, 17:15
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Set Motor position with Encoder?

Quote:
Originally Posted by team-4480 View Post
I want to be able to control our PG71 with the included encoder with servo like control.
Yes, you can do that with the hardware you described, but you'll have to have a reference position. If you always boot the robot with the mechanism in a known position (like against a hard stop), that can be your reference. That reference can be any value you choose.

Then you need to do what's called "closed-loop position control".

Most teams use a PID closed-loop controller for this. Try searching for those keywords and you'll get lots of relevant hits.



Last edited by Ether : 05-05-2016 at 17:22.
Reply With Quote
  #5   Spotlight this post!  
Unread 05-05-2016, 17:16
nathannfm's Avatar
nathannfm nathannfm is online now
Registered User
AKA: Nathan
FRC #3940
Team Role: Mentor
 
Join Date: Jun 2010
Rookie Year: 2005
Location: Kokomo, IN
Posts: 331
nathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant futurenathannfm has a brilliant future
Re: Set Motor position with Encoder?

This is a common need for swerve drives, I'm not a programmer at all but if you can find some swerve drive code (I know FRC 1640 posts a bunch of swerve drive stuff) you can probably figure out how they do it. The AndyMark swerve module actually uses this exact motor for steering.
__________________

[2016-20??]: Mentor: FRC Team 3940"CyberTooth"
[2013-2016]: Mentor: FRC Team 365 "MOE"
[2012-2013]: Mentor: FRC Team 3929 "Atomic Dragons"
[2011-2012]: Mentor: FRC Team 365 "MOE"
[2008-2011]: Student: FRC Team 365 "MOE"
[2007-2008]: Student: FTC Team 365 "MOE"
[2005-2007]: Student: FLL Team "The MOEstangs"
Reply With Quote
  #6   Spotlight this post!  
Unread 05-05-2016, 17:20
Knufire Knufire is offline
Rose-Hulman Institute of Technology
no team
Team Role: College Student
 
Join Date: Sep 2009
Rookie Year: 2010
Location: Terre Haute, IN
Posts: 740
Knufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond reputeKnufire has a reputation beyond repute
Re: Set Motor position with Encoder?

Quote:
Originally Posted by nathannfm View Post
This is a common need for swerve drives, I'm not a programmer at all but if you can find some swerve drive code (I know FRC 1640 posts a bunch of swerve drive stuff) you can probably figure out how they do it. The AndyMark swerve module actually uses this exact motor for steering.
Note the following from the Swerve & Steer product page.

Quote:
Originally Posted by http://www.andymark.com/product-p/am-3009.htm
The PG71 has an encoder on the back side of the 775 motor, which can help keep track of the steering location. In order to use this method of closed-loop encoder tracking, use a 3/16" allen wrench to ensure that the motor plate, bearing plate, and gear plate are all aligned before operation. Alternatively, there is a location to mount an absolute encoder (this absolute is not included).
This is the same issue that Ether and I described. Since the encoder is incremental, there is a mechanical feature to make sure that all of your modules are aligned in the same direction at the beginning of the match (so that zero is always in the same place). Alternatively, there are holes on the plate and an additional gear available as a separate purchase to mount the MA3 absolute encoder to the module.
__________________
Team 469: 2010 - 2013
Team 5188: 2014 - 2016
NAR (VEX U): 2014 - Present
Reply With Quote
Reply


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


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

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


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