Skip to content

Commit 689e3b1

Browse files
committed
Added StartSpeed, StartPower, StartSpeedForTime
- Added Encoder, Unit Tests - Added Code to BasicMotor and Technic Motors
1 parent 9f153b9 commit 689e3b1

File tree

9 files changed

+190
-22
lines changed

9 files changed

+190
-22
lines changed

examples/message-trace/Program.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,20 @@ await protocol.ReceiveMessageAsync(message =>
157157
await rgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
158158
//await rgbLight.SetRgbColorsAsync(0xFF, 0x00, 0x00);
159159

160+
var motor = new TechnicXLargeLinearMotor(protocol, 0);
161+
162+
await motor.StartSpeedForTimeAsync(3000, 90, 100, PortOutputCommandSpecialSpeed.Hold, PortOutputCommandSpeedProfile.None);
163+
// await motor.StartSpeedAsync(100, 90, PortOutputCommandSpeedProfile.None);
164+
165+
// await Task.Delay(2000);
166+
167+
// await motor.StartSpeedAsync(127, 90, PortOutputCommandSpeedProfile.None);
168+
169+
// await motor.StartSpeedAsync(-100, 90, PortOutputCommandSpeedProfile.None);
170+
// await Task.Delay(2000);
171+
172+
// await motor.StartSpeedAsync(0, 90, PortOutputCommandSpeedProfile.None);
173+
160174
Console.ReadLine();
161175

162176
logger.LogInformation("Switch off device");
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using SharpBrick.PoweredUp.Protocol;
4+
using SharpBrick.PoweredUp.Protocol.Messages;
5+
6+
namespace SharpBrick.PoweredUp.Devices
7+
{
8+
public abstract class BasicMotor
9+
{
10+
protected readonly PoweredUpProtocol _protocol;
11+
protected readonly byte _portId;
12+
13+
public BasicMotor()
14+
{ }
15+
public BasicMotor(PoweredUpProtocol protocol, byte portId)
16+
{
17+
_protocol = protocol ?? throw new ArgumentNullException(nameof(protocol));
18+
_portId = portId;
19+
}
20+
21+
public async Task StartPowerAsync(sbyte power)
22+
{
23+
await _protocol.SendMessageAsync(new PortOutputCommandStartPowerMessage()
24+
{
25+
PortId = _portId,
26+
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
27+
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
28+
Power = power,
29+
});
30+
}
31+
32+
public async Task StartSpeedAsync(sbyte speed, byte maxPower, PortOutputCommandSpeedProfile profile)
33+
{
34+
await _protocol.SendMessageAsync(new PortOutputCommandStartSpeedMessage()
35+
{
36+
PortId = _portId,
37+
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
38+
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
39+
Speed = speed,
40+
MaxPower = maxPower,
41+
Profile = profile,
42+
});
43+
}
44+
45+
public async Task StartSpeedForTimeAsync(ushort time, sbyte speed, byte maxPower, PortOutputCommandSpecialSpeed endState, PortOutputCommandSpeedProfile profile)
46+
{
47+
await _protocol.SendMessageAsync(new PortOutputCommandStartSpeedForTimeMessage()
48+
{
49+
PortId = _portId,
50+
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
51+
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
52+
Time = time,
53+
Speed = speed,
54+
MaxPower = maxPower,
55+
EndState = endState,
56+
Profile = profile,
57+
});
58+
}
59+
}
60+
}

src/SharpBrick.PoweredUp/Devices/TechnicLargeLinearMotor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using SharpBrick.PoweredUp.Knowledge;
5-
using SharpBrick.PoweredUp.Protocol.Messages;
4+
using SharpBrick.PoweredUp.Protocol;
65
using SharpBrick.PoweredUp.Utils;
76

87
namespace SharpBrick.PoweredUp.Devices
98
{
10-
public class TechnicLargeLinearMotor : IPowerdUpDevice
9+
public class TechnicLargeLinearMotor : BasicMotor, IPowerdUpDevice
1110
{
11+
public TechnicLargeLinearMotor()
12+
: base()
13+
{ }
14+
public TechnicLargeLinearMotor(PoweredUpProtocol protocol, byte portId)
15+
: base(protocol, portId)
16+
{ }
17+
1218
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version sw, Version hw)
1319
=> @"
1420
0B-00-43-00-01-0F-06-1E-00-1F-00

src/SharpBrick.PoweredUp/Devices/TechnicXLargeLinearMotor.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using SharpBrick.PoweredUp.Knowledge;
5-
using SharpBrick.PoweredUp.Protocol.Messages;
4+
using SharpBrick.PoweredUp.Protocol;
65
using SharpBrick.PoweredUp.Utils;
76

87
namespace SharpBrick.PoweredUp.Devices
98
{
10-
public class TechnicXLargeLinearMotor : IPowerdUpDevice
9+
10+
public class TechnicXLargeLinearMotor : BasicMotor, IPowerdUpDevice
1111
{
12+
public TechnicXLargeLinearMotor()
13+
: base()
14+
{ }
15+
public TechnicXLargeLinearMotor(PoweredUpProtocol protocol, byte portId)
16+
: base(protocol, portId)
17+
{ }
18+
1219
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version sw, Version hw)
1320
=> @"
1421
0B-00-43-02-01-0F-06-1E-00-1F-00

src/SharpBrick.PoweredUp/Protocol/Formatter/PortOutputCommandEncoder.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ public ushort CalculateContentLength(PoweredUpMessage message)
1010
{
1111
PortOutputCommandMessage portOutputMessage => 3 + portOutputMessage switch
1212
{
13+
PortOutputCommandStartSpeedMessage msg => 3,
14+
PortOutputCommandStartSpeedForTimeMessage msg => 6,
1315
PortOutputCommandWriteDirectModeDataMessage directWriteModeDataMessage => 1 + directWriteModeDataMessage switch
1416
{
17+
PortOutputCommandStartPowerMessage msg => 1,
1518
PortOutputCommandSetRgbColorNoMessage msg => 1,
1619
PortOutputCommandSetRgbColorNo2Message msg => 3,
1720

@@ -38,11 +41,30 @@ public void Encode(PortOutputCommandMessage message, in Span<byte> data)
3841

3942
switch (message)
4043
{
44+
case PortOutputCommandStartSpeedMessage msg:
45+
data[3] = (byte)msg.Speed;
46+
data[4] = msg.MaxPower;
47+
data[5] = (byte)msg.Profile;
48+
49+
break;
50+
51+
case PortOutputCommandStartSpeedForTimeMessage msg:
52+
BitConverter.TryWriteBytes(data.Slice(3, 2), msg.Time);
53+
data[5] = (byte)msg.Speed;
54+
data[6] = msg.MaxPower;
55+
data[7] = (byte)msg.EndState;
56+
data[8] = (byte)msg.Profile;
57+
58+
break;
4159
case PortOutputCommandWriteDirectModeDataMessage directWriteModeDataMessage:
4260
data[3] = directWriteModeDataMessage.Mode;
4361

4462
switch (directWriteModeDataMessage)
4563
{
64+
case PortOutputCommandStartPowerMessage msg:
65+
data[4] = (byte)msg.Power;
66+
break;
67+
4668
case PortOutputCommandSetRgbColorNoMessage msg:
4769
data[4] = (byte)msg.ColorNo;
4870
break;

src/SharpBrick.PoweredUp/Protocol/Messages/PortOutputCommandMessage.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class PortOutputCommandStartSpeedMessage : PortOutputCommandMessage
6666
{
6767
public PortOutputCommandStartSpeedMessage() : base(PortOutputSubCommand.StartSpeed) { }
6868
public sbyte Speed { get; set; }
69-
public sbyte MaxPower { get; set; }
69+
public byte MaxPower { get; set; }
7070
public PortOutputCommandSpeedProfile Profile { get; set; }
7171
}
7272

@@ -76,30 +76,30 @@ public class PortOutputCommandStartSpeed2Message : PortOutputCommandMessage
7676
public PortOutputCommandStartSpeed2Message() : base(PortOutputSubCommand.StartSpeed2) { }
7777
public sbyte Speed1 { get; set; }
7878
public sbyte Speed2 { get; set; }
79-
public sbyte MaxPower { get; set; }
79+
public byte MaxPower { get; set; }
8080
public PortOutputCommandSpeedProfile Profile { get; set; }
8181
}
8282

8383
// spec chapter: 3.27.7
8484
public class PortOutputCommandStartSpeedForTimeMessage : PortOutputCommandMessage
8585
{
8686
public PortOutputCommandStartSpeedForTimeMessage() : base(PortOutputSubCommand.StartSpeedForTime) { }
87-
public short Time { get; set; }
87+
public ushort Time { get; set; }
8888
public sbyte Speed { get; set; }
89-
public sbyte MaxPower { get; set; }
90-
public byte EndState { get; set; }
89+
public byte MaxPower { get; set; }
90+
public PortOutputCommandSpecialSpeed EndState { get; set; }
9191
public PortOutputCommandSpeedProfile Profile { get; set; }
9292
}
9393

9494
// spec chapter: 3.27.8
9595
public class PortOutputCommandStartSpeedForTime2Message : PortOutputCommandMessage
9696
{
9797
public PortOutputCommandStartSpeedForTime2Message() : base(PortOutputSubCommand.StartSpeedForTime2) { }
98-
public short Time { get; set; }
98+
public ushort Time { get; set; }
9999
public sbyte Speed1 { get; set; }
100100
public sbyte Speed2 { get; set; }
101-
public sbyte MaxPower { get; set; }
102-
public byte EndState { get; set; }
101+
public byte MaxPower { get; set; }
102+
public PortOutputCommandSpecialSpeed EndState { get; set; }
103103
public PortOutputCommandSpeedProfile Profile { get; set; }
104104
}
105105

@@ -109,8 +109,8 @@ public class PortOutputCommandStartSpeedForDegreesMessage : PortOutputCommandMes
109109
public PortOutputCommandStartSpeedForDegreesMessage() : base(PortOutputSubCommand.StartSpeedForDegrees) { }
110110
public int Degrees { get; set; }
111111
public sbyte Speed { get; set; }
112-
public sbyte MaxPower { get; set; }
113-
public byte EndState { get; set; }
112+
public byte MaxPower { get; set; }
113+
public PortOutputCommandSpecialSpeed EndState { get; set; }
114114
public PortOutputCommandSpeedProfile Profile { get; set; }
115115
}
116116

@@ -121,8 +121,8 @@ public PortOutputCommandStartSpeedForDegrees2Message() : base(PortOutputSubComma
121121
public int Degrees { get; set; }
122122
public sbyte Speed1 { get; set; }
123123
public sbyte Speed2 { get; set; }
124-
public sbyte MaxPower { get; set; }
125-
public byte EndState { get; set; }
124+
public byte MaxPower { get; set; }
125+
public PortOutputCommandSpecialSpeed EndState { get; set; }
126126
public PortOutputCommandSpeedProfile Profile { get; set; }
127127
}
128128

@@ -132,8 +132,8 @@ public class PortOutputCommandGotoAbsolutePositionMessage : PortOutputCommandMes
132132
public PortOutputCommandGotoAbsolutePositionMessage() : base(PortOutputSubCommand.GotoAbsolutePosition) { }
133133
public int AbsPos { get; set; }
134134
public sbyte Speed { get; set; }
135-
public sbyte MaxPower { get; set; }
136-
public byte EndState { get; set; }
135+
public byte MaxPower { get; set; }
136+
public PortOutputCommandSpecialSpeed EndState { get; set; }
137137
public PortOutputCommandSpeedProfile Profile { get; set; }
138138
}
139139
// spec chapter: 3.27.12
@@ -143,8 +143,8 @@ public PortOutputCommandGotoAbsolutePosition2Message() : base(PortOutputSubComma
143143
public int AbsPos1 { get; set; }
144144
public int AbsPos2 { get; set; }
145145
public sbyte Speed { get; set; }
146-
public sbyte MaxPower { get; set; }
147-
public byte EndState { get; set; }
146+
public byte MaxPower { get; set; }
147+
public PortOutputCommandSpecialSpeed EndState { get; set; }
148148
public PortOutputCommandSpeedProfile Profile { get; set; }
149149
}
150150

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SharpBrick.PoweredUp.Protocol.Messages
2+
{
3+
public enum PortOutputCommandSpecialSpeed : byte
4+
{
5+
Float = 0,
6+
Hold = 126,
7+
Brake = 127,
8+
}
9+
}

src/SharpBrick.PoweredUp/Protocol/Messages/PortOutputCommandSpeedProfile.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
using System;
2+
13
namespace SharpBrick.PoweredUp.Protocol.Messages
24
{
5+
[Flags]
36
public enum PortOutputCommandSpeedProfile : byte
47
{
8+
None = 0x00,
59
AccelerationProfile = 0x01,
610
DeccelerationProfile = 0x02,
711
}

test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortOutputCommandEncoderTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using SharpBrick.PoweredUp.Devices;
23
using SharpBrick.PoweredUp.Protocol.Messages;
34
using SharpBrick.PoweredUp.Utils;
45
using Xunit;
@@ -7,6 +8,51 @@ namespace SharpBrick.PoweredUp.Protocol.Formatter
78
{
89
public class PortOutputCommandEncoderTest
910
{
11+
[Theory]
12+
[InlineData("09-00-81-00-11-07-64-5A-03", 0, 100, 90, PortOutputCommandSpeedProfile.AccelerationProfile | PortOutputCommandSpeedProfile.DeccelerationProfile)]
13+
[InlineData("09-00-81-00-11-07-7F-5A-03", 0, 127, 90, PortOutputCommandSpeedProfile.AccelerationProfile | PortOutputCommandSpeedProfile.DeccelerationProfile)]
14+
[InlineData("09-00-81-00-11-07-9C-5A-03", 0, -100, 90, PortOutputCommandSpeedProfile.AccelerationProfile | PortOutputCommandSpeedProfile.DeccelerationProfile)]
15+
[InlineData("09-00-81-00-11-07-00-5A-03", 0, 0, 90, PortOutputCommandSpeedProfile.AccelerationProfile | PortOutputCommandSpeedProfile.DeccelerationProfile)]
16+
public void PortOutputCommandEncoder_Encode_PortOutputCommandStartSpeedMessage(string expectedData, byte port, sbyte speed, byte maxPower, PortOutputCommandSpeedProfile profile)
17+
=> PortOutputCommandEncoder_Encode(expectedData, new PortOutputCommandStartSpeedMessage()
18+
{
19+
PortId = port,
20+
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
21+
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
22+
Speed = speed,
23+
MaxPower = maxPower,
24+
Profile = profile,
25+
});
26+
27+
[Theory]
28+
[InlineData("0C-00-81-00-11-09-B8-0B-5A-64-7E-00", 0, 3000, 90, 100, PortOutputCommandSpecialSpeed.Hold, PortOutputCommandSpeedProfile.None)]
29+
public void PortOutputCommandEncoder_Encode_PortOutputCommandStartSpeedForTimeMessage(string expectedData, byte port, ushort time, sbyte speed, byte maxPower, PortOutputCommandSpecialSpeed endState, PortOutputCommandSpeedProfile profile)
30+
=> PortOutputCommandEncoder_Encode(expectedData, new PortOutputCommandStartSpeedForTimeMessage()
31+
{
32+
PortId = port,
33+
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
34+
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
35+
Time = time,
36+
Speed = speed,
37+
MaxPower = maxPower,
38+
EndState = endState,
39+
Profile = profile,
40+
});
41+
42+
[Theory]
43+
[InlineData("08-00-81-00-11-51-00-64", 0, 100)]
44+
[InlineData("08-00-81-00-11-51-00-7F", 0, 127)]
45+
[InlineData("08-00-81-00-11-51-00-9C", 0, -100)]
46+
[InlineData("08-00-81-00-11-51-00-00", 0, 0)]
47+
public void PortOutputCommandEncoder_Encode_PortOutputCommandStartPowerMessage(string expectedData, byte port, sbyte power)
48+
=> PortOutputCommandEncoder_Encode(expectedData, new PortOutputCommandStartPowerMessage()
49+
{
50+
PortId = port,
51+
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
52+
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
53+
Power = power
54+
});
55+
1056
[Theory]
1157
[InlineData("0A-00-81-32-11-51-01-00-FF-00", 50, 0x00, 0xFF, 0x00)]
1258
public void PortOutputCommandEncoder_Encode_PortOutputCommandSetRgbColorNo2Message(string expectedData, byte port, byte r, byte g, byte b)

0 commit comments

Comments
 (0)