Building a Netduino Rover – Part 3

See other posts in this series

Software Basics
If this is your first Netduino project you’ll probably need to install a few things. Everything you need to get started is free, and can be found here on the Netduino website. I’m going to assume you have the environment setup and that you have a basic knowledge of programming.

The first task is to abstract away some of the complexity caused when dealing with hardware. The class below controls a single motor via the SetSpeed method, which takes a value on a scale of -1.0 to 1.0. Passing a negative value automatically sets the motor into reverse.

using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;

namespace NetduinoRover.Outputs
{
    public class Motor
    {
        private PWM _motor;
        private OutputPort _direction;
        private int _correction;

        private const uint PERIOD = 1000 * 50;
        private const int TOP_SPEED = 10000;

        public Motor(Cpu.Pin pwm, Cpu.Pin direction, int correction)
        {
            _motor = new PWM(pwm);
            _direction = new OutputPort(direction, true);
            _correction = correction;
        }

        // Sets the motor speed and direction on a scale of -1 to 1
        public void SetSpeed(double percent)
        {
            // Set direction
            if (percent < 0)
                _direction.Write(false);
            else
                _direction.Write(true);

            // If the percent is negative, make it positive
            if (percent < 0)
                percent = percent * -1;

            // Set pulse width modulation (PWM)
            double duration = percent * TOP_SPEED;
            duration += _correction;
            _motor.SetPulse(PERIOD, (uint)duration);
        }
    }
}

The second task is to create a class to handle the distance sensors. If you read the raw value from the sensors you’ll find that the value increases as the distance decreases. The class below performs a (very crude) conversion of the raw value into a distance in centimetres.

using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;

namespace NetduinoRover.Sensors
{
    public class RangeSensor
    {
        private AnalogInput _analogInput;

        public RangeSensor(Cpu.Pin pin)
        {
            _analogInput = new AnalogInput(pin);
        }

        // Returns approximate distance in cm
        public int Read()
        {
            int sensorReading = _analogInput.Read();

            // Very crude conversion to cm
            double distanceInCm = (1000 - sensorReading) * 0.1;
            
            // Ensure answer is sensible
            if (distanceInCm > 0 && distanceInCm < 100)
                return (int)distanceInCm;
            else
                return 100; // default
        }
    }
}

By using the classes above you should be able to build a simple program to control the rover and detect obstacles. In parts 4 and 5 I’ll show two different ways of implementing this, as well as how to build a behaviour based architecture.

Advertisements
This entry was posted in Projects and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s