Scott Hanselman

Building a working Robot controlled by a C#, an iPhone, and Monkey.Robotics

October 28, 2014 Comment on this post [24] Posted in Learning .NET | Open Source
Sponsored By

Robot_TinyThis year at Xamarin's Evolve there was a Robot Building area that introduced a new beta platform called Monkey.Robotics (GitHub). There is a disclaimer on the GitHub: "Monkey.Robotics is still a beta/work in progress and is not officially supported by Xamarin" but I can say that I was able to successfully build this robot with my 6 year old and we've had a blast driving it around the house! He was thrilled to solder (but not too much) and setup all the wires on the breadboard.

This robot is a great showcase for the larger Monkey.Robotics stack PLUS a great way to learn how to use Xamarin tools AND the .NET Micro Framework. And the end you'll have not just a robot with code running on a Netduino, but also have deployed C# code using Xamarin onto (in my case) an iPhone.

The resulting stuff you get to build and use are:

  • iPhone/Android app using Xamarin.Forms and controls or a gyroscope talking over your phone's Bluetooth radio to some code running on an...
  • Netduino running the open source .NET Micro Framework. This Netduino will receive communications via a Bluetooth radio on a chip. This BT LE board is connected to the GPIO (general purpose input output) pins on the Netduino. The board then controls...
  • Motors and wheels on a nice base, powered by 4 AA batteries.

The authors are some of my favorite people, including Bryan Costanich, Frank Krueger, Craig Dunn, David Karlas, and Oleg Rakhmatulin.

There's a deceptively large amount of code here and it's whole job is to hide the yucky parts of a project that connects mobile devices to wearables, sensors, peripherals, and low-level communication. They include a cross-platform API for talking with BLE (Bluetooth Low-Energy) devices, as well as the beginnings of a similar API over Wifi. Even more important is a higher level messaging framework sitting on top of these lower=level APIs. The net result is that talking Bluetooth between your phone and a device isn't very hard.

On the device side (in my case with .NET Micro Framework) they make things more compose-able with "blocks and scopes" style abstractions, allowing one to fairly easily connect output pins (LEDs, motors) to input pins (buttons, sensors).

Here is one of their basic examples. This makes an LED blink when a button is pressed.

public class Program
{
static H.Cpu.Pin buttonHardware = Pins.ONBOARD_BTN;
static H.Cpu.Pin ledHardware = Pins.ONBOARD_LED;

public static void Main()
{
var button = new DigitalInputPin (buttonHardware);
var led = new DigitalOutputPin (ledHardware);

button.Output.ConnectTo (led.Input);

// keep the program alive
while (true) {
System.Threading.Thread.Sleep (1000);
}
}
}

I went through the very excellent Project Walkthrough on building a Robot. Note that all this is on GitHub so if you have any issues, you can fix them and submit a pull request. I'm sure the project would appreciate it!

Architectural_Overview

Here's what you need to buy/or have (I took from their GitHub site) if you want to build this same robot.

  • bot Chassis w/Motors - Just about any 2 wheeled robot chassis with motors and gears will work, but we like these. They're only $11 and they work well. Spark Fun also makes a similar robot chassis for $15, but the battery holder is difficult to access, otherwise, it's also very nice.
  • Netduino 2 - From Secret Labs, the Netduino is a programmable micro controller that runs the .NET Micro Framework.
  • BLE Mini - From Redbear Labs. Used to communicate with the robot from Xamarin.iOS and Xamarin.Android.
  • 400 Point Breadboard - The breadboard is where our electronics will all get connected. It has holes in it to put chips, sensors, wires, etc., into.
  • Breadboard Wiring Kit - Any wiring kit will work, but we especially like these. You may also want to get some male-to-male flexible jumper wires.
  • Dual H-Bridge Chip - Used to power the motors, H-bridges are simple chips that allow you to control motors with external power, so you don't burn out your Netduino trying to drive them with that. :) You can also build an H-bridge with some simple components, but buying a chip is easier and will likely cost about the same.
  • iOS or Android Device w/BLE Support - If using an iOS device, you'll need an iPhone 4 or later. If using an Android Device, you'll want to make sure that it has BLE support and is running at least Android v4.3.

