Skip to content

Commit

Permalink
RSDK-8878: gantry shouldn't look for the second limit switch until it…
Browse files Browse the repository at this point in the history
… has cleared the first limit switch (#4400)
  • Loading branch information
martha-johnston authored Oct 1, 2024
1 parent d8a1647 commit 38f055d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
8 changes: 7 additions & 1 deletion components/gantry/singleaxis/singleaxis.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,14 @@ func (g *singleAxis) testLimit(ctx context.Context, pin int) (float64, error) {
}
// Short pause after stopping to increase the precision of the position of each limit switch
position, err := g.motor.Position(ctx, nil)
if err != nil {
return position, err
}
time.Sleep(250 * time.Millisecond)
return position, err
if err := g.moveAway(ctx, pin); err != nil {
return position, err
}
return position, nil
}

// this function may need to be run in the background upon initialisation of the ganty,
Expand Down
61 changes: 26 additions & 35 deletions components/gantry/singleaxis/singleaxis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ var fakeFrame = &referenceframe.LinkConfig{

var badFrame = &referenceframe.LinkConfig{}

var (
count = 0
pinValues = []int{1, 1, 0}
)
// alternate high and low to replicate the behavior of a gantry hitting and moving away from a limit switch during homing.
var defaultPinValues = []int{1, 0, 1, 0, 1, 0, 1, 0}

func createFakeMotor() motor.Motor {
return &inject.Motor{
PropertiesFunc: func(ctx context.Context, extra map[string]interface{}) (motor.Properties, error) {
return motor.Properties{PositionReporting: true}, nil
},
PositionFunc: func(ctx context.Context, extra map[string]interface{}) (float64, error) {
return float64(count + 1), nil
return 1.0, nil
},
ResetZeroPositionFunc: func(ctx context.Context, offset float64, extra map[string]interface{}) error { return nil },
GoToFunc: func(ctx context.Context, rpm, position float64, extra map[string]interface{}) error { return nil },
Expand All @@ -51,26 +49,19 @@ func createFakeMotor() motor.Motor {
}
}

func createLimitBoard() board.Board {
injectGPIOPin := &inject.GPIOPin{
GetFunc: func(ctx context.Context, extra map[string]interface{}) (bool, error) { return true, nil },
SetFunc: func(ctx context.Context, high bool, extra map[string]interface{}) error { return nil },
}
return &inject.Board{GPIOPinByNameFunc: func(pin string) (board.GPIOPin, error) { return injectGPIOPin, nil }}
}

func createFakeBoard() board.Board {
func createFakeBoard(pinValues []int) board.Board {
pinCount := 0
injectGPIOPin := &inject.GPIOPin{
GetFunc: func(ctx context.Context, extra map[string]interface{}) (bool, error) {
pinVal := false
if pinValues[pinCount] == 1 {
return true, nil
pinVal = true
}
pinCount++
if pinCount == len(pinValues) {
pinCount = 0
}
return false, nil
return pinVal, nil
},
SetFunc: func(ctx context.Context, high bool, extra map[string]interface{}) error { return nil },
}
Expand All @@ -80,7 +71,7 @@ func createFakeBoard() board.Board {
func createFakeDepsForTestNewSingleAxis(t *testing.T) resource.Dependencies {
t.Helper()
deps := make(resource.Dependencies)
deps[board.Named(boardName)] = createFakeBoard()
deps[board.Named(boardName)] = createFakeBoard(defaultPinValues)
deps[motor.Named(motorName)] = createFakeMotor()
return deps
}
Expand Down Expand Up @@ -206,7 +197,7 @@ func TestNewSingleAxis(t *testing.T) {
}
deps = make(resource.Dependencies)
deps[motor.Named(motorName)] = injectMotor
deps[board.Named(boardName)] = createFakeBoard()
deps[board.Named(boardName)] = createFakeBoard(defaultPinValues)

_, err = newSingleAxis(ctx, deps, fakecfg, logger)
test.That(t, err.Error(), test.ShouldContainSubstring, "invalid gantry type")
Expand All @@ -221,7 +212,7 @@ func TestNewSingleAxis(t *testing.T) {

deps = make(resource.Dependencies)
deps[motor.Named(motorName)] = injectMotor
deps[board.Named(boardName)] = createFakeBoard()
deps[board.Named(boardName)] = createFakeBoard(defaultPinValues)
properties, _ := injectMotor.Properties(ctx, nil)
_, err = newSingleAxis(ctx, deps, fakecfg, logger)
expectedErr := motor.NewPropertyUnsupportedError(properties, motorName)
Expand Down Expand Up @@ -276,7 +267,7 @@ func TestHome(t *testing.T) {
logger := logging.NewTestLogger(t)
fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand Down Expand Up @@ -316,7 +307,7 @@ func TestHome(t *testing.T) {

fakegantry = &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -337,7 +328,7 @@ func TestHome(t *testing.T) {

fakegantry = &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -354,7 +345,7 @@ func TestHomeLimitSwitch(t *testing.T) {
logger := logging.NewTestLogger(t)
fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand Down Expand Up @@ -451,7 +442,7 @@ func TestHomeLimitSwitch2(t *testing.T) {
logger := logging.NewTestLogger(t)
fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand Down Expand Up @@ -536,7 +527,7 @@ func TestTestLimit(t *testing.T) {
fakegantry := &singleAxis{
limitSwitchPins: []string{"1", "2"},
motor: createFakeMotor(),
board: createLimitBoard(),
board: createFakeBoard(defaultPinValues),
rpm: float64(300),
limitHigh: true,
opMgr: operation.NewSingleOperationManager(),
Expand All @@ -551,7 +542,7 @@ func TestTestLimitTimeout(t *testing.T) {
fakegantry := &singleAxis{
limitSwitchPins: []string{"1", "2"},
motor: createFakeMotor(),
board: createLimitBoard(),
board: createFakeBoard(defaultPinValues),
rpm: float64(3000),
limitHigh: true,
opMgr: operation.NewSingleOperationManager(),
Expand Down Expand Up @@ -579,7 +570,7 @@ func TestLimitHit(t *testing.T) {
ctx := context.Background()
fakegantry := &singleAxis{
limitSwitchPins: []string{"1", "2", "3"},
board: createLimitBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
opMgr: operation.NewSingleOperationManager(),
}
Expand All @@ -601,7 +592,7 @@ func TestPosition(t *testing.T) {
},
PositionFunc: func(ctx context.Context, extra map[string]interface{}) (float64, error) { return 1, nil },
},
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
positionLimits: []float64{0, 1},
positionRange: 1.0,
limitHigh: true,
Expand All @@ -622,7 +613,7 @@ func TestPosition(t *testing.T) {
return 1, errors.New("not supported")
},
},
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
limitSwitchPins: []string{"1", "2"},
positionLimits: []float64{0, 1},
Expand Down Expand Up @@ -650,7 +641,7 @@ func TestMoveToPosition(t *testing.T) {
logger := logging.NewTestLogger(t)
fakegantry := &singleAxis{
logger: logger,
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
motor: createFakeMotor(),
limitHigh: true,
positionRange: 10,
Expand Down Expand Up @@ -752,7 +743,7 @@ func TestStop(t *testing.T) {

fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -771,7 +762,7 @@ func TestCurrentInputs(t *testing.T) {

fakegantry := &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -788,7 +779,7 @@ func TestCurrentInputs(t *testing.T) {

fakegantry = &singleAxis{
motor: createFakeMotor(),
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: true,
logger: logger,
rpm: float64(300),
Expand All @@ -810,7 +801,7 @@ func TestCurrentInputs(t *testing.T) {
return 5, errors.New("nope")
},
},
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitHigh: false,
logger: logger,
rpm: float64(300),
Expand Down Expand Up @@ -846,7 +837,7 @@ func TestGoToInputs(t *testing.T) {
test.That(t, err.Error(), test.ShouldContainSubstring, "is homed")

fakegantry := &singleAxis{
board: createFakeBoard(),
board: createFakeBoard(defaultPinValues),
limitSwitchPins: []string{"1", "2"},
limitHigh: true,
motor: createFakeMotor(),
Expand Down

0 comments on commit 38f055d

Please sign in to comment.