Go to Post Yes, I think we can all agree that bad people suck. - Joe Johnson [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 01-17-2016, 07:54 PM
rod@3711 rod@3711 is offline
Registered User
AKA: rod nelson
FRC #3711 (Iron Mustangs)
Team Role: Mentor
 
Join Date: May 2014
Rookie Year: 2014
Location: Trout Lake, WA
Posts: 64
rod@3711 is an unknown quantity at this point
Shared pointers? Unique pointers?

We're having some issues getting the code to work. It seems that this year's coding approach is different than last year's.
What are shared pointers for?
The problem arose when we were attempting to get the value of the joystick.

Code:
void Chassis::SixDrive(std::shared_ptr<Joystick>& stickPosition){  
	tankdrive->ArcadeDrive(stickPosition->GetX(), stickPosition->GetZ());
}
Code:
void Drivewithjoystick::Execute() {
	Robot::chassis->SixDrive(Robot::oi->getDriveJoystick());
}
This is where our problem is, when referencing the drivejoystick, it seems we have a shared_ptr issue.

Declaration of the joystick
Code:
std::shared_ptr<Joystick> OI::getDriveJoystick() {
   return driveJoystick;
}

Thank you!
Reply With Quote
  #2   Spotlight this post!  
Unread 01-17-2016, 09:48 PM
heuristics heuristics is offline
Registered User
FRC #3634
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Trumbull, CT
Posts: 21
heuristics is on a distinguished road
Re: Shared pointers? Unique pointers?

Shared pointers assist in memory management. They are implemented using an atomic reference count with the underlying resource (the Joystick object in your case) being shared with each copy of the shared pointer.
When a copy is created, the reference count is increased.
When a copy is destructed, the reference count is decreased. If the reference count reaches zero, the resource is also deleted.

It is better practice to pass your shared pointers by value instead of by reference. This makes it so that each scope owns a copy of your resource. I don't think this will fix your problem, though.

What do you mean by shared_ptr issue? Are you using multiple threads? Where is the initialization/declaration of your driveJoystick variable? It will be difficult to find the problem without seeing more of your code.
Reply With Quote
  #3   Spotlight this post!  
Unread 01-17-2016, 10:59 PM
rod@3711 rod@3711 is offline
Registered User
AKA: rod nelson
FRC #3711 (Iron Mustangs)
Team Role: Mentor
 
Join Date: May 2014
Rookie Year: 2014
Location: Trout Lake, WA
Posts: 64
rod@3711 is an unknown quantity at this point
Re: Shared pointers? Unique pointers?

I had asked a student to post this question, because I had given up and he was willing to work on it with no success.

We have used Robot Builder with enough success last year that we wanted to use it again this year. It is great for organizing command based code and is a tremendous testing tool. Normally you can export the c++ code, build it and deploy it and start testing motors and PID Loops. We just love it.

This year it did not work in Test Mode and with the help of Joe Ross (my hero), we had to make some changes to code generated by Robot Builder. Life was good and we have already been able to test motors and PID loop controls without have to write any C++ code (other than the Joe Ross fixes).

As shown in the original Brad Mill videos and as described in RobotBuilder, "Driving the robot with tank drive and joysticks", we had to write code to drive with joystick. Not being particularly bright, it usually takes us an hour or so to get it to build. This year I spent a couple hours and a student has spent a couple of hours without success. I am pretty sure that this has convinced him to give up the programming career and pursue Fine Arts.

Basically we designed a 2 motor drive subsystem (Chassis), a Joystick and a DriveWithJoystick command in Robot Builder then export (C++). We added the following method to our Chassis subsystem:

// Put methods for controlling this subsystem
// here. Call these from Commands.
void Chassis::SixDrive(Joystick driveStick){ // %seb17-1
tankdrive->ArcadeDrive(driveStick);

With a declaration in Chassis.h.

Then we added this code to DriveWithJoyStick command:

void Drivewithjoystick::Execute() {

Robot::chassis->SixDrive((Joystick)(Robot:i->getDriveJoystick()));
}

This gives us a Red X which displays the message:

Multiple markers at this line
- candidate is:
- no matching function for call to 'Chassis::SixDrive (std::shared_ptr<Joystick>'

So we tried random things for several hours, then our X student programmer posted the beginning of this thread.

Any help may get him to rethink his Fine Arts goal.

Thanx

Last edited by rod@3711 : 01-17-2016 at 11:02 PM.
Reply With Quote
  #4   Spotlight this post!  
Unread 01-17-2016, 11:38 PM
heuristics heuristics is offline
Registered User
FRC #3634
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Trumbull, CT
Posts: 21
heuristics is on a distinguished road
Re: Shared pointers? Unique pointers?

The error message you're seeing tells me that you're not passing the correct type as a parameter to your function.
You still haven't posted the declaration of driveJoystick so I am assuming its type is shared_ptr<Joystick>. That should be fine.

It looks like you've changed the parameters of this function
Code:
void Chassis::SixDrive(Joystick driveStick);
This will not work since the Joystick class does not have a copy constructor.
Code:
Joystick (const Joystick &)=delete
You should change the function's parameter type back to shared_ptr<Joystick> and remove the (Joystick) cast where you're calling that function.

Alternatively, you could change your OI::getDriveJoystick() to this:

Code:
Joystick& OI::getDriveJoystick() {
   return *driveJoystick;
}
Then just always pass your Joystick around as a Joystick reference type (Joystick&). This will only work if you can guarantee that the Joystick object won't be deleted somewhere else while being used by the caller (which seems unlikely).

It would be much easier to diagnose if you could post compiler output and put all of your current code on pastebin or something.
Reply With Quote
  #5   Spotlight this post!  
Unread 01-18-2016, 12:33 AM
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,547
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: Shared pointers? Unique pointers?

Quote:
Originally Posted by rod@3711 View Post
The problem arose when we were attempting to get the value of the joystick.

Code:
void Chassis::SixDrive(std::shared_ptr<Joystick>& stickPosition){  
	tankdrive->ArcadeDrive(stickPosition->GetX(), stickPosition->GetZ());
}
Get rid of the &. I think everything else should work.
Reply With Quote
  #6   Spotlight this post!  
Unread 01-18-2016, 10:22 AM
rod@3711 rod@3711 is offline
Registered User
AKA: rod nelson
FRC #3711 (Iron Mustangs)
Team Role: Mentor
 
Join Date: May 2014
Rookie Year: 2014
Location: Trout Lake, WA
Posts: 64
rod@3711 is an unknown quantity at this point
Re: Shared pointers? Unique pointers?

Thanx Joe, that was it. If you ever tire of LA and want to move to Trout Lake, Wa, you would be most welcome.
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 09:16 AM.

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