Go to Post The minute you get a functioning mechanism start working on a better one - [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

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 03-12-2011, 14:36
Stonemotmot Stonemotmot is offline
Registered User
FRC #0486
 
Join Date: Sep 2011
Location: United States, Pa
Posts: 53
Stonemotmot is an unknown quantity at this point
Using a variable as a class object in Windriver/C++

Hello, I am currently working on streamlining our teams code and was wondering if it is possible to use a variable to reference a specific object.
For Example
Say I named a solenoid object 1 and had a int variable named b that stored 1 could i say
b.Set(true) and have the same result as 1.Set(True)
If it cannot be done this way is there another way to get the same result. The key to what I want is to have a numeric value that relates to a specific object.

Thanks for any help in advance.
  #2   Spotlight this post!  
Unread 03-12-2011, 16:15
connor.worley's Avatar
connor.worley connor.worley is offline
Registered User
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Mar 2011
Rookie Year: 2010
Location: Berkeley/San Diego
Posts: 601
connor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond repute
Re: Using a variable as a class object in Windriver/C++

Try a map.

Code:
#include <map>

...

map<int, Solenoid*> solenoids;
solenoids[1] = new Solenoid(1);

...
__________________
Team 973 (2016-???)
Team 5499 (2015-2016)
Team 254 (2014-2015)

Team 1538 (2011-2014)
2014 Driver (25W 17L 1T)
日本語でOK
  #3   Spotlight this post!  
Unread 03-12-2011, 20:25
theprgramerdude theprgramerdude is offline
WPI Freshman
AKA: Alex
FRC #2503 (Warrior Robotics)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2008
Location: Brainerd, Minnesota
Posts: 347
theprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud of
Re: Using a variable as a class object in Windriver/C++

A map is the only way to "map" (hint hint nudge nudge) integer values to solenoid references.
__________________
Attending: MN Duluth Regional
  #4   Spotlight this post!  
Unread 04-12-2011, 00:05
WizenedEE's Avatar
WizenedEE WizenedEE is offline
Registered User
AKA: Adam
FRC #3238 (Cyborg Ferrets)
Team Role: Leadership
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Anacortes, WA
Posts: 395
WizenedEE is a name known to allWizenedEE is a name known to allWizenedEE is a name known to allWizenedEE is a name known to allWizenedEE is a name known to allWizenedEE is a name known to all
Re: Using a variable as a class object in Windriver/C++

Why doesn't an array work?

Code:
Solenoid* solenoids[3];
solenoid[0] = new Solenoid(5);
//...
  #5   Spotlight this post!  
Unread 04-12-2011, 01:03
theprgramerdude theprgramerdude is offline
WPI Freshman
AKA: Alex
FRC #2503 (Warrior Robotics)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2008
Location: Brainerd, Minnesota
Posts: 347
theprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud of
Re: Using a variable as a class object in Windriver/C++

It does, at the penalty of having a fixed number of spots with fixed names to map to unless additional code is written.

To be clear, I was referring to the data type map, not necessarily the c++ implementation. I can count at least 8 ways in C++ to implement the map with varying degrees of effectiveness, map and hash_map (assuming that library is included in Windriver by default) being the most flexible with mapping names.

Point being, the map structure could accept any valid integer and associate the corresponding solenoid with it; with an array, you are forced to associate a solenoid with a specific value from 0 to size - 1.
__________________
Attending: MN Duluth Regional
  #6   Spotlight this post!  
Unread 04-12-2011, 04:14
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,187
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: Using a variable as a class object in Windriver/C++

Use an array. The amount of solenoids you have is known, so the associative array (map) doesn't add any value.
  #7   Spotlight this post!  
Unread 04-12-2011, 12:36
jhersh jhersh is offline
National Instruments
AKA: Joe Hershberger
FRC #2468 (Appreciate)
Team Role: Mentor
 
Join Date: May 2008
Rookie Year: 1997
Location: Austin, TX
Posts: 1,006
jhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond repute
Re: Using a variable as a class object in Windriver/C++

Quote:
Originally Posted by Stonemotmot View Post
Say I named a solenoid object 1 and had a int variable named b that stored 1 could i say
b.Set(true) and have the same result as 1.Set(True)
Many people are answering your question, but no one has asked why you want to do this. Generally numbers are a poor way to refer to things. Especially if the numbers are arbitrary (don't have any meaning in the mechanism you are controlling).

The best generic way to do this is to simply name variables or pointers for each mechanism and use those to refer to the solenoids or anything else.

Such as:

Code:
Solenoid *leftGripper = new Solenoid(2);
Solenoid *impaler = new Solenoid(3);

impaler->Set(true);
or

Code:
class MyRobot : public IterativeRobot
{
...
Solenoid leftGripper;
Solenoid impaler;
...
	MyRobot() :
		leftGripper(2),
		impaler(3)
	{
	}
...
	void AutonomousInit(void)
	{
		impaler.Set(true);
	}
...
};

Do you have some compelling reason not to refer to each object directly?
  #8   Spotlight this post!  
Unread 05-12-2011, 20:46
WizenedEE's Avatar
WizenedEE WizenedEE is offline
Registered User
AKA: Adam
FRC #3238 (Cyborg Ferrets)
Team Role: Leadership
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Anacortes, WA
Posts: 395
WizenedEE is a name known to allWizenedEE is a name known to allWizenedEE is a name known to allWizenedEE is a name known to allWizenedEE is a name known to allWizenedEE is a name known to all
Re: Using a variable as a class object in Windriver/C++

Quote:
Originally Posted by jhersh View Post
Do you have some compelling reason not to refer to each object directly?
Last year, we wanted to program the robot to set all the solenoids on to power the line tracking sensors. We just looped through each of the ports (it was in labview though).
  #9   Spotlight this post!  
Unread 06-12-2011, 01:37
theprgramerdude theprgramerdude is offline
WPI Freshman
AKA: Alex
FRC #2503 (Warrior Robotics)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2008
Location: Brainerd, Minnesota
Posts: 347
theprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud oftheprgramerdude has much to be proud of
Re: Using a variable as a class object in Windriver/C++

Well, if you had an array of solenoid pointers for this, it'd just be something simple as having:

for(int solenoidPort = 0; 0 < 8; solenoidPort++){
solenoids[solenoidPort]->set(true);
}
__________________
Attending: MN Duluth Regional
  #10   Spotlight this post!  
Unread 07-12-2011, 14:32
Stonemotmot Stonemotmot is offline
Registered User
FRC #0486
 
Join Date: Sep 2011
Location: United States, Pa
Posts: 53
Stonemotmot is an unknown quantity at this point
Re: Using a variable as a class object in Windriver/C++

Thanks for all the imput. I was wondering if any of you could elaborate on how to implement a solenoid array.
  #11   Spotlight this post!  
Unread 07-12-2011, 14:50
connor.worley's Avatar
connor.worley connor.worley is offline
Registered User
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Mar 2011
Rookie Year: 2010
Location: Berkeley/San Diego
Posts: 601
connor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond repute
Re: Using a variable as a class object in Windriver/C++

Quote:
Originally Posted by Stonemotmot View Post
Thanks for all the imput. I was wondering if any of you could elaborate on how to implement a solenoid array.
See WizenedEE's first reply. I think we could provide more useful feedback if you could post your team's code.
__________________
Team 973 (2016-???)
Team 5499 (2015-2016)
Team 254 (2014-2015)

Team 1538 (2011-2014)
2014 Driver (25W 17L 1T)
日本語でOK
  #12   Spotlight this post!  
Unread 07-12-2011, 18:16
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,586
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Using a variable as a class object in Windriver/C++

Quote:
Originally Posted by WizenedEE View Post
Last year, we wanted to program the robot to set all the solenoids on to power the line tracking sensors. We just looped through each of the ports (it was in labview though).
I'm don't really know C++, so I'm not sure if it is accessible, but SolenoidBase has a set which takes a value and mask and writes the whole module at once. For something simple like your use case, I would do something like that (value 0xFF, mask 0xFF).

However, trying to do something like that to for anything less trivial would greatly trade readability and maintainability for compactness, which for FIRST is not something I would try to do.
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


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

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