Skip to content

Commit b94eb14

Browse files
committed
moved spot control scripts into zerosim. publishing twist stamped message.
1 parent c645d92 commit b94eb14

File tree

7 files changed

+211
-1
lines changed

7 files changed

+211
-1
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using System;
5+
6+
namespace ZO {
7+
public class ZOSpotCharacterController : MonoBehaviour {
8+
9+
public Rigidbody _rigidBody;
10+
public float _maxFloorAngle = 45;
11+
public float _maxForwardVelocity = 1.0f;
12+
13+
public float MaxForwardVelocity {
14+
get => _maxForwardVelocity;
15+
set => _maxForwardVelocity = value;
16+
}
17+
public float _maxSideMoveVelocity = 0.5f;
18+
public float MaxSideMoveVelocity {
19+
get => _maxSideMoveVelocity;
20+
set => _maxSideMoveVelocity = value;
21+
}
22+
23+
public float _maxTurnVelocityDegreesSecond = 45.0f;
24+
public float MaxTurnVelocityDegreesSecond {
25+
get => _maxTurnVelocityDegreesSecond;
26+
set => _maxTurnVelocityDegreesSecond = value;
27+
}
28+
29+
private Vector2 _targetVelocity = Vector2.zero;
30+
private float _targetTurnVelocityDegreesPerSecond = 0.0f;
31+
32+
public enum StateEnum {
33+
Off,
34+
Down,
35+
UpAndReady,
36+
Error
37+
}
38+
39+
public ZOSpotCharacterController.StateEnum State {
40+
get;
41+
private set;
42+
} = ZOSpotCharacterController.StateEnum.Off;
43+
44+
public enum ErrorEnum {
45+
Ok,
46+
GeneralError,
47+
ErrorShutdown
48+
}
49+
50+
public ZOSpotCharacterController.ErrorEnum Error {
51+
get;
52+
private set;
53+
} = ZOSpotCharacterController.ErrorEnum.Ok;
54+
55+
56+
57+
public delegate void SpotControllerStatusDelegate(ZOSpotCharacterController thisClass, Vector2 linearVelocity, float turnVelocityDegreesPerSecond, float orientationDegrees, ZOSpotCharacterController.StateEnum state, ZOSpotCharacterController.ErrorEnum error);
58+
private event SpotControllerStatusDelegate _spotContrllerStatusDelegate;
59+
public event SpotControllerStatusDelegate SpotControllerStatus {
60+
add {
61+
_spotContrllerStatusDelegate += value;
62+
}
63+
remove {
64+
_spotContrllerStatusDelegate -= value;
65+
}
66+
}
67+
68+
69+
70+
// Start is called before the first frame update
71+
void Start() {
72+
73+
}
74+
75+
// Update is called once per frame
76+
void Update() {
77+
78+
}
79+
80+
void FixedUpdate() {
81+
82+
_rigidBody.velocity = _rigidBody.transform.rotation * new Vector3(_targetVelocity.x, _rigidBody.velocity.y, _targetVelocity.y);
83+
84+
Vector3 angularVelocity = _rigidBody.angularVelocity;
85+
angularVelocity.y = _targetTurnVelocityDegreesPerSecond * Mathf.Deg2Rad;
86+
_rigidBody.angularVelocity = angularVelocity;
87+
88+
if (_spotContrllerStatusDelegate != null) {
89+
_spotContrllerStatusDelegate.Invoke(this, new Vector2(_rigidBody.velocity.x, _rigidBody.velocity.z), _rigidBody.angularVelocity.y * Mathf.Rad2Deg, transform.rotation.eulerAngles.y, State, Error);
90+
91+
}
92+
}
93+
94+
95+
public void Move(Vector2 targetVelocity, float targetTurnVelocityDegreesPerSecond) {
96+
_targetVelocity = targetVelocity;
97+
_targetTurnVelocityDegreesPerSecond = targetTurnVelocityDegreesPerSecond;
98+
}
99+
// void Sit(Delegate OnSitFinishedOrError)
100+
// void Stand(Delegate OnStandFinishedOrError)
101+
// void TurnOn(Delegate OnTurnOnFinishedOrError)
102+
// void TurnOff(Delegate OnTurnOffFinishedOrError)
103+
}
104+
105+
}

Runtime/Scripts/Controllers/Robots/Spot/ZOSpotCharacterController.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Scripts/Controllers/Robots/Spot/ZOSpotController.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@
1010
using ZO.ROS.Unity;
1111
using ZO.ROS.MessageTypes;
1212
using ZO.ROS.MessageTypes.Geometry;
13+
using ZO.ROS.MessageTypes.Nav;
1314

1415

