Go to Post Note that these comments are based on the 2006 FRC game rules, and they may or may not apply to the 2007 game. Your mileage may vary. - dlavery [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 Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 04-02-2015, 18:46
DrOmni9885's Avatar
DrOmni9885 DrOmni9885 is offline
Registered User
FRC #1595 (Dragons)
Team Role: Programmer
 
Join Date: Feb 2013
Rookie Year: 2012
Location: Spokane, WA
Posts: 8
DrOmni9885 is an unknown quantity at this point
Child function problems

My goal right now is to put everything in our main code (west coast, motor automation, etc.) into child functions in the IterativeRobot.h header file. However, the CANTalon and Talon instances, when initialized within the function itself, executes over and over again as the main TeleopPeriodic section in the main program loops and therefore starts and shuts off the motor over and over again. Here's the code:

Code:
	
int motorAutomationEncoder(double encoderDist,
				           double encoderDistThresholdMax,
					   double encoderDistThresholdMin,
					   const int motorPort,
					   double motorSpeed)
	{
		CANTalon motor(motorPort);
		if(encoderDist > encoderDistThresholdMax) {
			motor.Set(0.0);
		}
		else if(encoderDist <= encoderDistThresholdMax && encoderDist >= encoderDistThresholdMin) {
			motor.Set(motorSpeed);
		}
		else if(encoderDist < encoderDistThresholdMin) {
			motor.Set(0.0);
		}
		SmartDashboard::PutNumber('current motor value', motor.Get());
	}
I'd appreciate any help. Thanks!
Reply With Quote
  #2   Spotlight this post!  
Unread 04-02-2015, 18:54
Ben Wolsieffer Ben Wolsieffer is offline
Dartmouth 2020
AKA: lopsided98
FRC #2084 (Robots by the C)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Manchester, MA (Hanover, NH)
Posts: 520
Ben Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud of
Re: Child function problems

Quote:
Originally Posted by DrOmni9885 View Post
CANTalon and Talon instances, when initialized within the function itself, executes over and over again as the main TeleopPeriodic section in the main program loops
You've basically explained the cause of the problem yourself. You need to declare the Talons as member variables of the class and initialize them in the function that starts your robot code. In other words, you need to move the initialization out of that function and into on that is only executed once. Its hard to be more specific without more code, but I hope this helps.
__________________



2016 North Shore District - Semifinalists and Excellence in Engineering Award
2015 Northeastern University District - Semifinalists and Creativity Award
2014 Granite State District - Semifinalists and Innovation in Control Award
2012 Boston Regional - Finalists
Reply With Quote
  #3   Spotlight this post!  
Unread 04-02-2015, 19:05
bob.wolff68's Avatar
bob.wolff68 bob.wolff68 is offline
Da' Mentor Man
FRC #1967
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2007
Location: United States
Posts: 157
bob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nice
Re: Child function problems

I agree with lopsided. I'll add a few things as well.
* As a bug/nit - I dont see encoderDist being 'set' or assigned or incremented anywhere. Maybe outside the scope of what you pasted in. But without...it'll not operate as you intend.
* You talk about putting code into the .h header file. Header files are not the best place to put implementations (code). This is because there are other files which may EACH #include such a header file. When it gets included by multiple files, the implementation will be done multiple times and you'll at least get strange errors from the compiler which may be very non-obvious how to fix. Put implementation/code in .cpp files only.

bob
__________________
~~~~~~~~~~~~~~~~~~~
Bob Wolff - Software from the old-school
Mentor / C / C++ guy
Team 1967 - The Janksters - San Jose, CA
Reply With Quote
  #4   Spotlight this post!  
Unread 04-02-2015, 19:14
Ben Wolsieffer Ben Wolsieffer is offline
Dartmouth 2020
AKA: lopsided98
FRC #2084 (Robots by the C)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Manchester, MA (Hanover, NH)
Posts: 520
Ben Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud of
Re: Child function problems

Quote:
Originally Posted by bob.wolff68 View Post
You talk about putting code into the .h header file. Header files are not the best place to put implementations (code). This is because there are other files which may EACH #include such a header file. When it gets included by multiple files, the implementation will be done multiple times and you'll at least get strange errors from the compiler which may be very non-obvious how to fix. Put implementation/code in .cpp files only.
Just a side note: It is not always bad to put implementation in a header, as long as you use include guards. The real problem is that every time you make a change to the implementation in the header, all files that include it need to be recompiled, which can take a long time in large projects. There are some times when you are required to put implementation in a header, such as when you are using templates, but that is another topic altogether.
__________________



2016 North Shore District - Semifinalists and Excellence in Engineering Award
2015 Northeastern University District - Semifinalists and Creativity Award
2014 Granite State District - Semifinalists and Innovation in Control Award
2012 Boston Regional - Finalists
Reply With Quote
  #5   Spotlight this post!  
Unread 04-02-2015, 19:59
DrOmni9885's Avatar
DrOmni9885 DrOmni9885 is offline
Registered User
FRC #1595 (Dragons)
Team Role: Programmer
 
Join Date: Feb 2013
Rookie Year: 2012
Location: Spokane, WA
Posts: 8
DrOmni9885 is an unknown quantity at this point
Re: Child function problems

Quote:
Originally Posted by bob.wolff68 View Post
I agree with lopsided. I'll add a few things as well.
* As a bug/nit - I dont see encoderDist being 'set' or assigned or incremented anywhere. Maybe outside the scope of what you pasted in. But without...it'll not operate as you intend.
* You talk about putting code into the .h header file. Header files are not the best place to put implementations (code). This is because there are other files which may EACH #include such a header file. When it gets included by multiple files, the implementation will be done multiple times and you'll at least get strange errors from the compiler which may be very non-obvious how to fix. Put implementation/code in .cpp files only.

bob
In my actual program, Robot.cpp, I currently have this line:

Code:
motorAutomationEncoder(e_encoder_dist, 200, -100, fan_motor_SRX, 1.0);
In other words, I have a variable in Robot.cpp, e_encoder_dist, that reads the distance returned by the encoder. Currently both the header file and the program itself give me the error "use of deleted function CANTalon::CANTalon(const CANTalon&)", and after I changed my function code to this:

Code:
	
int motorAutomationEncoder(double encoderDist,
							   double encoderDistThresholdMax,
							   double encoderDistThresholdMin,
							   CANTalon motor,
							   double motorSpeed)


	{
		if(encoderDist > encoderDistThresholdMax) {
			motor.Set(0.0);
		}
		else if(encoderDist <= encoderDistThresholdMax && encoderDist >= encoderDistThresholdMin) {
			motor.Set(motorSpeed);
		}
		else if(encoderDist < encoderDistThresholdMin) {
			motor.Set(0.0);
		}
	}
...the constantly-shutting-off-and-on problem seems to have disappeared.
Reply With Quote
  #6   Spotlight this post!  
Unread 04-02-2015, 20:00
DrOmni9885's Avatar
DrOmni9885 DrOmni9885 is offline
Registered User
FRC #1595 (Dragons)
Team Role: Programmer
 
Join Date: Feb 2013
Rookie Year: 2012
Location: Spokane, WA
Posts: 8
DrOmni9885 is an unknown quantity at this point
Re: Child function problems

Quote:
Originally Posted by lopsided98 View Post
You've basically explained the cause of the problem yourself. You need to declare the Talons as member variables of the class and initialize them in the function that starts your robot code. In other words, you need to move the initialization out of that function and into on that is only executed once. Its hard to be more specific without more code, but I hope this helps.
In other words there's no way I can write a child function that dictates the movement of motors? That's... to bad I guess. I'll see what I can do. Thanks for your help.
Reply With Quote
  #7   Spotlight this post!  
Unread 04-02-2015, 20:04
Ben Wolsieffer Ben Wolsieffer is offline
Dartmouth 2020
AKA: lopsided98
FRC #2084 (Robots by the C)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Manchester, MA (Hanover, NH)
Posts: 520
Ben Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud of
Re: Child function problems

Quote:
Originally Posted by DrOmni9885 View Post
In other words there's no way I can write a child function that dictates the movement of motors? That's... to bad I guess. I'll see what I can do. Thanks for your help.
You can, but you just can't create the motor controller within that function. You need to create it when the robot starts, but then you can set its speed in the function.
__________________



2016 North Shore District - Semifinalists and Excellence in Engineering Award
2015 Northeastern University District - Semifinalists and Creativity Award
2014 Granite State District - Semifinalists and Innovation in Control Award
2012 Boston Regional - Finalists
Reply With Quote
  #8   Spotlight this post!  
Unread 04-02-2015, 20:05
DrOmni9885's Avatar
DrOmni9885 DrOmni9885 is offline
Registered User
FRC #1595 (Dragons)
Team Role: Programmer
 
Join Date: Feb 2013
Rookie Year: 2012
Location: Spokane, WA
Posts: 8
DrOmni9885 is an unknown quantity at this point
Re: Child function problems

Quote:
Originally Posted by lopsided98 View Post
You can, but you just can't create the motor controller within that function. You need to create it when the robot starts, but then you can set its speed in the function.
Will this work

Code:
	int motorAutomationEncoder(double encoderDist,
							   double encoderDistThresholdMax,
							   double encoderDistThresholdMin,
							   CANTalon motor,
							   double motorSpeed)


	{
		if(encoderDist > encoderDistThresholdMax) {
			motor.Set(0.0);
		}
		else if(encoderDist <= encoderDistThresholdMax && encoderDist >= encoderDistThresholdMin) {
			motor.Set(motorSpeed);
		}
		else if(encoderDist < encoderDistThresholdMin) {
			motor.Set(0.0);
		}
	}
As aforementioned, the start-stop loop seems to have disappeared.
__________________
Code guy.
Reply With Quote
  #9   Spotlight this post!  
Unread 05-02-2015, 00:48
bob.wolff68's Avatar
bob.wolff68 bob.wolff68 is offline
Da' Mentor Man
FRC #1967
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2007
Location: United States
Posts: 157
bob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nice
Re: Child function problems

If you want to use your CANTalon object, you should pass it as a pointer .

Code:
CANTalon *pMySpeedController
...
pMySpeedController->Set(x);
...
__________________
~~~~~~~~~~~~~~~~~~~
Bob Wolff - Software from the old-school
Mentor / C / C++ guy
Team 1967 - The Janksters - San Jose, CA
Reply With Quote
  #10   Spotlight this post!  
Unread 05-02-2015, 19:32
DrOmni9885's Avatar
DrOmni9885 DrOmni9885 is offline
Registered User
FRC #1595 (Dragons)
Team Role: Programmer
 
Join Date: Feb 2013
Rookie Year: 2012
Location: Spokane, WA
Posts: 8
DrOmni9885 is an unknown quantity at this point
Re: Child function problems

Quote:
Originally Posted by bob.wolff68 View Post
If you want to use your CANTalon object, you should pass it as a pointer .

Code:
CANTalon *pMySpeedController
...
pMySpeedController->Set(x);
...
This is weird, but whenever I try deploying the code Eclipse tells me there's nothing to build for any project after I added the pointer. Here's the information from the console:

Code:
Info: Nothing to build for Encoder
Info: Nothing to build for Intermediate Vision
Info: Nothing to build for Mecanum Drive
Info: Nothing to build for Motor Control With Encoder
Info: Nothing to build for PDP CAN Monitoring
Info: Nothing to build for Simple Vision
Info: Nothing to build for Solenoids
Info: Nothing to build for [FRC2015_MAINCODE]
Info: Nothing to build for cases code
Info: Nothing to build for example
The project I'm trying to build is [FRC2015_MAINCODE]. Here's the portion of the code which I've changed:

Main program
Code:
...
	CANTalon *fan_motor_SRX;
...
	motorAutomationEncoder(e_encoder_dist, 300, -100, fan_motor_SRX, 1.0);
...
START_ROBOT_CLASS(Robot);
Header file (function):
Code:
	int motorAutomationEncoder(double encoderDist,
							   double encoderDistThresholdMax,
							   double encoderDistThresholdMin,
							   CANTalon *motor,
							   double motorSpeed)


	{
		if(encoderDist > encoderDistThresholdMax) {
			motor->Set(0.0);
		}
		else if(encoderDist <= encoderDistThresholdMax && encoderDist >= encoderDistThresholdMin) {
			motor->Set(motorSpeed);
		}
		else if(encoderDist < encoderDistThresholdMin) {
			motor->Set(0.0);
		}
	}
All other projects work when I try to deploy them. What could be wrong here?...
__________________
Code guy.

Last edited by DrOmni9885 : 05-02-2015 at 19:36. Reason: removed irrelevant sections of code
Reply With Quote
  #11   Spotlight this post!  
Unread 06-02-2015, 18:52
bob.wolff68's Avatar
bob.wolff68 bob.wolff68 is offline
Da' Mentor Man
FRC #1967
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2007
Location: United States
Posts: 157
bob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nice
Re: Child function problems

A couple thoughts...

I teach my students to right-click on the project they want to build (in the left pane) and select "Build Project" ... this way there's no question what was being asked of Eclipse.

I would suggest doing the following to help further diagnose if the above doesn't help (which it probably wont since its really just general)...
- Right click on the project and select 'clean project'
- Verify it's clean by opening up the 'Debug' folder in eclipse for that project and you should NOT see FRCUserProgram which is the final binary output file that gets sent to the roborRIO
- Click on the 'problems' tab in the lower / console area at the bottom of Eclipse.
- Right click on 'Errors' and select 'Delete' - and confirm.
- Right click on 'Warnings' and select 'Delete' - and confirm.
- Confirm there is nothing in the 'problems' tab any longer. This is a bit painful but will make sure you're only looking at NEW problems and not old ones.
- Now right click on the project again and select 'Build Project' ...

After it completes, look in the 'problems' tab for errors and warnings.
- If there are errors/warnings - post them here.
- If there are no errors, take a look again in the Debug folder in the project (left pane) and see if there is a FRCUserProgram (this would indicate a successful compile)
- If there is indeed an FRCUserProgram, then you should be good to right click on the project and select RunAs->WPILibC++Deploy

If you got to the deploy - then check in the lower area for a 'Console' tab which will have the results of the deploy operation.

Once this works well, it shouldn't be so painful as described above. I'm just trying to eliminate any variables I can so that the next step of troubleshooting becomes illuminating of the root cause. Hopefully.

bob
__________________
~~~~~~~~~~~~~~~~~~~
Bob Wolff - Software from the old-school
Mentor / C / C++ guy
Team 1967 - The Janksters - San Jose, CA
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 12:07.

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