Make sure you have you development and build environment setup, and then follow the guides below to get this thing up and running!

Basically you'll need Xamarin Studio or Visual Studio with Xamarin Tools. You'll also need the .NET Micro Framework bits, which are free for Visual Studio 2013, as well as the Netduino 4.3.1 SDK. There's a thread over at the Netduino Forums if you have trouble getting your device ready. I had no issues at all.

The Robot code is in a larger solution called Robotroller that's pretty nicely factored. You've got the Core functionality for the phone and the Core for the Robot.

The Phone code makes great use of databinding. The Car code is a little messy (it's beta and looks like Tests that turned into a working Robot! ) but it worked out of the box! It's surprisingly simple, in fact, due to the next abstraction layer provided by Monkey.Robotics.

How cool is this. That's basically it. Love it.

public class TestRCCar
{
public static void Run ()
{
// initialize the serial port for COM1 (using D0 & D1)
// initialize the serial port for COM3 (using D7 & D8)
var serialPort = new SerialPort (SerialPorts.COM3, 57600, Parity.None, 8, StopBits.One);
serialPort.Open ();
var server = new ControlServer (serialPort);

// Just some diagnostic stuff
var uptimeVar = server.RegisterVariable ("Uptime (s)", 0);

var lv = false;
var led = new Microsoft.SPOT.Hardware.OutputPort (Pins.ONBOARD_LED, lv);

// Make the robot
var leftMotor = HBridgeMotor.CreateForNetduino (PWMChannels.PWM_PIN_D3, Pins.GPIO_PIN_D1, Pins.GPIO_PIN_D2);
var rightMotor = HBridgeMotor.CreateForNetduino (PWMChannels.PWM_PIN_D6, Pins.GPIO_PIN_D4, Pins.GPIO_PIN_D5);

var robot = new TwoWheeledRobot (leftMotor, rightMotor);

// Expose some variables to control it
robot.SpeedInput.ConnectTo (server, writeable: true, name: "Speed");
robot.DirectionInput.ConnectTo (server, writeable: true, name: "Turn");

leftMotor.SpeedInput.ConnectTo (server);
rightMotor.SpeedInput.ConnectTo (server);

// Show diagnostics
for (var i = 0; true; i++) {
uptimeVar.Value = i;

led.Write (lv);
lv = !lv;
Thread.Sleep (1000);
}
}
}

Here's a video (don't play it too loud, my kids are yelling in the background) of me controlling the robot using my iPhone. Note I'm using the gyroscope control so I twist my hand to steer.

Robot via iPhone over BT LE. It's @Xamarin on phone plus @SecretLabs and @Xamarin on robot!

A video posted by Scott Hanselman (@shanselman) on

All in all, I'm enjoying Monkey.Robotics and I hope it takes off with more projects, more ideas, more things to build. If you've got interest or expertise in this area, go star their project and get involved! There's a LOT of stuff going on over there, so explore all the docs and diagrams.


Sponsor: Big thanks to my friends at Octopus Deploy. They are the deployment secret that everyone is talking about. Using NuGet and powerful conventions, Octopus Deploy makes it easy to automate releases of ASP.NET applications and Windows Services. Say goodbye to remote desktop and start automating today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
October 28, 2014 11:49
Scott, this is awesome! Great to see how .NET can be used in so many different scenarios. Next up: .NET flying machines! :-D
October 28, 2014 13:10
Cool!! I have recently bought two big RC toys for my sons, This would make it even more awesome while the kids also learn some fundamentals.!!

Thanks Scott!!
October 28, 2014 15:16
This is interesting, C# as the one language for the firmware and the control software. A lot more friendly to start with for beginners than AVR-GCC or even Arduino.

I now want my own money robot, I'll have to get to it!
October 28, 2014 16:58
This is really cool, but useless to me at this point since I do not own a Xamarin subscription.
October 28, 2014 22:39
Great post however same situation ^^. Xamarin sub at $1000 a year (VS integration) is still a bit out of reach if you're not making apps for $$.

