Windows Phone controlled car with Netduino and Bluetooth

TwitterFacebookRedditGoogle+EmailPinterest

The houses in Durban, South Africa (where I live) usually have some form of gap underneath them that ranges from 30cm to big enough to crawl inside. As far as I know it is for ventilation to help with the heat. The problem is that a bunch of squeaky rodents have decided that mine is a good home, and unfortunately the gap is pretty small so I can’t fit. So I decided to make a little car that could go scout out the area and show me what was going on under there, and possibly how to stop it.

So I built a little Bluetooth controlled car using a Netduino and a Windows Phone as a remote.

My puppy seems to be a fan.

The idea is pretty simple: Make a car that I can control remotely that is small enough to fit under the house. And have a place to mount a second Windows Phone running Skype video so I can view live-video.

car1

Design

For the frame and wheels I designed models and 3D printed them.

car3

The wheels needed to get quite a lot of grip because the surface is very sandy, so I made them spiked so they could dig in nicely. The lip is sized to fit over the motor shafts. The back wheels were pretty similar except just had a dowel-sized hole to use as an axle.
image

 

The body is a bit more complicated. It needs to hold both motors (front-wheel driven), the Netduino, the Motor driver, and battery (9V).

To save space I printed a platform for the motor driver that allowed me to mount it on-top of the Netduino using the same holes. Look at 3 the vertical lines going through the holes – those are bolts that go through the base, Netduino, and motor driver platform. The actual motor driver then sits on top of the platform using the other 3 holes.

image

The 9v battery sits right at the back with an elastic band hooked around the triangle. Directly underneath that is where the wooden dowel is placed for the back wheels.

The motors sit between the pillars on either side with bolts coming in from both sides (could have them go right through but I didn’t have any long enough).

Hardware

- Netduino (any model).

- 5v LED board. This isn’t required and was just for shining forwards. (eBay for $3)

- 2 x geared motors. (DX for $3 each)

- Dual DC motor driver. (eBay for $ 5)

- Serial Bluetooth module. (DX for $7)
- 9V battery
- A frame (I 3D printed mine, but a block of wood would work too).

OK, now to wire it all up!
The Bluetooth module is powered from the Netduino 3V3 and GND pins, and wired to pins 0 and 1 for data.
A very handy feature of the motor driver is that you can feed it with 9V and it will output 5V (from the 5V pin). So the battery goes into the VCC and GND, and then the Netduino is powered from the GND and 5V on the motor driver board. The LED board can also be driven directly from that 5V.
Each motor is connected to the outputs on each side.
Lastly, all the actual driver pins need to be wired to the Netduino. The purple and blue wires are connected to PWM pins on the Netduino and these are used to tell the motor driver how fast to make the left/right motor. The four remaining white wires are connected to digital Netduino pins and are used to tell the motor driver which direction to move the motor in.

board

From left to right the direction pins on the motor driver are labeled INA, INB, INC, IND – meaning that each is an input. The left motor is controlled by INA and INB, while the right motor is INC and IND.

To make the left motor (same for the right except with INC and IND) go forwards you set INA HIGH, and INB LOW. To make it go backwards you set INA LOW, and INB HIGH. To stop the motor set both LOW.
Obviously the speed that it spins forward or backwards is defined by the purple/blue wires mentioned earlier.

Directly to the right of the lower power points there is a white button – this is the on/off button for the board, so doubles up as a way to turn the entire car on and off without having to unplug the battery.

 

Netduino code

If you haven’t followed my Netduino+WP8 Bluetooth tutorial then go here now, because the Bluetooth part won’t be explained fully here.

Before adding any Bluetooth code, add a new class to the project called MotorController.cs.
This class will basically control one motor per instance, and is just a helper to set the speed and direction.

I found that these motors didn’t move at all (or VERY slowly) until I fed the PWM signal pin with at least 70%. To counter that I scale the incoming speed percent to between 70 and 100%. So if you tell the motor to go 1% then it will actually go just above 70%.

