Micromouse motors with continuous servos

In my previous post I connected four analog distance sensors to my Arduino Due and fed the sensor data to Processing, which displayed a graph of the results. In this post I’m attaching continuous servos and hooking these pieces together to get object avoidance.

Servos

SG5010 Micro Servo 39g 180 Degree Plastic

A servo is a motor with an encoder and some gearing. The motor spins very fast as long as it has power. The gears trade the speed for torque – so it’s slower but also more accurate and more turning force. The encoder reports on the angle of the last gear, the one that you can see on the outside of the servo.

Typical servos have three wires: red for power in, black for ground, and white for the Pulse Width Modulated (PWM) signal. The PWM signal is a subject for another day. Sufice to say for now that it’s a really easy way to send a number from my Arduino to the servo. Servos usually have a range of 0 to 180 degrees, and Arduino Servo.write(x) can take an x between 0 and 180. If I write code that says Servo.write(90) the servo output gear will turn to the halfway point.

Continuous servos

One variation on servos are continuous servos. Instead of turning to the angle I want, they go to at the speed I want, with Servo.write(0) being full speed backwards, Servo.write(180) being full speed forwards, and Servo.write(90) being full stop.

Wiring micromouse motors

This is an upgrade to the previous robot schematic. The two servos are connected on Arduino digital pins 6 and 7. It worked OK, except for one problem: if the servo starts, stop, or changes direction suddenly, it can suck up so much power that the Arduino reboots!

Adding capacitors between the servo and the motor is like a tiny battery that mitigates the sudden power drain.

Micromouse motor code

This builds on our previous sensor code and adds motors. Wave your hand in front of the sensors to change the movement of the servos. Plus it still works with our Processing sketch!

This code worked when I wrote it. If you are reading this in the distant future, check the micromouse github project for more current versions.

// read four analog distance sensors and send the values to serial.
// Also drive two wheels.
// Dan Royer ([email protected])
// 2016-05-16


#define LEFT_STOP   90
#define RIGHT_STOP  90


#include 


Servo left, right;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  
  // prepare the wheels
  left.attach(7);
  right.attach(6);

  fullStop();
}


void fullStop() {
  left.write(LEFT_STOP);
  right.write(RIGHT_STOP);
}


void loop() {
  int a = analogRead(2);
  int b = analogRead(3);
  int c = analogRead(4);
  int d = analogRead(5);

  // put your main code here, to run repeatedly:
  Serial.print(a);  Serial.print("\t");
  Serial.print(b);  Serial.print("\t");
  Serial.print(c);  Serial.print("\t");
  Serial.print(d);  Serial.print("\n");

  int bottom = 0;
  int top = 1023/2;
  left.write(map(a,bottom,top,LEFT_STOP-30,LEFT_STOP+30));
  right.write(map(d,bottom,top,RIGHT_STOP-30,RIGHT_STOP+30));
  
  delay(5);
}

Extra micromouse traction

I used instant contact cement to glue a short piece of belt around the discs that come with my servos. More surface area means more traction.

Results

If you’re wondering “Where are the capacitors?” well… +1 for being so observant! I shot this video before adding the capacitors.

Next

I believe I now have all the basic electronics for the micromouse. I will add a battery so it can run wirelessly. The next major step is to attach the pieces all together in a small enough form factor that it will fit in the maze and see the walls.