Go to Post And Dave, although I know you have connections, please don't slow the earth's rotation to get more time on the caption contest. ;) - Billfred [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
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 25-11-2015, 12:25
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Off Season Programming Question: Switch Statements

I am trying to implement a switch statement in auto mode. The code goes to the first statement then never goes to the second statement. Any suggestions?

Code:
#include "WPILib.h"

class RobotDemo : public SimpleRobot
{
	RobotDrive myRobot;
	Talon motor;
	Joystick stick;
	Encoder en;
	PIDController pid;

	int step;


public:
	RobotDemo(void):
		myRobot(7,8),
		motor (2),
		stick(1),
		en(1,2, true,Encoder::k4X),
		pid(.1, 0, .003, &en, &motor)


	{
		myRobot.SetExpiration(0.1);

		en.Start();
		step = 1;


	}


	void Autonomous(void)
	{
		myRobot.SetSafetyEnabled(false);



		switch (step){
			case 1:
				pid.Enable();
				pid.SetTolerance(.1);
				pid.SetOutputRange(-.5, .5);
				pid.SetSetpoint(497);

				if(pid.OnTarget()){
					step++;
				}

				break;

			case 2:
				pid.Enable();
				pid.SetTolerance(.1);
				pid.SetOutputRange(-.25, .25);
				pid.SetSetpoint(800);

				if(pid.OnTarget()){
					step++;
				}

				break;


			default:
				break;
		}

	}


	void OperatorControl(void)
	{


		myRobot.SetSafetyEnabled(false);

		while (IsOperatorControl() && IsEnabled())
		{



			Wait(0.005);
		}
	}


	void Test() {




	}
};

START_ROBOT_CLASS(RobotDemo);
Reply With Quote
  #2   Spotlight this post!  
Unread 25-11-2015, 12:33
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
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: 8,754
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: Off Season Programming Question: Switch Statements

Autonomous only gets called once, so the switch statement only gets executed once. You need to add a loop inside Autonomous not unlike the loop you have in OperatorControl.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
Reply With Quote
  #3   Spotlight this post!  
Unread 25-11-2015, 12:41
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Re: Off Season Programming Question: Switch Statements

Once I add the while loop will the method I am using for switching between case statements work?

Code:
void Autonomous(void)
	{
		myRobot.SetSafetyEnabled(false);

         
         while(IsAutonomous && IsEnabled){
		switch (step){
			case 1:
				pid.Enable();
				pid.SetTolerance(.1);
				pid.SetOutputRange(-.5, .5);
				pid.SetSetpoint(497);

				if(pid.OnTarget()){
					step++;
				}

				break;

			case 2:
				pid.Enable();
				pid.SetTolerance(.1);
				pid.SetOutputRange(-.25, .25);
				pid.SetSetpoint(800);

				if(pid.OnTarget()){
					step++;
				}

				break;


			default:
				break;
		}
            }
	}
Reply With Quote
  #4   Spotlight this post!  
Unread 25-11-2015, 12:44
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,575
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Off Season Programming Question: Switch Statements

Once you have it in a loop, you'd probably do much better not to be constantly resetting the PID parameters while it's enabled. I'd try something more like:

Code:
                    switch (step){
			case 1:
				pid.SetTolerance(.1);
				pid.SetOutputRange(-.5, .5);
				pid.SetSetpoint(497);
				pid.Enable();
                                step++;
				break;

			case 2:
				if(pid.OnTarget()){
					step++;
				}
				break;

			case 3:
                                pid.Disable();
				pid.SetTolerance(.1);
				pid.SetOutputRange(-.25, .25);
				pid.SetSetpoint(800);
				pid.Enable();
                                step++;
				break;

			case 4:
				if(pid.OnTarget()){
					step++;
				}
				break;

			default:
				break;
		}
Or, for brevity at the cost of readability, you could remove the case 4 block and move the case 4 statement up next to the case 2 statement, or remove case 4 entirely and never disable the PID in autonomous if you want to leave it on until the end; in this case, you should put the disable outside the loop.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.

Last edited by GeeTwo : 25-11-2015 at 12:47.
Reply With Quote
  #5   Spotlight this post!  
Unread 25-11-2015, 12:55
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Re: Off Season Programming Question: Switch Statements

Quote:
Originally Posted by GeeTwo View Post
Once you have it in a loop, you'd probably do much better not to be constantly resetting the PID parameters while it's enabled. I'd try something more like:

Code:
                    switch (step){
			case 1:
				pid.SetTolerance(.1);
				pid.SetOutputRange(-.5, .5);
				pid.SetSetpoint(497);
				pid.Enable();
                                step++;
				break;

			case 2:
				if(pid.OnTarget()){
					step++;
				}
				break;

			case 3:
                                pid.Disable();
				pid.SetTolerance(.1);
				pid.SetOutputRange(-.25, .25);
				pid.SetSetpoint(800);
				pid.Enable();
                                step++;
				break;

			case 4:
				if(pid.OnTarget()){
					step++;
				}
				break;

			default:
				break;
		}
Or, for brevity at the cost of readability, you could remove the case 4 block and move the case 4 statement up next to the case 2 statement, or remove case 4 entirely and never disable the PID in autonomous if you want to leave it on until the end; in this case, you should put the disable outside the loop.

When I ran that code it went into the first case statement got to the target then stopped. It doesn't seem to want to go into the second case statement.
Reply With Quote
  #6   Spotlight this post!  
Unread 25-11-2015, 13:20
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,575
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Off Season Programming Question: Switch Statements

Quote:
Originally Posted by tomy View Post
When I ran that code it went into the first case statement got to the target then stopped. It doesn't seem to want to go into the second case statement.
Did you put this in the IsAutonomous && IsEnabled loop?
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #7   Spotlight this post!  
Unread 25-11-2015, 13:24
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Re: Off Season Programming Question: Switch Statements

Quote:
Originally Posted by GeeTwo View Post
Did you put this in the IsAutonomous && IsEnabled loop?


Yes. I figured out the problem and I'm trying to think of a way to fix it.

Code:
case 2:
     if(pid.OnTarget()){
          step++;
     }
     break;
That section of code is not change the value of step. I put "step" on the smart dash board and once it gets to case 2 it doesn't move on to case 3. I also checked the value of the encoder with the smart dashboard and it is getting to get set point.


EDIT: I just put the pid.OnTarget(); on the smart dashboard; it never changes to true. I cant figure out why.

Last edited by tomy : 25-11-2015 at 13:32.
Reply With Quote
  #8   Spotlight this post!  
Unread 25-11-2015, 13:56
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,575
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Off Season Programming Question: Switch Statements

Quote:
Originally Posted by tomy View Post
I just put the pid.OnTarget(); on the smart dashboard; it never changes to true. I cant figure out why.
Have you checked what value is being returned from the sensor? Perhaps it's close enough to the actual value that the applied drive speed is within the deadband of the controller, but the sensor value is not within 0.5.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #9   Spotlight this post!  
Unread 25-11-2015, 14:04
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Re: Off Season Programming Question: Switch Statements

Quote:
Originally Posted by GeeTwo View Post
Have you checked what value is being returned from the sensor? Perhaps it's close enough to the actual value that the applied drive speed is within the deadband of the controller, but the sensor value is not within 0.5.
The sensor value being returned is between 495-497. What do you mean the sensor value is not within 0.5? I thought the tolerance of the setpoint was set by pid.SetTolerance(); and that pid.SetOutPutRange(); was the maximum and minimum speed that you wanted the motor to move.
Reply With Quote
  #10   Spotlight this post!  
Unread 25-11-2015, 15:39
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,575
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Off Season Programming Question: Switch Statements

Quote:
Originally Posted by tomy View Post
The sensor value being returned is between 495-497. What do you mean the sensor value is not within 0.5? I thought the tolerance of the setpoint was set by pid.SetTolerance(); and that pid.SetOutPutRange(); was the maximum and minimum speed that you wanted the motor to move.
IIRC, SetTolerance defines the allowable "plus-or-minus" for onTarget to return true. If your sensor is returning a value smaller than (or perhaps <=) 496.9, onTarget will continue to report false. Expand your tolerance to 2.0 or a bit higher, and this should let you move on.

Edit, I see that Tolerance is a percentage, so your answer must be above 496.5031 to have onTarget return true.

Re-edit: the percentage is a fraction of the range defined in SetInputRange(), which you have never called, so it defaults to zero. I suggest using SetAbsoluteTolerance() with a value of 2.0 or greater.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.

Last edited by GeeTwo : 25-11-2015 at 15:47.
Reply With Quote
  #11   Spotlight this post!  
Unread 25-11-2015, 16:02
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Re: Off Season Programming Question: Switch Statements

That fixed it! Thanks for the help one last question. Shouldn't the input to .SetAbsoluteTolerance() be something like .4 instead of 4? I thought in the code it converted it to a percentage.
Reply With Quote
  #12   Spotlight this post!  
Unread 25-11-2015, 16:12
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,575
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Off Season Programming Question: Switch Statements

SetAbsoluteTolerance() bypasses the percentage calculation, so that you specify the tolerance directly in terms of sensor output. SetTolerance() and SetPercentTolerance() are functionally equivalent and set the tolerance to a percentage of the range specified in the latest call to SetOutputRange(). Since you never called SetOutputRange, my reading of the source code makes me believe that these default to zero, so SetTolerance() will always produce a zero tolerance if SetOutputRange() has not been called.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
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 14:04.

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