Skip to content

Commit d8c3467

Browse files
committed
Add StopByBrake and StopByFloat, Guarding
- BasicMotor StopByBrake and StopByFloat - Guarding for methods whether device/hub is connected - Fix naming of parameter sharpbrick#8
1 parent a5f02e4 commit d8c3467

File tree

8 files changed

+295
-31
lines changed

8 files changed

+295
-31
lines changed

Diff for: examples/SharpBrick.PoweredUp.Examples/ExampleMotorControl.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public static async Task ExecuteAsync()
1414
var motor = technicMediumHub.A.GetDevice<TechnicXLargeLinearMotor>();
1515

1616
await motor.SetAccelerationTimeAsync(3000);
17-
await motor.SetDeccelerationTimeAsync(1000);
18-
await motor.StartSpeedForTimeAsync(6000, 90, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile | SpeedProfiles.DeccelerationProfile);
17+
await motor.SetDecelerationTimeAsync(1000);
18+
await motor.StartSpeedForTimeAsync(6000, 90, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile | SpeedProfiles.DecelerationProfile);
1919

2020
await Task.Delay(10_000);
2121

Diff for: src/SharpBrick.PoweredUp/Devices/AbsoluteMotor.cs

+29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public abstract class AbsoluteMotor : TachoMotor
1515

1616
public AbsoluteMotor()
1717
{ }
18+
1819
protected AbsoluteMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
1920
: base(protocol, hubId, portId)
2021
{
@@ -23,8 +24,21 @@ protected AbsoluteMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
2324
ObserveOnLocalProperty(AbsolutePositionObservable, v => AbsolutePosition = v.SI, v => AbsolutePositionPct = v.Pct);
2425
}
2526

