Examples
Use case
This example reads the CAN Magnetic Switch and publishes the detected state and timestamp to SmartDashboard. Use it to verify CAN communication, determine the repeatable trigger point, and test the magnet’s orientation and working gap on the actual mechanism.
Setup
Install the AndyMark WPILib vendor library before using these examples. See Device API for all available methods.
import com.andymark.jni.AM_CAN_Mag_Switch;
import com.andymark.jni.AM_CAN_Mag_Switch.AM_MagSwitchData;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
// CAN bus 2, device ID 0
AM_CAN_Mag_Switch magSwitch = new AM_CAN_Mag_Switch(2, 0);
// Restore the default 100 ms report period.
magSwitch.resetReportPeriod();
AM_MagSwitchData data = magSwitch.getData();
SmartDashboard.putBoolean(
"MagSwitch/MagnetDetected",
data.magnetDetected
);
SmartDashboard.putNumber(
"MagSwitch/Timestamp",
data.timeStamp
);#include "AM_CAN_Mag_Switch.h"
#include <frc/smartdashboard/SmartDashboard.h>
// CAN bus 2, device ID 0
AM_CAN_Mag_Switch magSwitch{2, 0};
// Restore the default 100 ms report period.
magSwitch.ResetReportPeriod();
AM_MagSwitchData data = magSwitch.GetData();
frc::SmartDashboard::PutBoolean(
"MagSwitch/MagnetDetected",
data.magnetDetected
);
frc::SmartDashboard::PutNumber(
"MagSwitch/Timestamp",
data.timeStamp
);Validation steps
Move the magnet toward and away from the sensing face while watching the reported state.
Repeat from both travel directions to identify any difference between activation and release points.
Test the complete tolerance stack, including mechanism play and vibration.
Confirm that nearby steel, current-carrying conductors, or other magnets do not create unexpected behavior.
Add software debouncing or persistence if a single sample should not change the mechanism state.
Last updated
Was this helpful?

