Declaring the AxisCamera as a module level variable.

What would be the proper way to declare the AxisCamera class as a module level so that autonomous and teleop can use the same connection and we don’t have to wait for a connection twice?

AxisCamera &camera = AxisCamera::GetInstance(“10.12.51.11”);

As a member variable to your SimpleRobot class, define AxisCamera& camera; Then in the initializers, initialize it there. For instance…


#define CAMERA_IP "10.19.67.11"

class RobotDemo : public SimpleRobot
{
  AxisCamera& camera;
  Joystick driver;    // driving Joystick for driver
public:
  RobotDemo(void) :
	driver(1),
	camera(AxisCamera::GetInstance(CAMERA_IP))
  {
	// Robot constructor
  }

// ....

awesome, thanks

Since AxisCamera is a singleton, you can just call AxisCamera::GetInstance(NULL). It doesn’t reconnect. Use the SOURCE :smiley:

See?


AxisCamera &AxisCamera::GetInstance(const char *cameraIP)
{
	if (NULL == _instance) //_instance is a static variable of type AxisCamera*
	{
		_instance = new AxisCamera(cameraIP);

		_instance->m_videoServer = new PCVideoServer();
	}

	return *_instance;
}

Just make sure you’re not the first one to call GetInstance, otherwise you’re screwed. Static initialization order fiasco anyone? Yay intermittent segfaults!

Another way is to just have an inline function:


static inline AxisCamera& camera() {
   static AxisCamera& c = AxisCamera::GetInstance(IP);
   return c;
}

Yes it’s nice to have encapsulation, but when the camera is a global singleton anyway, what’s the point?