27+
/// <summary>
28+
/// Start the motor with a speed of Speed using a maximum power of MaxPower and GOTO the Absolute position AbsPos. After position is reached the motor is stopped using the EndState mode of operation.
29+
/// </summary>
30+
/// <param name="absolutePosition">Absolute Position (0 is the position at the start of the motor)</param>
31+
/// <param name="speed">The speed used to move to the absolute position.</param>
32+
/// <param name="maxPower">Maximum Power level used.</param>
33+
/// <param name="endState">After time has expired, either Float, Hold or Brake.</param>
34+
/// <param name="profile">The speed profiles used (as flags) for acceleration and deceleration</param>
35+
/// <returns></returns>
2636
public async Task GotoAbsolutePositionAsync(int absolutePosition, sbyte speed, byte maxPower, SpecialSpeed endState, SpeedProfiles profile)
2737
{
38+
AssertValidSpeed(speed, nameof(speed));
39+
AssertValidMaxPower(maxPower, nameof(maxPower));
40+
AssertIsConnected();
41+
2842
await _protocol.SendMessageAsync(new PortOutputCommandGotoAbsolutePositionMessage()
2943
{
3044
HubId = _hubId,
@@ -39,8 +53,23 @@ await _protocol.SendMessageAsync(new PortOutputCommandGotoAbsolutePositionMessag
3953
});
4054
}
4155

56+
/// <summary>
57+
/// Start the motors with individual speeds calculated from the common given Speed using a powerlevel limited to the MaxPower value. And the GOTO the Absolute positions: AbsPos1 and AbsPos2.
58+
/// </summary>
59+
/// <param name="absolutePosition1">Absolute Position of motor 1 (0 is the position at the start of the motor)</param>
60+
/// <param name="absolutePosition2">Absolute Position of motor 2 (0 is the position at the start of the motor)</param>
61+
/// <param name="speed">The speed used to move to the absolute position.</param>
62+
/// <param name="maxPower">Maximum Power level used.</param>
63+
/// <param name="endState">After time has expired, either Float, Hold or Brake.</param>
64+
/// <param name="profile">The speed profiles used (as flags) for acceleration and deceleration</param>
65+
/// <returns></returns>
4266
public async Task GotoAbsolutePositionAsync(int absolutePosition1, int absolutePosition2, sbyte speed, byte maxPower, SpecialSpeed endState, SpeedProfiles profile)
4367
{
68+
AssertValidSpeed(speed, nameof(speed));
69+
AssertValidMaxPower(maxPower, nameof(maxPower));
70+
AssertIsConnected();
71+
AssertIsVirtualPort();
72+
4473
await _protocol.SendMessageAsync(new PortOutputCommandGotoAbsolutePosition2Message()
4574
{
4675
HubId = _hubId,

Diff for: src/SharpBrick.PoweredUp/Devices/BasicMotor.cs

+62-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@ public BasicMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
2424
ObserveOnLocalProperty(PowerObservable, v => Power = v.SI, v => PowerPct = v.Pct);
2525
}
2626

27+
/// <summary>
28+
/// Starts the motor with full speed at the given power level.
29+
/// </summary>
30+
/// <param name="power">
31+
/// - Power levels in percentage: 1 - 100 (CW), -1 - -100 (CCW)
32+
/// - Stop Motor (floating): 0
33+
/// - Stop Motor (breaking): 127
34+
/// </param>
35+
/// <returns>An awaitable Task.</returns>
2736
public async Task StartPowerAsync(sbyte power)
2837
{
38+
AssertValidPower(power, nameof(power));
39+
AssertIsConnected();
40+
2941
await _protocol.SendMessageAsync(new PortOutputCommandStartPowerMessage()
3042
{
3143
HubId = _hubId,
@@ -36,15 +48,61 @@ await _protocol.SendMessageAsync(new PortOutputCommandStartPowerMessage()
3648
});
3749
}
3850

39-
public Task StartPowerAsync(sbyte power1, sbyte power2)
40-
=> _protocol.SendMessageAsync(new PortOutputCommandStartPower2Message()
51+
/// <summary>
52+
/// Stops the motor (brake; no movement afterwards)
53+
/// </summary>
54+
/// <returns></returns>
55+
public Task StopByBrakeAsync()
56+
=> StartPowerAsync((sbyte)SpecialSpeed.Brake);
57+
58+
/// <summary>
59+
/// Stops the motor (float; freely floating by inertia)
60+
/// </summary>
61+
/// <returns></returns>
62+
public Task StopByFloatAsync()
63+
=> StartPowerAsync((sbyte)SpecialSpeed.Float);
64+
65+
/// <summary>
66+
/// Start the pair on motors with full speed on given power values.
67+
/// </summary>
68+
/// <param name="powerOnMotor1">
69+
/// - Power levels in percentage: 1 - 100 (CW), -1 - -100 (CCW)
70+
/// - Stop Motor (floating): 0
71+
/// - Stop Motor (breaking): 127
72+
/// </param>
73+
/// <param name="powerOnMotor2">
74+
/// - Power levels in percentage: 1 - 100 (CW), -1 - -100 (CCW)
75+
/// - Stop Motor (floating): 0
76+
/// - Stop Motor (breaking): 127
77+
/// </param>
78+
/// <returns></returns>
79+
public async Task StartPowerAsync(sbyte powerOnMotor1, sbyte powerOnMotor2)
80+
{
81+
AssertValidPower(powerOnMotor1, nameof(powerOnMotor1));
82+
AssertValidPower(powerOnMotor2, nameof(powerOnMotor2));
83+
AssertIsConnected();
84+
AssertIsVirtualPort();
85+
86+
await _protocol.SendMessageAsync(new PortOutputCommandStartPower2Message()
4187
{
4288
HubId = _hubId,
4389
PortId = _portId,
4490
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
4591
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
46-
Power1 = power1,
47-
Power2 = power2,
92+
Power1 = powerOnMotor1,
93+
Power2 = powerOnMotor2,
4894
});
95+
}
96+
97+
protected void AssertValidPower(sbyte power, string argumentName)
98+
{
99+
if (
100+
power < -100 ||
101+
(power > 100 && power != 127)
102+
)
103+
{
104+
throw new ArgumentOutOfRangeException(argumentName);
105+
}
106+
}
49107
}
50108
}

Diff for: src/SharpBrick.PoweredUp/Devices/Device.cs

+13
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage()
7777
});
7878
}
7979

80+
protected void AssertIsConnected()
81+
{
82+
if (!IsConnected)
83+
{
84+
throw new InvalidOperationException("The device needs to be connected to a protocol.");
85+
}
86+
}
87+
88+
protected void AssertIsVirtualPort()
89+
{
90+
//TODO
91+
}
92+
8093
#region Disposable Pattern
8194
private bool disposedValue;
8295
protected virtual void Dispose(bool disposing)

Diff for: src/SharpBrick.PoweredUp/Devices/RgbLight.cs

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public RgbLight(IPoweredUpProtocol protocol, byte hubId, byte portId)
1919

2020
public async Task SetRgbColorNoAsync(PoweredUpColor color)
2121
{
22+
AssertIsConnected();
23+
2224
await _protocol.SendMessageAsync(new PortOutputCommandSetRgbColorNoMessage()
2325
{
2426
HubId = _hubId,
@@ -30,6 +32,8 @@ await _protocol.SendMessageAsync(new PortOutputCommandSetRgbColorNoMessage()
3032
}
3133
public async Task SetRgbColorsAsync(byte red, byte green, byte blue)
3234
{
35+
AssertIsConnected();
36+
3337
await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage()
3438
{
3539
HubId = _hubId,

0 commit comments

Comments
 (0)