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.

void Chassis::SixDrive(std::shared_ptr<Joystick>& stickPosition){  
	tankdrive->ArcadeDrive(stickPosition->GetX(), stickPosition->GetZ());
}
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

std::shared_ptr<Joystick> OI::getDriveJoystick() {
   return driveJoystick;
}

Thank you!

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.

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::oi->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

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

void Chassis::SixDrive(Joystick driveStick);

This will not work since the Joystick class does not have a copy constructor.

Joystick (const Joystick &)=delete

You should change the function’s parameter type back to shared_ptr and remove the (Joystick) cast where you’re calling that function.

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


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.

Get rid of the &. I think everything else should work.

Thanx Joe, that was it. If you ever tire of LA and want to move to Trout Lake, Wa, you would be most welcome.