Skip to content

Commit

Permalink
Start adding task status support.
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoga committed May 20, 2024
1 parent 404fc62 commit 7de3cb8
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 20 deletions.
7 changes: 7 additions & 0 deletions examples/module/chassis/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
robomaster "github.com/brunoga/robomaster"
"github.com/brunoga/robomaster/module"
"github.com/brunoga/robomaster/module/chassis"
"github.com/brunoga/robomaster/module/chassis/controller"
"github.com/brunoga/robomaster/unitybridge/support"
"github.com/brunoga/robomaster/unitybridge/support/logger"
)
Expand All @@ -32,5 +33,11 @@ func TestMain(m *testing.M) {

chassisModule = c.Chassis()

// Set controller mode to SDK for the tests here.
err = chassisModule.SetControllerMode(controller.ModeSDK)
if err != nil {
panic(err)
}

os.Exit(m.Run())
}
17 changes: 17 additions & 0 deletions examples/module/chassis/set_position_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package chassis

import (
"testing"
"time"

"github.com/brunoga/robomaster/module/chassis"
)

func TestSetPosition(t *testing.T) {
err := chassisModule.SetPosition(chassis.ModeAngularVelocity, 1, 0, 0)
if err != nil {
panic(err)
}

time.Sleep(5 * time.Second)
}
12 changes: 3 additions & 9 deletions examples/module/chassis/set_speed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ import (
"time"

"github.com/brunoga/robomaster/module/chassis"
"github.com/brunoga/robomaster/module/chassis/controller"
)

func TestSetSpeed(t *testing.T) {
err := chassisModule.SetControllerMode(controller.ModeSDK)
if err != nil {
panic(err)
}
defer chassisModule.SetControllerMode(controller.ModeFPV)

setSpeed(1*time.Second, 0.2, 0.0, 0.0)
setSpeed(1*time.Second, 0.0, 0.2, 0.0)
setSpeed(1*time.Second, -0.2, 0.0, 0.0)
setSpeed(1*time.Second, 0.0, 0.2, 0.0)
setSpeed(1*time.Second, 0.0, -0.2, 0.0)
setSpeed(1*time.Second, 0.0, 0.0, 360.0)
setSpeed(1*time.Second, 0.0, 0.0, 180.0)
setSpeed(1*time.Second, 0.0, 0.0, -180.0)
}

func setSpeed(d time.Duration, x, y, z float64) {
Expand Down
7 changes: 3 additions & 4 deletions module/chassis/chassis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/brunoga/robomaster/unitybridge/unity/key"
"github.com/brunoga/robomaster/unitybridge/unity/result"
"github.com/brunoga/robomaster/unitybridge/unity/result/value"
"github.com/brunoga/robomaster/unitybridge/unity/task"
)

// Chassis allows controlling the robot chassis. It also works as the robot main
Expand Down Expand Up @@ -115,15 +116,13 @@ func (c *Chassis) SetPosition(m Mode, x, y, z float64) error {
controlMode = 1
}

return c.UB().PerformActionForKey(key.KeyMainControllerChassisPosition, &value.ChassisPosition{
TaskID: 1,
return c.UB().PerformActionForKeySync(key.KeyMainControllerChassisPosition, &value.ChassisPosition{
TaskType: task.TypeChassisPosition,
IsCancel: 0,
ControlMode: controlMode,
X: float32(x),
Y: float32(y),
Z: float32(z),
}, func(r *result.Result) {
fmt.Println("ChassisPosition result:", r)
})
}

Expand Down
2 changes: 1 addition & 1 deletion unitybridge/unity/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ var (
KeyRobomasterSystemConfigSkillTable = newKey("KeyRobomasterSystemConfigSkillTable", 83886130, AccessTypeWrite, nil)
KeyRobomasterSystemWorkingDevices = newKey("KeyRobomasterSystemWorkingDevices", 83886131, AccessTypeRead, &value.List[uint16]{})
KeyRobomasterSystemExceptions = newKey("KeyRobomasterSystemExceptions", 83886132, AccessTypeRead, nil)
KeyRobomasterSystemTaskStatus = newKey("KeyRobomasterSystemTaskStatus", 83886133, AccessTypeRead, nil)
KeyRobomasterSystemTaskStatus = newKey("KeyRobomasterSystemTaskStatus", 83886133, AccessTypeRead, &value.TaskStatus{})
KeyRobomasterSystemReturnEnabled = newKey("KeyRobomasterSystemReturnEnabled", 83886134, AccessTypeRead|AccessTypeWrite, nil)
KeyRobomasterSystemSafeMode = newKey("KeyRobomasterSystemSafeMode", 83886135, AccessTypeRead|AccessTypeWrite, nil)
KeyRobomasterSystemScratchExecuteState = newKey("KeyRobomasterSystemScratchExecuteState", 83886136, AccessTypeRead, nil)
Expand Down
14 changes: 8 additions & 6 deletions unitybridge/unity/result/value/chassis_position.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package value

import "github.com/brunoga/robomaster/unitybridge/unity/task"

type ChassisPosition struct {
TaskID uint8 `json:"taskId"`
IsCancel uint8 `json:"isCancel"`
ControlMode uint8 `json:"controlMode"`
X float32 `json:"positionX"`
Y float32 `json:"positionY"`
Z float32 `json:"positionYaw"`
TaskType task.Type `json:"taskId"`
IsCancel uint8 `json:"isCancel"`
ControlMode uint8 `json:"controlMode"`
X float32 `json:"positionX"`
Y float32 `json:"positionY"`
Z float32 `json:"positionYaw"`
}
9 changes: 9 additions & 0 deletions unitybridge/unity/result/value/task_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package value

import "github.com/brunoga/robomaster/unitybridge/unity/task"

type TaskStatus struct {
TaskType task.Type `json:"taskId"` // JSON name seems mismatched. Check.
Percent float64 `json:"percent"`
Status task.Status `json:"status"`
}
10 changes: 10 additions & 0 deletions unitybridge/unity/task/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package task

type Status int8

const (
StatusRunning Status = iota
StatusSuccess
StatusFailure
StatusCount
)
11 changes: 11 additions & 0 deletions unitybridge/unity/task/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package task

type Type int8

const (
TypeUnknown Type = iota
TypeChassisPosition
TypeGimbalReset
TypeGimbalAngle
TypeCount
)

0 comments on commit 7de3cb8

Please sign in to comment.