Skip to content
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

Feature/add medium linear motor #105

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using SharpBrick.PoweredUp;

namespace Example
{
public class ExampleTwoPortHubMediumLinearMotor : BaseExample
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually add the test run to the Program.cs commented out ... like that it can be easier discovered. Fix it on the next PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good idea! Will take care of that in the next PR

{
public override async Task ExecuteAsync()
{
using (var twoPortHub = Host.FindByType<TwoPortHub>())
{
await twoPortHub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
.AddHub<TwoPortHub>(hubBuilder => hubBuilder
.AddDevice<MediumLinearMotor>(twoPortHub.A)
)
);

var motor = twoPortHub.A.GetDevice<MediumLinearMotor>();

await motor.SetAccelerationTimeAsync(3000);
await motor.SetDecelerationTimeAsync(1000);
await motor.StartSpeedForTimeAsync(6000, 90, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile | SpeedProfiles.DecelerationProfile);

await Task.Delay(10_000);

await motor.StartSpeedForDegreesAsync(180, -10, 100, SpecialSpeed.Brake, SpeedProfiles.None);

await Task.Delay(10_000);

await motor.StartSpeedAsync(100, 90, SpeedProfiles.None);
await Task.Delay(2000);
await motor.StartSpeedAsync(-100, 90, SpeedProfiles.None);
await Task.Delay(2000);
await motor.StartSpeedAsync(0, 90, SpeedProfiles.None);

await Task.Delay(10_000);

await twoPortHub.SwitchOffAsync();
}
}
}
}
2 changes: 2 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Type GetTypeFromDeviceType(DeviceType deviceType)
DeviceType.TechnicMediumHubGyroSensor => typeof(TechnicMediumHubGyroSensor),
DeviceType.TechnicMediumHubTiltSensor => typeof(TechnicMediumHubTiltSensor),
DeviceType.TechnicMediumHubTemperatureSensor => typeof(TechnicMediumHubTemperatureSensor),
DeviceType.MediumLinearMotor => typeof(MediumLinearMotor),
_ => null,
};

Expand All @@ -56,6 +57,7 @@ public static DeviceType GetDeviceTypeFromType(Type type)
nameof(TechnicMediumHubGyroSensor) => DeviceType.TechnicMediumHubGyroSensor,
nameof(TechnicMediumHubTiltSensor) => DeviceType.TechnicMediumHubTiltSensor,
nameof(TechnicMediumHubTemperatureSensor) => DeviceType.TechnicMediumHubTemperatureSensor,
nameof(MediumLinearMotor) => DeviceType.MediumLinearMotor,
_ => DeviceType.Unknown,
};
}
Expand Down
45 changes: 45 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/MediumLinearMotor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SharpBrick.PoweredUp.Protocol;
using SharpBrick.PoweredUp.Utils;

namespace SharpBrick.PoweredUp
{
public class MediumLinearMotor : TachoMotor, IPoweredUpDevice
{
public MediumLinearMotor()
: base()
{ }
public MediumLinearMotor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{ }

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
=> @"
0B-00-43-00-01-07-03-06-00-07-00
07-00-43-00-02-06-00
12-00-44-00-00-00-50-4F-57-45-52-00-00-00-00-00-00-00
0E-00-44-00-00-01-00-00-C8-C2-00-00-C8-42
0E-00-44-00-00-02-00-00-C8-C2-00-00-C8-42
0E-00-44-00-00-03-00-00-C8-C2-00-00-C8-42
0B-00-44-00-00-04-50-43-54-00-00
08-00-44-00-00-05-00-10
0A-00-44-00-00-80-01-00-01-00
12-00-44-00-01-00-53-50-45-45-44-00-00-00-00-00-00-00
0E-00-44-00-01-01-00-00-C8-C2-00-00-C8-42
0E-00-44-00-01-02-00-00-C8-C2-00-00-C8-42
0E-00-44-00-01-03-00-00-C8-C2-00-00-C8-42
0B-00-44-00-01-04-50-43-54-00-00
08-00-44-00-01-05-10-10
0A-00-44-00-01-80-01-00-04-00
12-00-44-00-02-00-50-4F-53-00-00-00-00-00-00-00-00-00
0E-00-44-00-02-01-00-00-B4-C3-00-00-B4-43
0E-00-44-00-02-02-00-00-C8-C2-00-00-C8-42
0E-00-44-00-02-03-00-00-B4-C3-00-00-B4-43
0B-00-44-00-02-04-44-45-47-00-00
08-00-44-00-02-05-08-08
0A-00-44-00-02-80-01-02-04-00
".Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s));
}
}
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp/Enums/DeviceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum DeviceType : ushort
ExternalTiltSensor = 0x0022, // TILT_SENSOR
MotionSensor = 0x0023, // MOTION_SENSOR
VisionSensor = 0x0025, // COLOR_DISTANCE_SENSOR
ExternalMotorWithTacho = 0x0026, // MEDIUM_LINEAR_MOTOR
MediumLinearMotor = 0x0026, // MEDIUM_LINEAR_MOTOR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming choice is perfect. I copied these from poweredup-node ... but they have history.

Normally, this would apply for a breaking change. But honestly, the motor is not implemented so I cannot imagine why someone would use this enum value.

Therefore, fine with me!

InternalMotorWithTacho = 0x0027, // MOVE_HUB_MEDIUM_LINEAR_MOTOR
InternalTilt = 0x0028, // MOVE_HUB_TILT_SENSOR

Expand Down