Saturday, August 29, 2015

Servo Controller: Test Stand

I've done a lot of work on my servo controller project, but I don't have much to say. Luckily, I have progress pictures. :-)

I designed and 3-D printed a test stand for 9 servos.

All servos low...



All servos neutral...



All servos high...



Currently in the works is binary communication with the Arduino to efficiently give servo commands. Once that is complete, I'll begin thorough testing to decide which servo is best for my application.

Sunday, August 16, 2015

Servo Controller: Servo Control via .NET Application

Using the Serial Monitor in the Arduino IDE was convenient for quick testing, but ultimately I want a custom application to control all my servos. So my next step was to recreate the Serial Monitor as my own application (that I could eventually expand upon).

I created a very simple VB.NET application to communicate over a serial port. A couple notes on it:

- The program automatically connects to the first COM port it finds. That's generally pretty safe as nowadays the average computer user doesn't have any devices that communicate over a serial port. (Ahh, I remember back in the day, before USB, when you plugged the mouse into a COM port. Or Doom over serial, good times.) This automatic connection is dangerous if you do have devices such as 3-D printers that communicate over a COM port.
- The baud rate and similar are hard coded to match my Arduino code, but you may want to expose that as a configurable item.


The application in action:




The application in Visual Studio's designer:



The first dropdown list is for the COM port (automatically selected to the first found at startup). It is named ddlCOMPort.

The buttons from left to right are: btnConnect, btnDisconnect, btnRefreshPorts, btnClear, and btnSend.

The input text box is txtSend, and the output read-only text box is txtReceive.

The code:


Imports System.IO.Ports

