How does the callback InertialGyro.collision work?

Hey there,

I am trying to understand how the method InertialGyro.collision works.

If I had this:

void collisionDetection()
{

Brain.Screen.print("collision detected");

}

/**
 * @author @DVT7125
 * @date 4/10/24
 * @brief User control task.
 * @return 0
 */
void userControl()
{
	if (!Competition.isEnabled())
	{
		logHandler("drivetrain_main", "Ctrl1 is NOT in command mode!", Log::Level::Fatal);
	}

	vex::thread motortemp(motorMonitor);
	InertialGyro.collision(collisionDetection);
	// Variables
	double turnVolts;
	double forwardVolts;

	while (Competition.isEnabled())
	{
		turnVolts = primaryController.Axis1.position() * 0.12; // -12 to 12
		forwardVolts = primaryController.Axis3.position() * 0.12 * (1 - (std::abs(turnVolts) / 12));
		LeftDriveSmart.spin(vex::forward, forwardVolts + turnVolts, vex::voltageUnits::volt);
		RightDriveSmart.spin(vex::forward, forwardVolts - turnVolts, vex::voltageUnits::volt);
		vex::this_thread::sleep_for(ConfigManager.getCtrlr1PollingRate());
	}
	return;
}

It won’t compile, and i’ll get the error:

src/comptition/comptition.cpp:30:25: error: cannot initialize a parameter of type 'void (*)(axisType, double, double, double)' with an lvalue of type 'void ()': different number of parameters (4 vs 0)
   30 |         InertialGyro.collision(collisionDetection);
      |                                ^~~~~~~~~~~~~~~~~~
c:\Users\Admin\AppData\Roaming\Code - Insiders\User\globalStorage\vexrobotics.vexcode\sdk\cpp\V5\V5_20240223_11_00_00/vexv5/include\vex_imu.h:282:27: note: passing argument to parameter 'callback' here
  282 |     void collision(void (*callback)(axisType, double, double, double));

I’ve tried using () with the function (So InertialGyro.collision(collisionDetection()); )
But that does not work.
I thought this function was just a callback when a collision was detected.

The callback is a little different than other events, it needs four parameters

void collision( axisType axis, double x, double y, double z ) {
  printf("collision %d %6.2f %6.2f %6.2f\n", (int)axis, x, y, z);
}

you will get an indication of which axis the code thinks the collision occurred on and three raw accelerometer values.

2 Likes