Netduino home hardware projects downloads community

Jump to content


Photo

SMC05A Driver


  • Please log in to reply
No replies to this topic

#1 PenZenMaster

PenZenMaster

    New Member

  • Members
  • Pip
  • 6 posts

Posted 02 January 2012 - 08:13 AM

Good New Year Everyone,

I got myself a Netduino and have been having a good bit of fun with it. Since robots are so 21st century I wanted to build one.
The kids over at Pololu had a Tamiya 70168 and a SMC05A Dual Serial Motor Controller on sale.
The controller is discontinued, but for what it's worth here is a driver I wrote for it.



/// 
/// <summary>
/// This class is intended to allow a Netduino to drive a 
/// Pololu Low-Voltage Dual Serial Motor Controller (DSMC) Pololu item #: 120  (SMC05A)
/// This class requires a reference to Microsoft.SPOT.Hardware.SerialPort as part of the project
/// </summary>
/// <remarks>
/// The SMC50A holds state.  When a motor is given a direction and speed the settings hold
/// until it recieves another set of parameters. 
/// </remarks>
/// 
/// 

using System;
using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
using SecretLabs.NETMF.Hardware.Netduino;

namespace MotorTest
{
    class DualSerialMotorController
    {
        /// <summary>
        /// The four-byte motor controller command
        /// byte 1: Start Byte - Always 0x80 (128) to signify the beginning of a command
        /// byte 2: Device Type - This should always be 0x00 for commands sent to the SMC05a
        /// byte 3: Motor and Direction - This byte has three parts
        ///     bit 0 - Specifies the direction of the motor 1 = forward, 0 = backwards
        ///     bits 1 - 6 - Specifies the motor number.  All motors respond to 0 and 1 if in dual-motor mode
        ///     bit 7 - Must always be 0
        ///     To calculate byte 3 (motor number x 2) + ( zero for forward or one for reverse)
        /// byte 4: Motor Speed - The most significant bit (MSB) must always be 0.  Range 0x00 - 0x7F
        ///     0x00 = STOP
        ///     0x7F = Full Speed
        /// </summary>
        SerialPort serialPort;

        static byte[] stopMotor0 = new byte[4] { 0x80, 0, 0x00, 0 };        // Stop Motor Zero
        static byte[] stopMotor1 = new byte[4] { 0x80, 0, 0x02, 0 };        // Stop Motor One

        static byte[] forwardMotor0 = new byte[4] { 0x80, 0, 0x00, 0 };     // Forward speed passed by caller
        static byte[] forwardMotor1 = new byte[4] { 0x80, 0, 0x02, 0 };     // Forward speed passed by caller

        static byte[] reverseMotor0 = new byte[4] { 0x80, 0, 0x01, 0 };     // Reverse speed passed by caller
        static byte[] reverseMotor1 = new byte[4] { 0x80, 0, 0x03, 0 };     // Reverse speed passed by caller

        public DualSerialMotorController(string portName = SerialPorts.COM1, int baudRate = 9600, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
        {
            serialPort = new SerialPort(portName, baudRate);
            serialPort.Open();
        }

        // Motor Zero forward
        public void Forward0(int speed)
        {
            forwardMotor0[3] = (byte)speed;
            serialPort.Write(forwardMotor0, 0, 4);
        }

        // Motor One forward
        public void Forward1(int speed)
        {
            forwardMotor1[3] = (byte)speed;
            serialPort.Write(forwardMotor1, 0, 4);
        }

        // Motor Zero and One forward
        public void ForwardAll(int speed)
        {
            forwardMotor0[3] = (byte)speed;
            forwardMotor1[3] = (byte)speed;
            serialPort.Write(forwardMotor0, 0, 4);
            serialPort.Write(forwardMotor1, 0, 4);
        }

        // Motor Zero backward
        public void Reverse0(int speed)
        {
            reverseMotor0[3] = (byte)speed;
            serialPort.Write(reverseMotor0, 0, 4);
        }

        // Motor One backward
        public void Reverse1(int speed)
        {
            reverseMotor1[3] = (byte)speed;
            serialPort.Write(reverseMotor1, 0, 4);
        }

        // Motor Zero and One backward
        public void ReverseAll(int speed)
        {
            reverseMotor0[3] = (byte)speed;
            reverseMotor1[3] = (byte)speed;
            serialPort.Write(reverseMotor0, 0, 4);
            serialPort.Write(reverseMotor1, 0, 4);
        }

        // Motor Zero forward and One backward
        public void SpinLeft(int speed)
        {
            forwardMotor0[3] = (byte)speed;
            reverseMotor1[3] = (byte)speed;
            serialPort.Write(forwardMotor0, 0, 4);
            serialPort.Write(reverseMotor1, 0, 4);
        }

        // Motor Zero backward and One forward
        public void SpinRight(int speed)
        {
            forwardMotor1[3] = (byte)speed;
            reverseMotor0[3] = (byte)speed;
            serialPort.Write(forwardMotor1, 0, 4);
            serialPort.Write(reverseMotor0, 0, 4);
        }

        // Stop both motors
        public void Stop()
        {
            serialPort.Write(stopMotor0, 0, 4);
            serialPort.Write(stopMotor1, 0, 4);
        }

    }
}


All my little bot does now is run a little test pattern when you turn him on.


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace MotorTest
{
    public class Program
    {
        public static void Main()
        {
            Thread.Sleep(500);
            DualSerialMotorController dsmc = new DualSerialMotorController();
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            
            led.Write(true);       		// turn on the LED

            dsmc.ForwardAll(127);          // Tell the controller to spin motor Zero forward and motor One forward
            Thread.Sleep(1000);
            dsmc.Stop();           		// All stop

            dsmc.ReverseAll(127);          // Tell the controller to spin motor Zero backward and motor One backward
            Thread.Sleep(1000);
            dsmc.Stop();           		// All stop

            dsmc.SpinLeft(127);     		// Tell the controller to spin the robot left
            Thread.Sleep(1000);
            dsmc.Stop();                    // All stop

            dsmc.SpinRight(127);            // Tell the controller to spin the robot left
            Thread.Sleep(1000);
            dsmc.Stop();                    // All stop

            led.Write(false);       		// turn off the LED
        }

    }
}



If I'm going to let him out of my office I'd better figure out a way to make him "see".

Cheers
PenZenMaster
If it isn't autonomous, it is just an ROV.

PenZenMaster




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2010-2014 Secret Labs LLC  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.