Saturday, December 31, 2011

Netduino "Robot"

Over the past month or so my son and I built a "Robot" with our Netduino.



Parts:

Physical Assembly:

First, we mounted the 8 pin headers upside down in the motor driver breakout like this:


We did this so that we could easily mount this into the breadboard and see the pin descriptions.  We didn't insert it all of the way into the breadboard - rumor has it that these get hot - although ours does not seem to.

Next, we assembled the robot chassis.  Some of the parts have tight tolerances, but everything fit together fine.

We attached the battery holders to the lower platform and the Netduino and breadboard onto the top platform.  Then we mounted the IR sensor to the "front" (the end with the 2 wheels).

Do NOT mount the IR sensor like it is in the picture above.  Our first day we managed to drive it under a kitchen cabinet and it cracked the circuit board of the IR sensor.  :(  When our replacement arrives, I think we will mount it on the lower deck so it is safer.

Wiring:

We wired it like this:

IR sensor yellow wire to the Netduino AIN0
IR sensor black to ground on Netduino
IR sensor red to Netduino 5V

9 volt power to Netduino
6 volt power black to ground on Netduino (via the RadioShack connector and some hookup wire)
6 volt power red to motor driver VM (same as above)

Netduino to TB6612FNG breakout
DIO0 = Standby (This needs to be high (true))
DIO1 = AIN1
DIO2 = AIN2
DIO5 = PWMA
DIO6 = PWMB
DIO8 = BIN1
DIO9 = BIN2
Ground to ground

TB6612FNG breakout to motors
AO1 = Red motor lead (Right)
AO2 = Black motor lead (Right)
BO2 = Black motor lead (Left)
BO1 = Red motor lead (Left)

Code:

Now it was time to code a little bit.  As usual, the path was paved by the great authors in the communities at Netduino.com (http://forums.netduino.com/index.php?/forum/4-netduino/).

We used the library provided from this post: http://forums.netduino.com/index.php?/topic/493-netduino-tankbot/page__view__findpost__p__3665 from user ajcg1973.


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
// make sure you include the library - ours was (messily) inline

namespace IRRobot
{
    public class Program
    {
        static AnalogInput analogInput = new AnalogInput(Pins.GPIO_PIN_A0);
        public static void Main()
        {

            OutputPort stby = new OutputPort(Pins.GPIO_PIN_D0, true);
            AjbMotorTB6612FNG motors = new AjbMotorTB6612FNG(Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D1, Pins.GPIO_PIN_D2, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D8, Pins.GPIO_PIN_D9);
            motors.Motor1Direction = AjbMotorTB6612FNG.MotorDirection.Forward;
            motors.Motor1Speed = 50;  // Set the motor to 50% speed
            motors.Motor2Direction = AjbMotorTB6612FNG.MotorDirection.Forward;
            motors.Motor2Speed = 50;  // Set the motor to 50% speed
            while (true)
            {
                Thread.Sleep(250); // pause to gather analog sample
                int analogValue = 1024 - analogInput.Read();
                Debug.Print(analogValue.ToString());
                if (analogValue < 550)
                {
                    motors.Motor1Direction = AjbMotorTB6612FNG.MotorDirection.Reverse;
                    motors.Motor1Speed = 30;  // Set the motor to 30% speed
                    motors.Motor2Direction = AjbMotorTB6612FNG.MotorDirection.Forward;
                    motors.Motor2Speed = 30;  // Set the motor to 30% speed
                    Thread.Sleep(1000);
                    motors.Motor1Direction = AjbMotorTB6612FNG.MotorDirection.Forward;
                    motors.Motor1Speed = 50;  // Set the motor to 50% speed
                    motors.Motor2Direction = AjbMotorTB6612FNG.MotorDirection.Forward;
                    motors.Motor2Speed = 50;  // Set the motor to 50% speed
                }

            }

        }

    }
}

This still needs some tweaking.  Or a whole re-write.  :)  But it shows how to drive the motors using ajcg1973's library and how to read the IR sensor.  We flipped forward and reverse in the library - this might mean we are driving backwards.  :)

You might want to watch the IR output values for a bit to get a feel for what it can see - ours is a bit far sighted so it runs into things some times. :) 

Conclusion:

This was a fun project.  The kids loved the little robot as it scooted across the floor avoiding obstacles.  We'll probably enhance our software and try some different sensors over the next few weeks.

We hope that you enjoyed this post.

References:

Original library code: http://forums.netduino.com/index.php?/topic/493-netduino-tankbot/page__view__findpost__p__3665
We got wiring help from this thread at the Netduino forum: http://forums.netduino.com/index.php?/topic/2396-control-dc-motor-with-dual-motor-driver/