Skip to content

Commit

Permalink
speed -> ticks per second
Browse files Browse the repository at this point in the history
  • Loading branch information
zaporter-work committed May 20, 2024
1 parent f371d35 commit 773457b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
24 changes: 12 additions & 12 deletions components/encoder/fake/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (e *fakeEncoder) Reconfigure(
}
e.mu.Lock()
e.updateRate = newConf.UpdateRate
e.speed = newConf.Speed
e.ticksPerSec = newConf.TicksPerSec
if e.updateRate == 0 {
e.updateRate = 100
}
Expand All @@ -71,8 +71,8 @@ func (e *fakeEncoder) Reconfigure(

// Config describes the configuration of a fake encoder.
type Config struct {
UpdateRate int64 `json:"update_rate_msec,omitempty"`
Speed float64 `json:"speed,omitempty"`
UpdateRate int64 `json:"update_rate_msec,omitempty"`
TicksPerSec float64 `json:"ticks_per_sec,omitempty"`
}

// Validate ensures all parts of a config is valid.
Expand All @@ -88,11 +88,11 @@ type fakeEncoder struct {
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
mu sync.RWMutex
workers rdkutils.StoppableWorkers
position int64
ticksPerSec float64 // increment to position every sec
updateRate int64 // update position in start every updateRate ms
}

// Position returns the current position in terms of ticks or
Expand Down Expand Up @@ -127,7 +127,7 @@ func (e *fakeEncoder) start(cancelCtx context.Context) {
}

e.mu.Lock()
e.position += int64(e.speed / float64(60*1000/updateRate))
e.position += int64(e.ticksPerSec / float64(1000/updateRate))
e.mu.Unlock()
}
}
Expand Down Expand Up @@ -160,15 +160,15 @@ func (e *fakeEncoder) Close(ctx context.Context) error {
// Encoder is a fake encoder used for testing.
type Encoder interface {
encoder.Encoder
SetSpeed(ctx context.Context, speed float64) error
SetSpeed(ctx context.Context, ticksPerSec 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 {
func (e *fakeEncoder) SetSpeed(ctx context.Context, ticksPerSec float64) error {
e.mu.Lock()
defer e.mu.Unlock()
e.speed = speed
e.ticksPerSec = ticksPerSec
return nil
}

Expand Down
14 changes: 7 additions & 7 deletions components/encoder/fake/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func TestEncoder(t *testing.T) {
})
})

// Set Speed
t.Run("set speed", func(t *testing.T) {
// Set ticks per sec
t.Run("set ticks per second", func(t *testing.T) {
e1 := e.(*fakeEncoder)
err := e1.SetSpeed(ctx, 1)
test.That(t, err, test.ShouldBeNil)
test.That(t, e1.speed, test.ShouldEqual, 1)
test.That(t, e1.ticksPerSec, test.ShouldEqual, 1)
})

// Start with default update rate
Expand All @@ -99,22 +99,22 @@ func TestEncoder(t *testing.T) {
})
})

t.Run("reconfigure with speed", func(t *testing.T) {
t.Run("reconfigure with different update ticks per second", func(t *testing.T) {
e1 := e.(*fakeEncoder)
err := e1.SetSpeed(ctx, 0)
test.That(t, err, test.ShouldBeNil)
err = e1.SetPosition(ctx, 0)
test.That(t, err, test.ShouldBeNil)

ic := Config{
UpdateRate: 100,
Speed: 700,
UpdateRate: 100,
TicksPerSec: 700,
}
cfg := resource.Config{Name: "enc1", ConvertedAttributes: &ic}
e.Reconfigure(ctx, nil, cfg)

test.That(t, e1.updateRate, test.ShouldEqual, 100)
test.That(t, e1.speed, test.ShouldEqual, 700)
test.That(t, e1.ticksPerSec, test.ShouldEqual, 700)

testutils.WaitForAssertion(t, func(tb testing.TB) {
tb.Helper()
Expand Down

0 comments on commit 773457b

Please sign in to comment.