I wish MSFT/Xamarin could strike a deal to include it in Ultimate.
October 28, 2014 23:05
Andrew - The $25 a month version works https://store.xamarin.com/ or you can use the trial. No need for $1000.
October 29, 2014 0:18
Micro framework is not that supported my microsoft, I think they see no future for it , the latest one still only work with 2010 , that really discourage me from playing with cards based on this framework .
Sam
October 29, 2014 0:53
Sam - No, the .NET Micro Framework for 2012, 2013, AND Dev14 is out https://netmf.codeplex.com/releases/view/133285
October 29, 2014 2:13
Love playing with my netduino. The CLR is an interpreter rather than a just-in-time compiler. Its runtime speed compared to a simple arduino just a laugh. Microsoft missed the boat here?
Still love .Net on my netduino though.
Ed
October 29, 2014 2:27
Really cool proof of concept. Xamarin is all over the place, I will have to start playing around with it.
October 29, 2014 8:29
Great blog post. I've been into Arduino/IoT/Bluetooth for awhile and just started playing around with Netduino. Good stuff. Don't think Xamarin will fly in this arena though with it's hefty price. Hard to justify paying, as a freelancer, for something that costs more than my MSDN subscription.
October 29, 2014 12:19
Wow. Just wow. Oh, and friggin amazing!
October 29, 2014 12:37
This is very nice to play with, i'm starting to play with android/Xamarin, the problem is that it's very heavy on the PC :(
October 29, 2014 15:41
Scott - Not worth using Xamarin Studio.

I live in VS all day and love it...no integration at the $25 mark is a deal breaker for me.
October 29, 2014 16:47
Love it Scott!! What a fun project to work on. Working with Xamarin currently and have found the support team to be great and enjoying the development experience all in all. Was a little frustrating at first but am dealing.
October 29, 2014 17:23
I agree with Bruce and Andrew. The hefty price of Xamarin is still the deterrent.
October 29, 2014 18:35
Hey Scott, this post is going into my www.XamarinWeekly.com newsletter issue #12. It comes out this Friday the 31st. Thanks.
October 29, 2014 20:49
Andrew- If you're making simple applications (which is pretty much all these boards are really capable of) then Xamarin Studio is perfectly acceptable. It might even make you a better, more flexible programmer if you learn to use other tools besides just VS. That'll translate to better programming when you do use VS. VS is great, but don't become someone who's only capable of using one tool.
October 31, 2014 5:21
I echo some of the comments here regarding Xamarin. I switched to a Mac recently so lack of VS isn't a dealbreaker but I've tried the free version and the restrictions enforced render it virtually unusable. I've seen this app done with Python and Ruby already but still respect for getting it done with C# :-)
October 31, 2014 8:42
Scott, it is not necessary to build an entire app in Xamarin for the phone. The gyroscope interface in the browsers work perfectly fine in Javascript these days. I did something similar with my kids recently and here is the video: https://www.youtube.com/watch?v=4w5AiApTPPI

For reference, the robot board is from GHI Electronics. The code in Safari is being served by the robot and client-side Javascript runs the whole thing.

November 03, 2014 23:54
Potential massively dumb question. Does this work on Windows Phone? I'm guessing you would not need Xamarin, but would it just work?

My boy has a Windows phone, he likes this kind of stuff, but I'd like a clue before I surprise him with stuff he cannot use!
November 05, 2014 12:42
You are Awesome , Love from pakistan <3
November 17, 2014 3:51
Thanks Scott,
I have been meaning to do a robotics project with my 13 year old and this seems like an easy and fun path to get going.

I just ordered the parts. I think we will have a lot of fun with this project.

Thanks And appreciate your post.

regards,
Atul
November 21, 2014 0:24
Reminds me of my final year engineering project using Arduino Here

Was written in Python and C++ (Sigh Netduino had not been invented yet)

Oh the bliss of Embedded Systems programming and robotics - you almost instantly feel connected to it as if it was your own kid. You can feel it being ALIVE! The feeling's always of *WOW*

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.