Skip to content

Commit

Permalink
add do command to motor
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston committed Sep 10, 2024
1 parent 9adfe1d commit b8c95ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions components/motor/gpio/controlled.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gpio

import (
"context"
"fmt"
"math"
"sync"
"time"
Expand All @@ -18,6 +19,8 @@ import (
rdkutils "go.viam.com/rdk/utils"
)

const getPID = "get_tuned_pid"

// SetState sets the state of the motor for the built-in control loop.
func (cm *controlledMotor) SetState(ctx context.Context, state []*control.Signal) error {
if cm.loop != nil && !cm.loop.Running() {
Expand Down Expand Up @@ -393,3 +396,21 @@ func (cm *controlledMotor) GoFor(ctx context.Context, rpm, revolutions float64,

return nil
}

func (cm *controlledMotor) DoCommand(ctx context.Context, req map[string]interface{}) (map[string]interface{}, error) {
resp := make(map[string]interface{})

cm.mu.Lock()
defer cm.mu.Unlock()
ok := req[getPID].(bool)
if ok {
var respStr string
if !(*cm.tunedVals)[0].NeedsAutoTuning() {
respStr += fmt.Sprintf("{p: %v, i: %v, d: %v, type: %v} ",
(*cm.tunedVals)[0].P, (*cm.tunedVals)[0].I, (*cm.tunedVals)[0].D, (*cm.tunedVals)[0].Type)
}
resp[getPID] = respStr
}

return resp, nil
}
2 changes: 1 addition & 1 deletion control/setup_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func TunedPIDErr(name string, tunedVals []PIDConfig) error {
return fmt.Errorf(`%v has been tuned, please copy the following control values into your config: %v`, name, tunedStr)
}

// TuningInProgressErr returns an error when the loop is actively tuning
// TuningInProgressErr returns an error when the loop is actively tuning.
func TuningInProgressErr(name string) error {
return fmt.Errorf(`tuning for %v is in progress`, name)
}

0 comments on commit b8c95ab

Please sign in to comment.