Skip to content

Commit 0ede73c

Browse files
committed
fixed arm controller state management
1 parent e3b7e68 commit 0ede73c

File tree

4 files changed

+67
-37
lines changed

4 files changed

+67
-37
lines changed

Runtime/Scripts/ROS/ROSBridge/ZOROSActionServer.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -254,29 +254,27 @@ private Task _OnGoalReceived(ZOROSBridgeConnection rosBridgeConnection, ZOROSMes
254254
private Task _OnCancelReceived(ZOROSBridgeConnection rosBridgeConnection, ZOROSMessageInterface msg) {
255255

256256
GoalIDMessage goalID = msg as GoalIDMessage;
257-
OnCancelReceived?.Invoke(this, goalID);
257+
// OnCancelReceived?.Invoke(this, goalID);
258258

259259
Debug.Log("INFO: ZOROSActionServer::OnCancelReceived for goal id: " + goalID.id);
260-
// switch (ActionStatus) {
261-
// case ActionStatusEnum.PENDING:
262-
// ActionStatus = ActionStatusEnum.RECALLING;
263-
// OnCancelReceived?.Invoke(this, goalID);
264-
// break;
265-
// case ActionStatusEnum.ACTIVE:
266-
// ActionStatus = ActionStatusEnum.PREEMPTING;
267-
// OnCancelReceived?.Invoke(this, goalID);
268-
// break;
269-
// default:
270-
// Debug.LogWarning("WARNING: Goal cannot be canceled in current state: " + _actionStatus.ToString());
271-
// break;
272-
// }
260+
switch (CurrentGoalActionStatus) {
261+
case ActionStatusEnum.PENDING:
262+
CurrentGoalActionStatus = ActionStatusEnum.RECALLING;
263+
OnCancelReceived?.Invoke(this, goalID);
264+
break;
265+
case ActionStatusEnum.ACTIVE:
266+
CurrentGoalActionStatus = ActionStatusEnum.PREEMPTING;
267+
OnCancelReceived?.Invoke(this, goalID);
268+
break;
269+
default:
270+
Debug.LogWarning("WARNING: Goal cannot be canceled in current state: " + CurrentGoalActionStatus.ToString());
271+
break;
272+
}
273273

274274
return Task.CompletedTask;
275275
}
276276

277277
// Server Triggered Actions
278-
// protected abstract void OnGoalActive();
279-
280278
/// <summary>
281279
/// Accepts a new goal when one is available. The status of this goal is set to active upon
282280
/// acceptance, and the status of any previously active goal is set to preempted. Preempts

Runtime/Scripts/ROS/Unity/Controllers/ZOArmController.cs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public string HardwareInterface {
314314
get { return "hardware_interface::PositionJointInterface"; }
315315
}
316316

317-
ControllerStateEnum _state;
317+
ControllerStateEnum _state = ControllerStateEnum.Uninitialized;
318318
/// <summary>
319319
/// Returns controller state of Stopped, Initialize, or Running
320320
/// </summary>
@@ -395,32 +395,50 @@ public ControllerStateMessage ControllerStateMessage {
395395
/// </summary>
396396
public void LoadController() {
397397
Debug.Log("INFO: ZOArmController::Load");
398+
if (ControllerState != ControllerStateEnum.Uninitialized && ControllerState != ControllerStateEnum.Terminated) {
399+
Debug.LogError("ERROR: Cannot load arm controller in current state: " + ControllerState.ToString());
400+
return;
401+
}
398402
Initialize(); // BUGBUG: we should separate initialized into load -> start calls
399403
ControllerState = ControllerStateEnum.Initialized;
404+
_actionServer.OnGoalReceived += OnActionGoalReceived;
405+
_actionServer.OnCancelReceived += OnActionCancelReceived;
406+
400407
}
401408

402409
/// <summary>
403410
/// Implements ROS Controller Unload
404411
/// </summary>
405412
public void UnloadController() {
406413
Debug.Log("INFO: ZOArmController::Unload");
414+
if (ControllerState == ControllerStateEnum.Uninitialized || ControllerState == ControllerStateEnum.Terminated) {
415+
Debug.LogError("ERROR: Cannot unload arm controller in current state: " + ControllerState.ToString());
416+
return;
417+
}
418+
407419
Terminate();
408420
ControllerState = ControllerStateEnum.Terminated;
421+
_actionServer.OnGoalReceived -= OnActionGoalReceived;
422+
_actionServer.OnCancelReceived -= OnActionCancelReceived;
409423
}
410424

411425
public void StartController() {
412426
Debug.Log("INFO: ZOArmController::Start");
427+
if (ControllerState != ControllerStateEnum.Initialized && ControllerState != ControllerStateEnum.Stopped) {
428+
Debug.LogError("ERROR: Cannot start arm controller in current state: " + ControllerState.ToString());
429+
return;
430+
}
413431
ControllerState = ControllerStateEnum.Running;
414-
_actionServer.OnGoalReceived += OnActionGoalReceived;
415-
_actionServer.OnCancelReceived += OnActionCancelReceived;
416-
417432
}
418433

419434
public void StopController() {
420435
Debug.Log("INFO: ZOArmController::Stop");
436+
if (ControllerState != ControllerStateEnum.Running) {
437+
Debug.LogError("ERROR: Cannot stop arm controller in current state: " + ControllerState.ToString());
438+
return;
439+
}
440+
421441
ControllerState = ControllerStateEnum.Stopped;
422-
_actionServer.OnGoalReceived -= OnActionGoalReceived;
423-
_actionServer.OnCancelReceived -= OnActionCancelReceived;
424442
}
425443

426444
#endregion // ZOROSControllerInterface
@@ -448,7 +466,7 @@ private void Initialize() {
448466
// advertise joint state
449467
ROSBridgeConnection.Advertise(ControllerManager.Name + "/arm_controller/state", JointTrajectoryControllerStateMessage.Type);
450468

451-
469+
452470

453471
}
454472

@@ -490,43 +508,56 @@ public Task OnControlMessageReceived(ZOROSBridgeConnection rosBridgeConnection,
490508

491509

492510
/// <summary>
493-
/// This is a FollowJointTrajectoryActionMessage action responder. Usually used by MoveIt.
511+
/// FollowJointTrajectoryActionMessage action goal responder. Usually used by MoveIt.
494512
/// </summary>
495513
/// <param name="actionServer"></param>
496514
/// <param name="goalMessage"></param>
497515
/// <returns></returns>
498516
Task OnActionGoalReceived(ZOROSActionServer<FollowJointTrajectoryActionMessage, FollowJointTrajectoryActionGoal> actionServer, FollowJointTrajectoryActionGoal goalMessage) {
499517

500-
Debug.Log("INFO: ZOArmController::OnGoalReceived");
501-
502-
// automatically acccept new goal
503-
// TODO: perhaps more complex logic? are there conditions where we will not accept a new goal?
504-
actionServer.AcceptNewGoal(goalMessage);
505-
506-
Debug.Log($"INFO: Goad id: {goalMessage.goal_id.id}: with number of points: {goalMessage.goal.trajectory.points.Length}");
507-
508-
_currentGoalTime = 0;
518+
Debug.Log("INFO: ZOArmController::OnActionGoalReceived");
519+
if (ControllerState == ControllerStateEnum.Terminated || ControllerState == ControllerStateEnum.Uninitialized) {
520+
Debug.LogError("INFO: ZOArmController::OnActionGoalReceived invalid controller state: " + ControllerState.ToString());
521+
actionServer.SetRejected("Controller not initialized");
522+
} else {
523+
if (ControllerState != ControllerStateEnum.Running) {
524+
StartController();
525+
}
526+
actionServer.AcceptNewGoal(goalMessage);
527+
Debug.Log($"INFO: Accepted Goal id: {goalMessage.goal_id.id}: with number of points: {goalMessage.goal.trajectory.points.Length}");
528+
_currentGoalTime = 0;
529+
}
509530

510531
return Task.CompletedTask;
511532
}
512533

534+
/// <summary>
535+
/// FollowJointTrajectoryActionMessage action cancel responder. Usually used by MoveIt.
536+
/// </summary>
537+
/// <param name="actionServer"></param>
538+
/// <param name="goalID"></param>
539+
/// <returns></returns>
513540
Task OnActionCancelReceived(ZOROSActionServer<FollowJointTrajectoryActionMessage, FollowJointTrajectoryActionGoal> actionServer, GoalIDMessage goalID) {
514541

515542
Debug.Log("INFO: ZOArmController::OnCancelReceived");
543+
544+
if (ControllerState == ControllerStateEnum.Running) {
545+
// Stop Controller
546+
StopController();
547+
}
548+
516549
// confirm cancellation to the action server
517550
actionServer.SetCanceled();
518551

519552
return Task.CompletedTask;
520-
521-
522553
}
523554

524555
#endregion
525556

526557

527558
#region ZOSerializationInterface
528559

529-
public string Type {
560+
public override string Type {
530561
get => "controller.arm_controller";
531562
}
532563

Runtime/Scripts/ROS/Unity/Controllers/ZOROSControllerInterface.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ZO.ROS.MessageTypes.ControllerManager;
22
namespace ZO.ROS.Controllers {
33
public enum ControllerStateEnum {
4+
Uninitialized,
45
Stopped,
56
Initialized,
67
Running,

Runtime/Scripts/ROS/Unity/Services/ZOControllerManagerService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private void Initialize() {
180180
// check if controller is registered
181181
if (_controllers.ContainsKey(controller)) {
182182

183-
// Unload the controller if necessary
183+
// stop the controller if necessary
184184
_controllers[controller].StopController();
185185

186186
} else {

0 commit comments

Comments
 (0)