Thursday, September 13, 2018

ServoMeters -- Interactive Tactile Gauges with Servos

The Basics

Makers loves servos and, perhaps not surprisingly, some of my favorite servo applications are for building accessible haptic displays such as interactive tactile dials, gauges, and meters.

Servo motors are ubiquitous in hobby robotics. They are awesome little motors that incorporate a feedback system, making them extremely easy to use with Arduino and other microcontrollers. A few decades ago they were quite expensive, having most applications in aerospace or other fancy fields, but with the growth of hobby robotics and the maker movement, to say nothing of the popularity of RC vehicles, the size and price of servos have come down significantly at the same time as the reliability has gone up. Today you can buy little servos for well under $10 each and they are well supported with a Servo Library in the Arduino environment.

Servos come in two basic flavors: those with a range of motion of 0-180° and those that can rotate freely through 360°. Both kinds tend to be small rectangular boxes with a motor shaft sticking out of one side capable of mating with a variety of wheels, gears, arms, and other shapes designed to apply torque in one way or another. 180° servos have a built-in position sensor that lets you set the motor shaft to a specific orientation. Continuous rotation 360° servos have similar sensors, but for motor speed and direction instead of position. We will return to the topic of 360° servos presently. There are many other kinds of specialized servos, but for now we will stick to discussing these two types.

The 180° servos are capable of rotating their shafts through a half rotation and back again like a windshield wiper. More interestingly, you can tell the 180° servo to go to any position between 0° and 180°, and it will faithfully turn the shaft to that orientation and keep it there, even if you physically try to turn it away from that orientation. It's able to do this because it has a built-in sensor (usually a potentiometer) that lets it know what position it's in – if you try to force it away from where it's supposed to be, it tries to move back. This means it can reliably be placed at a specific angle and be counted upon to stay there.

Which Wires Are Which?

Before continuing with the discussion of using servos for accessibility we need to make the servos themselves accessible. Most servos have a color-coded, 3-conductor ribbon cable with a 3-pin female connector on the end. The colors are great if you can see, but if you can't, you'll need to know what's what with the wires.

Place your servo on a flat surface in front of you with the shaft pointing up and the ribbon cable toward you, making sure that the cable lies flat and is not twisted. In this configuration, the wires in the cable are FROUND, POWER, and SIGNAL from left to right for most servos. Check your product documentation if you are using an unusual servo.

Analog Gauges and Why They're Cool

Analog gauges have many distinct advantages over digital or text-to-speech for quantitative output. They are dynamic so readings can be constant rather than sampled at longer intervals. For example, a gauge that wiggles every time a wire is touched lets you know immediately that there's a loose connection, whereas an instrument with only a digital display might not catch this. A quick glance at a gauge lets you know if you are within an acceptable range much more efficiently than a digital readout, and a slightly longer look at the gauge gives you a reasonably accurate quantitative reading. All of these benefits apply to tactile versions of gauges as well.

Blind people have a long history of removing the glass from gauges in order to be able to read the position of the needle by touch. This worked great for analog alarm clocks which were designed to be thrown around a bit, but in most cases the glass is there for a reason: the needle is not designed to be touched. A delicate needle can move while being touched leading to a false reading. Worse, touching the needle could cause a delicate measurement instrument to permanently lose its calibration, leading to a long future of incorrect readings.

Meet the ServoMeter

The 180° servo is an ideal tool for building reliable and inexpensive gauges and meters designed to be touched – a category of displays I have dubbed ServoMeters. The robustness and pushback of the 180° servo is perfect for building tactile gauges, and the addition of tactile markings or braille numbers around the dial makes for an inexpensive, simple, and powerfully quantitative tool.

Servos are also much less expensive than TTS boards, putting tactile displays within easy reach of even the most financially strapped blind maker.

The most basic example of a servometer can be found right in the libraries directory of your Arduino development environment under Servo > Examples > Knob. The Knob.INO sketch demonstrates how to set the position of a 180° servo using a 10K potentiometer. From this example it is only a short hop of the imagination to see how you can use a servo to represent almost any quantitative Arduino output. The following is a modified version of the Knob.INO sketch I have used for teaching about servometers – a fundamental concept of accessible hardware design.

                    
/* 
 Controlling a servo position using a potentiometer (variable resistor) 
 by Michal Rinott  
 Modified by Josh Miele for the Blind Arduino Project, 05/11/2018
*/

#include <Servo.h>   //tells the compiler to use functions contained in the servo library