The Direction enum is just to have a nice way to define direction.

If you are on the latest version of .NETMF then you will need to add the following line to the namespaces at the top of the file:

Plus you will need to add the SecretLabs.NETMF.Hardware.PWM assembly to your project.

Now navigate back to Program.cs where we need to add the Bluetooth stuff, as well as creating an instance of MotorController for each motor.

So change the current code there to look like this:

The pins used to control the motor driver are based on the wiring diagram higher up – you can change them and it will still work as long as you ensure the Purple/Blue wires go to PWM pins, and the white wires go to digital pins.

Below that method we add the event handler for when the serial Bluetooth module receives data:

And lastly, when that method detects that an end-line character has been reached (“|” (a pipe) in this case), it will send the complete string to this next method:

As you can see above, the Windows Phone will send through commands in this format: direction,left speed, right speed. Along with forwards and backwards, you might notice “spin”. This spins the wheels in opposite directions to make the car turn on the spot, but doesn’t work particularly well due to the friction mentioned at the end of this post.

Build that code and deploy to the Netduino, then we can move on to the Windows Phone part!

Windows Phone code

Create a standard Windows Phone 8 application. Before you forget, enable the ID_CAP_PROXIMITY capability within WMAppManifest.xml.

Next we need to create a control stick that will be used to move around and drive the car. Create a new UserControl called ThumbStick.xaml within a new folder called Controls. The XAML below just defines a square with an X in the middle. There are events to detect when the user puts their finger down, moves it, and then lifts their finger. You might notice that instead of referring to a finger, it mentions a mouse – this is just because it is based on desktop Silverlight.

The RenderTransform is used to make the X move around with the users finger.

Now go into ThumbStick.xaml.cs to add the code.
Firstly, add the event handlers for the touch events:

Also notice that the first line defines an event that this control will fire each time a finger is moved.

Now we just need the method that will work out where the users finger is, move the X in the UI, and then work out how fast each motor should be.

The most complicated part of that code is working out the speeds of each motor based on how much it is turning. The way I have done it is that when turning left, the left motor will always be 100%, but the more left they turn, the slower the right motor will turn. So moving the thumbstick 20% left will set the left motor at 100% and the right motor at 80%. Moving the thumbstick 70% left will set the left motor at 100% and the right motor at 30%.

Note: All the % speeds above are then scaled based on the speed value.

Now go to MainPage.xaml and add the following line into the XML namespaces at the top:

Change the names accordingly if you project or controls folder is different.

Now lets define the UI:

Screen

All that is left to add is the code. Open up MainPage.xaml.cs.
Add the following boiler-plate Bluetooth code to handle the receiving and sending of data:

The last thing to do is to add the event handler for when the ThumbStick moves, and write the value to the Bluetooth module. The code above automatically adds the pipe (“|”) character to define the end of the message.

And that is everything!

Make sure the Netduino code is deployed to the Netduino, then wire everything up and turn it on. Once it is on, pair your Windows Phone with the BT module and then open up the app. Give it a few seconds to connect and then start driving around :) .

Considerations

I wanted to keep the mechanics very simple so decided to not have a turning mechanism, but instead slow down a specific motor to make it turn in that direction. Although this works, there is a lot of friction because the back wheels are forced to slide out when turning. In future I would probably add a way for the wheels themselves to turn, or go with a tank-tread style.

I designed the platform around using a 9V battery – which turns out is a pretty bad idea. Compared to most other types of batteries they have a very low mAh and die really fast when driving two motors and an array of LED’s.

 

You can grab the source code and .STL 3D printing files HERE!

car2

If you enjoy Bluetooth and Windows Phones, checkout my other posts on the topic.

Got questions? Ask me in the comments below or on the Twitter machine: @roguecode

TwitterFacebookRedditGoogle+EmailPinterest