1516
namespace ZO.Controllers {
1617

1718
[RequireComponent(typeof(ZOROSJointStatesPublisher))]
19+
[RequireComponent(typeof(ZOSpotCharacterController))]
1820
public class ZOSpotController : ZOROSUnityGameObjectBase {
1921

2022

2123
public string _TwistTopicSubscription = "/cmd_vel";
2224
private TwistMessage _twistMessage = new TwistMessage();
25+
private TwistWithCovarianceStampedMessage _twistPublishMessage = new TwistWithCovarianceStampedMessage();
26+
private ZOROSFakeOdometryPublisher _odomPublisher = null;
27+
28+
29+
2330

2431

2532
protected override void ZOOnValidate() {
@@ -30,6 +37,8 @@ protected override void ZOOnValidate() {
3037
protected override void ZOStart() {
3138
base.ZOStart();
3239

40+
_odomPublisher = GetComponentInChildren<ZOROSFakeOdometryPublisher>(true);
41+
3342

3443
}
3544

@@ -49,6 +58,13 @@ protected override void ZOFixedUpdate() {
4958

5059
protected override void ZOUpdateHzSynchronized() {
5160
base.ZOUpdateHzSynchronized();
61+
62+
if (_odomPublisher && ZOROSBridgeConnection.Instance.IsConnected == true) {
63+
_twistPublishMessage.header = _odomPublisher.CurrentOdometryMessage.header;
64+
_twistPublishMessage.twist = _odomPublisher.CurrentOdometryMessage.twist;
65+
ZOROSBridgeConnection.Instance.Publish<TwistWithCovarianceStampedMessage>(_twistPublishMessage, "/odometry/twist");
66+
67+
}
5268
}
5369

5470

@@ -62,11 +78,14 @@ public override void OnROSBridgeConnected(ZOROSUnityManager rosUnityManager) {
6278

6379
// subscribe to Twist Message
6480
ZOROSBridgeConnection.Instance.Subscribe<TwistMessage>(Name, _TwistTopicSubscription, _twistMessage.MessageType, OnROSTwistMessageReceived);
81+
82+
// advertise twist
83+
ZOROSBridgeConnection.Instance.Advertise("/odometry/twist", TwistWithCovarianceStampedMessage.Type);
6584

6685
}
6786

6887
public override void OnROSBridgeDisconnected(ZOROSUnityManager rosUnityManager) {
69-
ZOROSBridgeConnection.Instance.UnAdvertise(_TwistTopicSubscription);
88+
ZOROSBridgeConnection.Instance.UnAdvertise("/odometry/twist");
7089
Debug.Log("INFO: ZODifferentialDriveController::OnROSBridgeDisconnected");
7190
}
7291

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ZO {
6+
public class ZOSpotJoystickCmdVel : MonoBehaviour {
7+
8+
public ZOSpotCharacterController _spotCharacterController;
9+
10+
// Start is called before the first frame update
11+
void Start() {
12+
13+
}
14+
15+
// Update is called once per frame
16+
void Update() {
17+
18+
if (_spotCharacterController != null) {
19+
Vector2 targetVelocity = new Vector2(Input.GetAxis("Roll") * _spotCharacterController.MaxSideMoveVelocity, Input.GetAxis("Pitch") * _spotCharacterController.MaxForwardVelocity);
20+
_spotCharacterController.Move(targetVelocity, Input.GetAxis("Yaw") * _spotCharacterController._maxTurnVelocityDegreesSecond); // hack no turning yet
21+
22+
}
23+
24+
}
25+
}
26+
27+
}

Runtime/Scripts/Controllers/Robots/Spot/ZOSpotJoystickCmdVel.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Scripts/ROS/MessageTypes/Geometry/ZOROSGeometryMessages.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,37 @@ public TwistWithCovarianceMessage(TwistMessage twist, double[] covariance) {
212212

213213
}
214214

215+
/// <summary>
216+
/// This represents an estimated twist with reference coordinate frame and timestamp.
217+
/// See: https://docs.ros.org/en/api/geometry_msgs/html/msg/TwistWithCovarianceStamped.html
218+
/// </summary>
219+
public class TwistWithCovarianceStampedMessage : ZOROSMessageInterface {
220+
221+
[Newtonsoft.Json.JsonIgnore]
222+
public string MessageType { get { return TwistWithCovarianceStampedMessage.Type; } }
223+
[Newtonsoft.Json.JsonIgnore]
224+
public static string Type = "geometry_msgs/TwistWithCovarianceStamped";
225+
226+
public HeaderMessage header { get; set; }
227+
228+
// This expresses velocity in free space with uncertainty.
229+
public TwistWithCovarianceMessage twist { get; set; }
230+
231+
public TwistWithCovarianceStampedMessage() {
232+
this.twist = new TwistWithCovarianceMessage();
233+
}
234+
235+
public TwistWithCovarianceStampedMessage(TwistWithCovarianceMessage twist) {
236+
this.twist = twist;
237+
}
238+
239+
public void Update() {
240+
header.Update();
241+
}
242+
243+
244+
}
245+
215246
/// <summary>
216247
/// This represents the transform between two coordinate frames in free space.
217248
/// See: http://docs.ros.org/melodic/api/geometry_msgs/html/msg/Transform.html

Runtime/Scripts/ROS/Unity/Publishers/ZOROSFakeOdometryPublisher.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public Rigidbody Rigidbody {
3333
}
3434
}
3535

36+
public OdometryMessage CurrentOdometryMessage {
37+
get {
38+
return _currentOdometryMessage;
39+
}
40+
}
41+
3642
private ZOGaussianNoiseModel _noiseModel = new ZOGaussianNoiseModel();
3743
private OdometryMessage _currentOdometryMessage = null;
3844
private OdometryMessage _previousOdometryMessage = null;

0 commit comments

Comments
 (0)