Public Class FormMain

    ''' <summary>
    ''' Definite a delegate to transfer incoming data from the background
    ''' (serial) thread to the foreground (UI) thread.
    ''' </summary>
    ''' <param name="incomingBuffer"></param>
    ''' <remarks></remarks>
    Private Delegate Sub ProcessIncomingTextDelegate(ByVal incomingBuffer As String)

    ''' <summary>
    ''' The serial port object that will communicate.
    ''' </summary>
    ''' <remarks></remarks>
    Private WithEvents connectedSerialPort As New SerialPort()

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            refreshPortsList()

            If ddlCOMPort.SelectedIndex >= 0 Then
                ' Port is found, attempt to connect to it.
                connectSerialPort()
            Else
                ' Update which controls are enabled (so disconnect button is disabled at startup).
                updateConnectedEnableFields()
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
        Try
            connectSerialPort()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
        Try
            disconnectSerialPort()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
        Try
            connectedSerialPort.Write(txtSend.Text + vbCrLf)
            txtSend.Text = ""
        Catch ex As Exception
        End Try
    End Sub

    Private Sub connectedSerialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles connectedSerialPort.DataReceived
        Try
            ' Received data from the serial port, send it for processing.
            processIncomingData(connectedSerialPort.ReadExisting())
        Catch ex As Exception
        End Try
    End Sub

    Private Sub connectedSerialPort_ErrorReceived(sender As Object, e As SerialErrorReceivedEventArgs) Handles connectedSerialPort.ErrorReceived
        Try
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnRefreshPorts_Click(sender As Object, e As EventArgs) Handles btnRefreshPorts.Click
        Try
            refreshPortsList()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        Try
            txtReceive.Text = ""
        Catch ex As Exception
        End Try
    End Sub

    ''' <summary>
    ''' Returns if the serial port is currently connected.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private ReadOnly Property isSerialPortConnected
        Get
            Return connectedSerialPort.IsOpen()
        End Get
    End Property

    ''' <summary>
    ''' Update whether or not each UI control should be enabled based on
    ''' the connection state.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub updateConnectedEnableFields()
        Try
            Dim isConnected As Boolean = isSerialPortConnected
            btnConnect.Enabled = Not isConnected
            btnDisconnect.Enabled = isConnected
            ddlCOMPort.Enabled = Not isConnected
            btnRefreshPorts.Enabled = Not isConnected
            btnSend.Enabled = isConnected
        Catch ex As Exception
        End Try
    End Sub

    ''' <summary>
    ''' Connect to the selected serial port.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub connectSerialPort()
        Try
            If ddlCOMPort.SelectedIndex >= 0 Then
                connectedSerialPort.PortName = ddlCOMPort.SelectedItem
                connectedSerialPort.BaudRate = 9600

                connectedSerialPort.WriteTimeout = -1
                connectedSerialPort.ReadTimeout = -1

                connectedSerialPort.ReadBufferSize = 4096
                connectedSerialPort.WriteBufferSize = 2048

                connectedSerialPort.Parity = IO.Ports.Parity.None
                connectedSerialPort.StopBits = IO.Ports.StopBits.One
                connectedSerialPort.DataBits = 8
                connectedSerialPort.Open()
            End If
            updateConnectedEnableFields()
        Catch ex As Exception
        End Try
    End Sub

    ''' <summary>
    ''' Disconnect the serial port.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub disconnectSerialPort()
        Try
            connectedSerialPort.Close()
            updateConnectedEnableFields()
        Catch ex As Exception
        End Try
    End Sub

    ''' <summary>
    ''' Detect available serial ports and update the COM port list.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub refreshPortsList()
        Try
            ' Keep track of the selected port so we can reselect it after fresh.
            Dim currentlySelectedPort As String = ddlCOMPort.SelectedItem

            ' Get the available ports and update the dropdown list.
            Dim availablePorts As Array = IO.Ports.SerialPort.GetPortNames()
            ddlCOMPort.Items.Clear()
            ddlCOMPort.Items.AddRange(availablePorts)

            ' Reselect the previously selected port as a default.
            If currentlySelectedPort IsNot Nothing Then
                If ddlCOMPort.Items.Contains(currentlySelectedPort) Then
                    ddlCOMPort.SelectedItem = currentlySelectedPort
                End If
            End If

            If ddlCOMPort.SelectedIndex = -1 AndAlso ddlCOMPort.Items.Count > 0 Then
                ' No port selected but one is available, default to it.
                ddlCOMPort.SelectedIndex = 0
            End If
        Catch ex As Exception
        End Try
    End Sub

    ''' <summary>
    ''' Function to process incoming data from the serial port.
    ''' </summary>
    ''' <param name="incomingBuffer"></param>
    ''' <remarks></remarks>
    Private Sub processIncomingData(ByVal incomingBuffer As String)
        Try
            If txtReceive.InvokeRequired Then
                ' On background (serial port) thread. Invoke a delegate to call this
                ' function again on the foreground (UI) thread.
                Dim incomingTextDelegate As New ProcessIncomingTextDelegate(AddressOf processIncomingData)
                Me.Invoke(incomingTextDelegate, New Object() {incomingBuffer})
            Else
                ' On foreground (UI) thread. Process the message.
                txtReceive.Text += incomingBuffer
            End If
        Catch ex As Exception
        End Try
    End Sub
End Class

The code within this post is released into the public domain. You're free to use it however you wish, but it is provided "as-is" without warranty of any kind. In no event shall the author be liable for any claims or damages in connection with this software.

Saturday, August 8, 2015

Servo Controller: Servo Control via Serial Monitor

Ultimately I want a very efficient method for the computer to control the servos, but in the short term I just need a simple way to send servo commands from the computer. The quickest way to start is to use the Serial Monitor window in the Arduino IDE!

First I define a new global variable so I can keep track of which servo I'm controlling.

int32 debugServoNumber = 0;

Within the main program loop I continuously check if characters were received over the serial line. If an input was found (.available() > 0), then I call readSerialInput() to check for a command.

void loop()
{
  if (Serial.available() > 0)
  {
    readSerialInput();
  }
}

The readSerialInput() function parses the string that was submitted over the serial line and controls the servos based on those commands.