Servo myservo;  // create servo object to control a servo

int potPin = 0;  // analog pin used to connect the potentiometer
int servoPin = 5;  //Servo attached to digital pin 5
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(servoPin);  // attaches the servo on pin 5 to the servo object
}  //end of setup

void loop() 
{ 
  val = analogRead(potPin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits 15 milliseconds to stabilize the servo
} //end of main loop
 

In reality, many inexpensive 180° servos are a little unreliable at the extremes. It's generally best to limit your 180° servometer's range of motion to 160° (i.e. not using the 0-10° and 170-180° ends of the semicircle) as we have done in the sketch above. This can make placement of tactile markings something of a squeeze unless you have a rather long pointer attached to your servometer. For this reason it's often nice to have a gauge with more rotation than 180°. In fact, it's nice to be able to aim a pointer in any direction you want, giving you maximum flexibility with your servometer. Imagine, for example, trying to build a tactile digital compass.

The Continuous Rotation ServoMeter

Arduino communicates with continuous rotation 360 servos using the same servo object as the 180° servos. The Servo.write method accepts a value between 0 and 180, making it super easy to implement the 180° servometer. However, it is not so simple for 360° servos. Continuous rotation servos respond to the 0-180 input by rotating the servo shaft at a particular speed and direction with an input of 90 being no rotation, values slightly larger than 90 rotating the servo shaft slowly clockwise, and values slightly less than 90 rotating the shaft slowly counterclockwise. As values approach 0 or 180 the speed of rotation increases in a counterclockwise or clockwise direction respectively. This is cool, but it makes it pretty hard to rotate the shaft to a particular orientation and keep it there as is needed for a servometer. Luckily, you can buy a special 360° servo with a position feedback sensor built right into the unit. Parallax 900-00360 Feedback 360° High Speed Continuous Rotation

The product I have been experimenting with is the Parallax Feedback 360° High Speed Servo. It includes a Hall Effect sensor that provides reliable feedback regarding the orientation of the servo shaft. It requires a little knowledge of Arduino hardware interrupts and some mental-rotation gymnastics to implement something like the Knob sketch above, but it's not that complicated. The following short sections explain a little about how it works, but if you are impatient, feel free to skip it, download the knobFBS sketch, and start experimenting.

Sensor Feedback

The orientation of the servo shaft is measured not with a pot as is often done with 180° servos, but with a magnetic sensor that uses something called the Hall Effect. Feel free to study up on this cool electromagnetic phenomenon, but you don't need to understand how it works. What you do need to know is that, when the servo is hooked up to power and ground, the sensor sends almost a thousand pulses per second to the Arduino, and that the duration of each pulse is proportional to the angle of rotation. When the shaft is oriented to an arbitrary zero point, the pulses are at a minimum and are extremely short (about 24 microseconds). As the shaft rotates clockwise the duration of each pulse increases until the shaft has rotated almost all the way around to the starting point where the pulses will again become extremely short. Just before returning to zero, the pulses are much longer and are at a maximum (about 1076 microseconds). By measuring the duration of each pulse with the Arduino, and with knowledge of the exact durations of the shortest and longest possible pulses, we can calculate the current orientation of the shaft.

Being able to measure the orientation of the shaft makes it possible to use the servo speed and direction control to automate the movement of the servo shaft to any desired position – exactly what we need for a servometer. It also provides the possibility of using the feedback servo not just to display data, but also as a knob that can be adjusted by the user and read by the Arduino – a nice option for interactive accessible data display and input.

Measuring Minimum and Maximum Pulse duration

The best way to measure short pulses with an Arduino is by using external interrupts. Please feel free to explore this fascinating topic on your own, but for this project you just need to know that it's possible to use interrupts to instantly trigger desired actions through activity on certain digital pins. These actions take place regardless of what the main loop is currently doing, so interrupts are really great for catching input events that might otherwise be missed by a slower loop routine.

The following basicFBS.INO sketch allows you to measure the minimum and maximum pulse durations for your individual unit. It also provides a simple Sonification scheme for the servo's position sensor so you know it's working – as the servo turns clockwise the tone rises to about 1KHz and then jumps back down. Use a serial monitor and a screen reader to read the minimum and maximum pulse widths for use in the next section. I've tried to explain all the specific technical details in the code and comments, so take a careful read through this sketch to understand what's happening.

basicFBS.INO
/*********
* This sketch shows the basics of how to read from a 360Deg servo with feedback.
* Apot controls the direction and speed of the servo and a speaker sonifies the servo position.
* The serial monitor is used to read exact max and min values for the feedback pulse duration.
* For use with the parallax feedback 360° high speed servo
*     https://www.parallax.com/product/900-00360
* Feedback provided via Hall Effect sensor
* Duty cycle of sensor corresponds to position with a min of 2.9% frq approx 910 Hz
* (my unit min = 24 microS, max = 1076 microS)  
*********
* Wiring:
* Servo has a ribbon of 3 conductors and a second single wire, all with female connectors.
* With the servo shaft pointing up and the wires coming straight from the unit toward you the ribbon connections
* from left to right are GROUND, POWER, AND CONTROL. The single conductor is the feedback SENSOR.
* Connect CONTROL to D9 and the SENSOR to D2, with GROUND  and POWER connected to 0V and 5V respectively.
* Connect the arm of a 10K pot to A0 with one of the other two connections going to power and the other to ground.
* Connect one side of an 8W speaker to D5 and the other to GROUND.
* When you get tired of the sound you can disconnect one of the speaker connections.
*********
* Josh Miele for the Blind Arduino Project -- Sep-06-2018
*/////////

//variables for measuring pulse width from servo feedback sensor
//The width of the pulse corresponds to servo position
volatile int timeHigh = 0;  //number of microS in the current cycle
volatile int prevTime = 0;  //start time in microS (from reset) of the current high cycle

//specify I/O pins
const byte servoPin = 9;  //must support PWM
const byte sensorPin = 2; //must support hardware interrupt
const byte potPin = 0;   //analog pin for reading potentiometer
const byte speakerPin = 5;  //must support PWM

//Other variables
//For measuring your feedback unit's particular min and max pulse widths
int minTime = 500;   //a selection from the mid-range to be adjusted down as the servo turns
int maxTime = 500;  //a selection from the mid-range to be adjusted up as the servo turns

//use the standard servo library
#include <Servo.h>

Servo myservo;  // create servo object 

void setup() {
  myservo.attach(servoPin); 
  Serial.begin(9600);
  // when sensor pin goes high, call the rising function
  attachInterrupt(digitalPinToInterrupt(sensorPin), rising, RISING);
}  //end of setup fn

void loop() { 
  //use a pot to control the servo speed and direction
  //values received from the pot are between 0 and 1023
  //values sent to the servo should be between 0 and 180
  //map handles this conversion.
  myservo.write( map(analogRead(potPin), 0, 1023, 0, 180) );
  delay(5);    //for stability
  
  //sonify the servo feedback position
  //expect values between 20 and not much more than 1000 from the specs
  tone(speakerPin, timeHigh);  
  delay(5);  //again for stability
  
  //collect the min and max values for the pulse width of the feedback sensor
  //This will help us convert sensor feedback to angle for later use
  //Just let the servo spin for a while and the min and max will reach their extreme values after a few cycles
  if (minTime > timeHigh) minTime = timeHigh;
  if (maxTime < timeHigh) maxTime = timeHigh;
  printMinMax();  //see serial output function below
}   //end of main loop

void rising() {  //Interrupt service routine
  //Whenever the sensorPin goes high this routine is triggered
  attachInterrupt(digitalPinToInterrupt(sensorPin), falling, FALLING);  //reassign ISR to catch the other end of the pulse
  prevTime = micros();  //What time is it? It's safe to use micros in ISR for short durations
}  //end of rising fn

void falling() {  //interrupt service routine
  //during a pulse, this routine waits to be triggered by the drop in voltage
  attachInterrupt(digitalPinToInterrupt(sensorPin), rising, RISING);  //reset ISR to catch beginning of next pulse
  timeHigh = micros()-prevTime;  //This is how long the voltage was high in microS
}   //end of falling fn

void printMinMax() {
  //This routine prints the current min and max pulse width values on a single line 
  //Use Putty or some other terminal program to read it
  Serial.println("");  //put the newline at the beginning of  the process to stabilize braille display output
  Serial.print(minTime);
  Serial.print(' ');  //this puts a space between the numeric values
  Serial.print(maxTime);
}
knobFBS.INO

Now that you have measured the minimum and maximum pulse durations for your feedback servo, you can use the knobFBS.INO sketch below as a starting point for your own 360° servometer. Substitute your measured values for minTime and maxTime in the sketch and you should be up and running. As above, I tried to provide lots of explanatory comments in the sketch itself, so the best way to understand it is to read through it, download it, and play with it.

/*********
* This sketch shows how to control the position of a 360Deg servo with feedback.
* A pot controls the position of the servo and a speaker sonifies the servo position.
* For use with the parallax feedback 360° high speed servo
*     https://www.parallax.com/product/900-00360
* Feedback provided via Hall Effect sensor
* Duty cycle of sensor corresponds to position with a min of 2.9% frq approx 910 Hz
* (my unit min = 24 microS, max = 1076 microS)  
*********
* Wiring:
* Servo has a ribbon of 3 conductors and a second single wire, all with female connectors.
* With the servo shaft pointing up and the wires coming straight from the unit toward you the ribbon connections
* from left to right are GROUND, POWER, AND CONTROL. The single conductor is the feedback SENSOR.
* Connect CONTROL to D9 and the SENSOR to D2, with GROUND  and POWER connected to 0V and 5V respectively.
* Connect the arm of a 10K pot to A0 with one of the other two connections going to power and the other to ground.
* Connect one side of an 8W speaker to D5 and the other to GROUND.
* When you get tired of the sound you can disconnect one of the speaker connections.
*********
* Josh Miele for the Blind Arduino Project -- Sep-06-2018
*/////////

//variables for measuring pulse width from servo feedback sensor
volatile int timeHigh = 0;
volatile int prevTime = 0;

//specify I/O pins
const byte servoPin = 9;
const byte sensorPin = 2; 
const byte potPin = 0;
const byte speakerPin = 5;

//Other variables
//use the basicSFB sketch to collect these values for your particular unit
float minTime = 24;  //min pulse width in microS corresponding to angle of 0
float maxTime = 1076;  //max pulse width in microS corresponding to 359.99 deg

#include <Servo.h>

Servo myservo;  // create servo object 

void setup() {
  myservo.attach(servoPin); 
  Serial.begin(9600);
  // when sensor pin goes high, call the rising function
  attachInterrupt(digitalPinToInterrupt(sensorPin), rising, RISING);
}  //end of setup fn

void loop() { 
  //use a pot to specify target angle
  int potVal = analogRead(potPin);  //expect values between 0 and 1023 from pot
  //fbServoGo expects values between 0 and 360, so use map to get there from potVal
  int targetAngle = map(potVal, 0, 1023, 0, 360); 
  fbServoGo(targetAngle);  //see function below for setting the servo position
  tone(speakerPin, time2deg(timeHigh)+360);  //sonify the servo position over 1 octave
  delay(5);  //for stability
}   //end of main loop

void rising() {  //Interrupt service routine
  attachInterrupt(digitalPinToInterrupt(sensorPin), falling, FALLING);  //reassign ISR to catch the other end of the high time
  prevTime = micros();  //What time is it? It's safe to use micros in ISR for short durations
}  //end of rising fn

void falling() {  //interrupt service routine
  attachInterrupt(digitalPinToInterrupt(sensorPin), rising, RISING);  //reset ISR to catch next voltage rise
  timeHigh = micros()-prevTime;  //This is how long the voltage was high
}   //end of falling fn

int time2deg(int time) {
  //convert pulse duration to servo angle in degrees
  return ((time - minTime) / (maxTime - minTime)) * 360.99;
}   //end of time2deg fn

void fbServoGo(int targetAngle) {
  //Make the  feedback servo point in  a particular direction
  //with the minimum of movement (don't go the long way around)
  byte speed = 2;  //larger values slow response and increase stability
  int servoAngle;  //just initializing it for later.
  int currentAngle = time2deg(timeHigh);  //gets servo position in degrees
  
  //calculate the 360 deg difference between current and target angles.
  int angleDifference = (targetAngle - currentAngle + 360) % 360;
  
  //calculate the absolute (minimum) angle difference between current and target angles
  int absAngleDifference = abs(360-angleDifference);
  
  //Use 360 deg angle difference to decide which way to turn (CW or CCW)
  //use absolute angle difference to adjust motor speed as you get closer to the target.
  if (angleDifference <= 180) servoAngle = 90 - (absAngleDifference/speed);
  else servoAngle = 90 + (absAngleDifference/speed);
  myservo.write(constrain(servoAngle, 0, 180)); //use constrain to keep things reasonable...
}

2 comments:

  1. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
    Mysql online training

    ReplyDelete
  2. You are so awesome! I don’t suppose I have read anything like this before. So wonderful to discover another person with genuine thoughts on this issue. Really.. many thanks for starting this up. This site is something that’s needed on the web, someone with a little originality. 먹튀검증


    ReplyDelete