Skip to main content

Meadow.Foundation.Motors.ElectronicSpeedController

ElectronicSpeedController
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet Gallery for Meadow.Foundation.Motors.ElectronicSpeedController
float frequency =50f;
constfloat armMs =0.5f;
constfloat powerIncrement =0.05f;

ElectronicSpeedController esc;
RotaryEncoderWithButton rotary;

publicMeadowApp()
{
Initialize();
DisplayPowerOnLed(esc.Power);
}

voidInitialize()
{
Console.WriteLine("Initialize hardware...");

//==== rotary encoder
rotary =newRotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
rotary.Rotated += Rotary_Rotated;
rotary.Clicked +=(s, e)=>{
Console.WriteLine($"Arming the device.");
_ = esc.Arm();
};;

//==== Electronic Speed Controller
esc =newElectronicSpeedController(Device, Device.Pins.D02, frequency);

Console.WriteLine("Hardware initialized.");
}

privatevoidRotary_Rotated(object sender,Meadow.Peripherals.Sensors.Rotary.RotaryChangeResult e)
{
esc.Power +=(e.Direction == RotationDirection.Clockwise)? powerIncrement :-powerIncrement;
DisplayPowerOnLed(esc.Power);

Console.WriteLine($"New Power: {esc.Power *(float)100:n0}%");
}

/// <summary>
/// Displays the ESC power on the onboard LED as full red @ `100%`,
/// blue @ `0%`, and a proportional mix, in between those speeds.
/// </summary>
/// <param name="power"></param>
voidDisplayPowerOnLed(float power)
{
// `0.0` - `1.0`
int r =(int)Map(power,0f,1f,0f,255f);
int b =(int)Map(power,0f,1f,255f,0f);

var color = Color.FromRgb(r,0, b);
}

floatMap(floatvalue,float fromSource,float toSource,float fromTarget,float toTarget)
{
return(value- fromSource)/(toSource - fromSource)*(toTarget - fromTarget)+ fromTarget;
}

Sample project(s) available on GitHub

|

Code Example

privatereadonlyFrequency frequency =newFrequency(50, Frequency.UnitType.Hertz);
privateconstfloat armMs =0.5f;
privateconstfloat powerIncrement =0.05f;
privateElectronicSpeedController esc;
privateRotaryEncoderWithButton rotary;

publicoverrideTaskInitialize()
{
Resolver.Log.Info("Initialize...");

rotary =newRotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
rotary.Rotated += RotaryRotated;
rotary.Clicked +=(s, e)=>
{
Resolver.Log.Info($"Arming the device.");
esc.Arm();
};;

esc =newElectronicSpeedController(Device.Pins.D02, frequency);

Resolver.Log.Info("Hardware initialized.");

returnbase.Initialize();
}

privatevoidRotaryRotated(object sender,RotaryChangeResult e)
{
esc.Power +=(e.New == RotationDirection.Clockwise)? powerIncrement :-powerIncrement;
DisplayPowerOnLed(esc.Power);

Resolver.Log.Info($"New Power: {esc.Power *100:n0}%");
}

/// <summary>
/// Displays the ESC power on the onboard LED as full red @ `100%`,
/// blue @ `0%`, and a proportional mix, in between those speeds.
/// </summary>
/// <param name="power"></param>
privatevoidDisplayPowerOnLed(float power)
{
// `0.0` - `1.0`
int r =(int)power.Map(0f,1f,0f,255f);
int b =(int)power.Map(0f,1f,255f,0f);

var color = Color.FromRgb(r,0, b);
}

publicoverrideTaskRun()
{
DisplayPowerOnLed(esc.Power);

returnbase.Run();
}

Sample project(s) available on GitHub

close