void readSerialInput()
{
  String serialInputString = Serial.readStringUntil('\n');

  // Echo back the command.
  Serial.println(serialInputString);
  
  if (serialInputString.startsWith("s=")) // Servo
  {
    String servoNumberString = serialInputString.substring(2);
    debugServoNumber = servoNumberString.toInt();
    Serial.print("Debug servo:");
    Serial.println(debugServoNumber);
  }
  else if (serialInputString.startsWith("p=")) // Percent Value
  {
    String valuePercentString = serialInputString.substring(2);
    servos[debugServoNumber].setToValue_percent(valuePercentString.toFloat());
  }
  else if (serialInputString.startsWith("v=")) // Pulse Value
  {
    String pulseValueString = serialInputString.substring(2);
    servos[debugServoNumber].setToValue_pulseLength(pulseValueString.toInt());
  }
  else if (serialInputString.startsWith("u=")) // Upper Pulse Value
  {
    String upperPulseValueString = serialInputString.substring(2);
    servos[debugServoNumber].setUpperPulseLength(upperPulseValueString.toInt());
    servos[debugServoNumber].setToMaximumValue();
  }
  else if (serialInputString.startsWith("l=")) // Lower Pulse Value
  {
    String lowerPulseValueString = serialInputString.substring(2);
    servos[debugServoNumber].setLowerPulseLength(lowerPulseValueString.toInt());
    servos[debugServoNumber].setToMinimumValue();
  }
  return;
}

How to Use


Using the Serial Monitor I can give commands to my microcontroller.



First I select which servo I want to control:
s=0


Then I instruct it a command such as percentage value:
p=0



I can also redefine the lower limit:
l=200
Or upper limit:
u=600



At any time I can switch servos:
s=2
And send new instructions to it, such as a specific pulse (rather than a percentage):
v=200


This approach is very straightforward to use for a person entering commands on the fly. However, I'll more than likely send binary values when I create the full-fledged servo software. It will no longer be human readable, but it will be faster.


Copyright (c) 2015 Clinton Kam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Servo Controller: Arduino Servo Class

Soldering up the servo shield and running Adafruit's examples applications were very straightforward. I recommend following Adafruit's fantastic tutorials to work through that. https://learn.adafruit.com/adafruit-16-channel-pwm-slash-servo-shield/overview However, to manage many servos at once I needed an object model to cleanly handle them all. I want to be able to support not only many servos but many servos on multiple servo drivers.

I created a new ServoModel class that contains the index of the servo, a pointer to the servo driver, and the minimum and maximum pulse values. A constructor defines all of those basic values, but there are additional functions to redefine min/max values (configuring for example) and setting the position based on a specific pulse or just a percentage.

Includes and Constants


#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

typedef long int32;

const uint16_t pulseLengthMinimum = 0;
const uint16_t pulseLengthMaximum = 4096;
const int32 numberOfServoDrivers = 1;
const int32 numberOfServos = 6;
const int32 numberOfServosPerDriver = 16;

ServoModel Class


class ServoModel
{
  private:
  int32 servoNumber;
  Adafruit_PWMServoDriver *servoDriver;
  uint16_t minimumValue_pulseLength;
  uint16_t maximumValue_pulseLength;
  uint16_t currentValue_pulseLength;
  
  public:
  ServoModel()
  {
  }

  ServoModel(Adafruit_PWMServoDriver *newServoDriver, int newServoNumber,
             uint16_t newMinimumValue_pulseLength, uint16_t newMaximumValue_pulseLength,
             uint16_t newValue_percent)
  {
    servoDriver = newServoDriver;
    servoNumber = newServoNumber;
    minimumValue_pulseLength = newMinimumValue_pulseLength;
    maximumValue_pulseLength = newMaximumValue_pulseLength;
    
    setToValue_percent(newValue_percent);
  }

  uint16_t setLowerPulseLength(uint16_t newValue_pulseLength)
  {
    if (newValue_pulseLength < pulseLengthMinimum)
    {
      newValue_pulseLength = pulseLengthMinimum;
    }
    minimumValue_pulseLength = newValue_pulseLength;
    return minimumValue_pulseLength;
  }

  uint16_t setUpperPulseLength(uint16_t newValue_pulseLength)
  {
    if (newValue_pulseLength > pulseLengthMaximum)
    {
      newValue_pulseLength = pulseLengthMaximum;
    }
    maximumValue_pulseLength = newValue_pulseLength;
    return maximumValue_pulseLength;
  }

  uint16_t setToMinimumValue()
  {
    return setToValue_pulseLength(minimumValue_pulseLength);
  }

  uint16_t setToMaximumValue()
  {
    return setToValue_pulseLength(maximumValue_pulseLength);
  }

  uint16_t setToValue_pulseLength(uint16_t newValue_pulseLength)
  {
    if (newValue_pulseLength < minimumValue_pulseLength)
    {
      newValue_pulseLength = minimumValue_pulseLength;
    }
    else if (newValue_pulseLength > maximumValue_pulseLength)
    {
      newValue_pulseLength = maximumValue_pulseLength;
    }
    currentValue_pulseLength = newValue_pulseLength;
    servoDriver->setPWM(servoNumber, 0, currentValue_pulseLength);
    return currentValue_pulseLength;
  }

  uint16_t setToValue_percent(float newValue_percent)
  {
    uint16_t newValue_pulseLength = minimumValue_pulseLength + (uint16_t)((newValue_percent / 100) * 
                                    (float)(maximumValue_pulseLength - minimumValue_pulseLength));
    return setToValue_pulseLength(newValue_pulseLength);
  }
};

Global Variables


Adafruit_PWMServoDriver servoDrivers[numberOfServoDrivers];
ServoModel servos[numberOfServos];

One Time Setup (When the controller initializes.)


void setup() {
  Serial.begin(9600);
  Serial.println("Begin Setup");

  // Initialize Servo Drivers
  Serial.println("Initialize Servo Drivers");
  servoDrivers[0] = Adafruit_PWMServoDriver(0x40);
  servoDrivers[0].begin();
  servoDrivers[0].setPWMFreq(60);
  
  // Initialize Servo Objects
  // The following loop automatically assigns the servos to the
  // available servo drivers.
  Serial.println("Initialize Servo Objects");
  int32 driverNumber = 0;
  int32 servoNumber = 0;
  for (int32 servoIndex = 0; servoIndex < numberOfServos; servoIndex++)
  {
    // Initialize with a min and max pulse length of 200 and 550 respectively.
    // This value varies by servo! Do not exceeds the physical limits of your servo!
    servos[servoIndex] = ServoModel(&servoDrivers[driverNumber], servoNumber,
                                    200, 550, 50);
    servoNumber ++;
    if (servoNumber >= numberOfServosPerDriver)
    {
      servoNumber = 0;
      driverNumber ++;
      if (driverNumber >= numberOfServoDrivers)
      {
        // Not good, we're out of drivers for how many servos specified!
        driverNumber = 0;
      } // driverNumber >= numberOfServoDrivers
    } // servoNumber >= numberOfServosPerDriver
  } // servoIndex

  Serial.println("Completed Setup");
}

How to Use


The code above is the foundation for how the microcontroller will interact with the servos.

To instruct servo 0 to go to its minimum value:
servos[0].setToMinimumValue();

To instructor servo 1 to go to 25%:
servos[1].setToValue_percent(25);


My next post will show how I'm reading inputs from the serial port to manipulate the values of the servo objects.


Copyright (c) 2015 Clinton Kam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Friday, August 7, 2015

Servo Controller: Intro

As my Arduino Retro Computer has come to a good closing (well, pausing) point, I'm starting a new long term project. I have some big goals in store, but I'm going to keep the exact plans publicly a little vague as I work through to the final product.


Without giving out too many details, I'd like to create a system that enables very quick and simple servo control by a computer program. There are additional input and output features that will be added, but at the heart of it is a robust servo control mechanism.

My previous projects have been with Arduino Megas, but this time I'm basing the device around the Arduino Due. It has some impressive stats over the Mega (let alone the Uno).

SRAM: Uno 2 kB, Mega 8 kB, Due 96 kB.
Flash: Uno 32 kB, Mega 256 kB, Due 512 kB.
Processor: Uno & Mega 16 MHz, Due 84 MHz

Plus the Due has loads of Digital I/O, PWM, Analog, and Serial pins.
https://learn.adafruit.com/adafruit-arduino-selection-guide/arduino-comparison-chart

This is all probably way overkill as I'm trying to keep the vast majority of the intelligence on the computer itself, but it will be fun trying out a new board. (Honestly though I may need to use a Due for version 2 of my Arduino Retro Computer!)

Adafruit has a very nice 16 channel I2C stackable servo shield. The shield has 6 address select pins so you can stack one on top of another with the same I2C bus. (According to their website up to 62 shields totally 992 servos!) https://www.adafruit.com/products/1411

Adafruit Servo Shield on Arduino Due

I've also begun testing quite a few servos to try and find the best balance of cost, compatibility, and performance. There are a lot of factors going into which servo to use so that will be in a post all its own.