I have a PS4 game controller, and I can now read data from it using HID, including 6 axes as shown in the image below.
Three axes represent acceleration, and the other three represent angular rate.
With this data, I should be able to visualize the actual posture of the controller using a 3D model in WPF. Currently, I found that the "3D Controller Overlay" feature in Steam perfectly achieves the effect I desire. Is there any library or framework available to help achieve this effect?
In the end, I found this solution which perfectly solved my problem. Create a instance of MadgwickAHRS
, then call Update
to update the six-axis data, and finally obtain a quaternion for rotation. The specific code is as follows:
static MadgwickAHRS AHRS = new MadgwickAHRS(4f / 1000f, 0.1f);
static float deg2rad(float degrees)
{
return (float)(Math.PI / 180) * degrees;
}
private void OnSensorDataUpdated(byte[] hidData){
short gyroX = BitConverter.ToInt16(hidData[13]),
gyroY = BitConverter.ToInt16(hidData[15]),
gyroZ = BitConverter.ToInt16(hidData[17]),
accelX = BitConverter.ToInt16(hidData[19]),
accelY = BitConverter.ToInt16(hidData[21]),
accelZ = BitConverter.ToInt16(hidData[23]);
//Since my gyro data is in degrees, it needs to be converted to radians
AHRS.Update(deg2rad(gyroX), deg2rad(gyroY), deg2rad(gyroZ), accelX, accelY, accelZ);
var rotation = new Quaternion(AHRS.Quaternion[1], AHRS.Quaternion[2], AHRS.Quaternion[3], AHRS.Quaternion[0]);
this.Dispatcher.Invoke(()=>{
var rotateTransform = new RotateTransform3D();
rotateTransform.Rotation = new QuaternionRotation3D(rotation);
YourModel.Transform = rotateTransform;//Update to ModelVisual3D
});
}