Your comments
I tested this with Articulation Bodies. I used Python and ROS2 as a bridge between Unity and the PLC. All works well.
The reason I purchased your package is that it offers various interfaces.
I need a script similar to 'DriveMeasureSpeedPosition.cs' but for Articulation Body.
Here is the code that works. Can I implement PLC IO (Speed PLC Input Float) into my script?
Of course, I need to get rid of ROS2.
I want to achieve same, but related to Articulation Body.
We have already agreed on the project with the customer, so we are in a hurry.
using UnityEngine;
using ROS2;
using System;
using std_msgs.msg;
public class Encoder : Ros2TopicComponent
{
private ArticulationBody articulationBody;
public char axisDrive = 'x'; // Default axis ('x', 'y', or 'z')
public bool publish = true;
public float distanceTraveled = 0f;
public float encoderDiameter = 0.1f; // Encoder diameter in meters (0.1m = 100mm)
private float lastAngle = 0f; // Last recorded angle in radians
private const float FullCircle = 2 * Mathf.PI;
void Start()
{
articulationBody = GetComponent();
if (articulationBody == null)
{
Debug.LogError("ArticulationBody not assigned to " + this.name);
return;
}
if (publish)
{
CreatePub(articulationBody.name, new Float32());
}
// Initialize lastAngle with the starting angle
lastAngle = GetCurrentAngle();
}
void FixedUpdate()
{
float currentAngle = GetCurrentAngle();
float angleDelta = currentAngle - lastAngle;
// Account for overflow if the angle wraps around (360 degrees or 2π radians)
if (angleDelta > Mathf.PI)
{
angleDelta -= FullCircle;
}
else if (angleDelta < -Mathf.PI)
{
angleDelta += FullCircle;
}
// Calculate distance traveled based on angle delta
distanceTraveled += (angleDelta / FullCircle) * (encoderDiameter * Mathf.PI);
lastAngle = currentAngle;
// Publish distance traveled
if (publish)
{
Float32 msg = new Float32();
msg.Data = distanceTraveled;
Float32Publisher(msg);
}
}
// Helper method to get the current angle based on the selected axis
private float GetCurrentAngle()
{
switch (axisDrive)
{
case 'x':
return articulationBody.jointPosition[0];
case 'y':
return articulationBody.jointPosition[1];
case 'z':
return articulationBody.jointPosition[2];
default:
Debug.LogError("Invalid axisDrive character. Use 'x', 'y', or 'z'.");
return 0f;
}
}
}
Customer support service by UserEcho
Hi! Thanks for PLCInputFloat :)
I managed to connect the ArticulationBody
using realvirtual;
public PLCInputFloat Position;
Position.Value = ArticulatedBody.Rotation;