-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
APP-4940 - Update fake encoder config to have ticks_per_sec
parameter
#3976
Closed
zaporter-work
wants to merge
9
commits into
viamrobotics:main
from
zaporter-work:zp/encoder-with-speed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f371d35
initial
zaporter-work 773457b
speed -> ticks per second
zaporter-work 2820f0a
not TriviallyClosable
zaporter-work dda31d8
add warning to reconfigure if ticks_per_sec is zero
zaporter-work ee2365e
update rate msec was truncating and did not work
zaporter-work e42d697
no more speed
zaporter-work 9dcd30b
delete all speed references
zaporter-work 45efe95
lint
zaporter-work 0216229
revert motor_test changes
zaporter-work File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"go.viam.com/rdk/components/encoder" | ||
"go.viam.com/rdk/logging" | ||
"go.viam.com/rdk/resource" | ||
rdkutils "go.viam.com/rdk/utils" | ||
) | ||
|
||
var fakeModel = resource.DefaultModelFamily.WithModel("fake") | ||
|
@@ -44,8 +45,8 @@ func NewEncoder( | |
if err := e.Reconfigure(ctx, nil, cfg); err != nil { | ||
return nil, err | ||
} | ||
e.workers = rdkutils.NewStoppableWorkers(e.start) | ||
|
||
e.start(ctx) | ||
return e, nil | ||
} | ||
|
||
|
@@ -59,6 +60,7 @@ func (e *fakeEncoder) Reconfigure( | |
return err | ||
} | ||
e.mu.Lock() | ||
|
||
e.updateRate = newConf.UpdateRate | ||
if e.updateRate == 0 { | ||
e.updateRate = 100 | ||
|
@@ -80,16 +82,14 @@ func (cfg *Config) Validate(path string) ([]string, error) { | |
// fakeEncoder keeps track of a fake motor position. | ||
type fakeEncoder struct { | ||
resource.Named | ||
resource.TriviallyCloseable | ||
|
||
positionType encoder.PositionType | ||
activeBackgroundWorkers sync.WaitGroup | ||
logger logging.Logger | ||
positionType encoder.PositionType | ||
logger logging.Logger | ||
|
||
mu sync.RWMutex | ||
workers rdkutils.StoppableWorkers | ||
position int64 | ||
speed float64 // ticks per minute | ||
updateRate int64 // update position in start every updateRate ms | ||
updateRate int64 // update position in start every updateRate ms | ||
} | ||
|
||
// Position returns the current position in terms of ticks or | ||
|
@@ -109,35 +109,32 @@ func (e *fakeEncoder) Position( | |
|
||
// Start starts a background thread to run the encoder. | ||
func (e *fakeEncoder) start(cancelCtx context.Context) { | ||
e.activeBackgroundWorkers.Add(1) | ||
utils.ManagedGo(func() { | ||
for { | ||
select { | ||
case <-cancelCtx.Done(): | ||
return | ||
default: | ||
} | ||
|
||
e.mu.RLock() | ||
updateRate := e.updateRate | ||
e.mu.RUnlock() | ||
if !utils.SelectContextOrWait(cancelCtx, time.Duration(updateRate)*time.Millisecond) { | ||
return | ||
} | ||
|
||
e.mu.Lock() | ||
e.position += int64(e.speed / float64(60*1000/updateRate)) | ||
e.mu.Unlock() | ||
for { | ||
select { | ||
case <-cancelCtx.Done(): | ||
return | ||
default: | ||
} | ||
|
||
e.mu.RLock() | ||
updateRate := e.updateRate | ||
e.mu.RUnlock() | ||
if !utils.SelectContextOrWait(cancelCtx, time.Duration(updateRate)*time.Millisecond) { | ||
return | ||
} | ||
}, e.activeBackgroundWorkers.Done) | ||
|
||
e.mu.Lock() | ||
e.position++ | ||
e.mu.Unlock() | ||
} | ||
} | ||
|
||
// ResetPosition sets the current position of the motor (adjusted by a given offset) | ||
// to be its new zero position. | ||
func (e *fakeEncoder) ResetPosition(ctx context.Context, extra map[string]interface{}) error { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
e.position = int64(0) | ||
e.position = 0 | ||
return nil | ||
} | ||
|
||
|
@@ -149,21 +146,20 @@ func (e *fakeEncoder) Properties(ctx context.Context, extra map[string]interface | |
}, nil | ||
} | ||
|
||
// Close safely shuts down the fake encoder. | ||
func (e *fakeEncoder) Close(ctx context.Context) error { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to lock the mutex: it protects the position, updateRate, and similar things, but we're not using any of those. |
||
e.workers.Stop() | ||
return nil | ||
} | ||
|
||
// Encoder is a fake encoder used for testing. | ||
type Encoder interface { | ||
encoder.Encoder | ||
SetSpeed(ctx context.Context, speed float64) error | ||
SetPosition(ctx context.Context, position int64) error | ||
} | ||
|
||
// SetSpeed sets the speed of the fake motor the encoder is measuring. | ||
func (e *fakeEncoder) SetSpeed(ctx context.Context, speed float64) error { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
e.speed = speed | ||
return nil | ||
} | ||
|
||
// SetPosition sets the position of the encoder. | ||
func (e *fakeEncoder) SetPosition(ctx context.Context, position int64) error { | ||
e.mu.Lock() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dang, I think this just fixed a bug!
ctx
was an argument to this function, and would likely be cancelled by the end of reconfiguration. By switching to aStoppableWorkers
, the context used in thestart
function won't be cancelled until someone callsClose
on the component. Thanks!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heh, the two bugs canceled each other out: our background goroutine would shut down unexpectedly early, and then when we don't shut it down during
Close
, we don't leak goroutines. Thanks for fixing both these issues! 😂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed -- When I saw these issues,
StoppableWorkers
is the first thing that came to mind. Thanks for making it!