Project updated to V1.0 Release Candidate 1 (October 23rd, 2022)
In this project we're going to learn how easy is to build your first rover using a few LEDs, a couple of motors and a SN754410N chip to control them. Everything you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro. We'll see how easy is to program these peripherals using Meadow.Foundation.
Meadow.Foundation a platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LED project to properly set up your development environment.
Step 1 - Assemble the circuitWire all the components as shown in the Fritzing diagram below:
Notice that we have 4 AA batteries connected to the Power and Ground rails on the breadboard. The motors would draw too much power for Meadow to handle it, which is important to have an external power supply.
To power Meadow externally, you can use a its battery charging onboard circuit. Connect any standard 3.7V LiPo/LiIon battery, and you can charge it while the board is connected via the USB connector. You can check Powering the Meadow F7 to read more.
Step 2 - Create a Meadow Application projectCreate a new Meadow Application project in Visual Studio 2022 for Windows or macOS and name it MeadowLedRover.
Step 3 - Write the code for MeadowLedRoverIt is a good practice to create a Controller class for every peripheral involved to make the project scalable and maintainable. These Controller classes abstracts all the peripheral's logic so the main program logic will remain cleaner and easier to understand.
Add CarController class
Add a CarController class with the following code:
public class CarController
{
float SPEED = 0.75f;
HBridgeMotor motorLeft;
HBridgeMotor motorRight;
public CarController(HBridgeMotor motorLeft, HBridgeMotor motorRight)
{
this.motorLeft = motorLeft;
this.motorRight = motorRight;
}
public void Stop()
{
motorLeft.Power = 0f;
motorRight.Power = 0f;
}
public void TurnLeft()
{
motorLeft.Power = SPEED;
motorRight.Power = -SPEED;
}
public void TurnRight()
{
motorLeft.Power = -SPEED;
motorRight.Power = SPEED;
}
public void MoveForward()
{
motorLeft.Power = -SPEED;
motorRight.Power = -SPEED;
}
public void MoveBackward()
{
motorLeft.Power = SPEED;
motorRight.Power = SPEED;
}
}
This controller is a simple driver for the car, encapsulating the logic to control the motors. Rather than changing the speed of the individual motors every time we want the car to go to any direction, we can simply create four methods (MoveForward
, MoveBackwards
, TurnLeft
, TurnRight
) and one method to stop (Stop
).
MeadowApp Class
For the main MeadowApp class, copy the following code:
// public class MeadowApp : App<F7FeatherV1> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7FeatherV2>
{
Led up, down, left, right;
CarController carController;
public override Task Initialize()
{
var led = new RgbLed(
Device,
Device.Pins.OnboardLedRed,
Device.Pins.OnboardLedGreen,
Device.Pins.OnboardLedBlue);
led.SetColor(RgbLedColors.Red);
up = new Led(Device, Device.Pins.D13);
down = new Led(Device, Device.Pins.D10);
left = new Led(Device, Device.Pins.D11);
right = new Led(Device, Device.Pins.D12);
up.IsOn = down.IsOn = left.IsOn = right.IsOn = false;
var motorLeft = new HBridgeMotor
(
device: Device,
a1Pin: Device.Pins.D07,
a2Pin: Device.Pins.D08,
enablePin: Device.Pins.D09
);
var motorRight = new HBridgeMotor
(
device: Device,
a1Pin: Device.Pins.D02,
a2Pin: Device.Pins.D03,
enablePin: Device.Pins.D04
);
carController = new CarController(motorLeft, motorRight);
led.SetColor(RgbLedColors.Green);
return base.Initialize();
}
public override async Task Run()
{
while (true)
{
up.IsOn = true;
carController.MoveForward();
await Task.Delay(1000);
up.IsOn = false;
carController.Stop();
await Task.Delay(500);
down.IsOn = true;
carController.MoveBackward();
await Task.Delay(1000);
down.IsOn = false;
carController.Stop();
await Task.Delay(500);
left.IsOn = true;
carController.TurnLeft();
await Task.Delay(1000);
left.IsOn = false;
carController.Stop();
await Task.Delay(500);
right.IsOn = true;
carController.TurnRight();
await Task.Delay(1000);
right.IsOn = false;
carController.Stop();
await Task.Delay(500);
}
}
}
In MeadowApp's Constructor, notice two methods: Initialize
and Run
.
In the Initialize
method, you can see how all the four LEDs are initialized as Led (which we'll use to turn them on and off depending on the direction the car is going), two HBridgeMotor objects (one for each motor), and are passed to a new CarController
object.
In the Run
method, the App enters an infinite while
loop, and it will call the CarController's methods to move on each direction for one second and stop for half a second in between.
Click the Run button in Visual Studio. It should look like to the following GIF:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments