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.

No comments:

Post a Comment