MTobjects servomotors
Event management for Uno, Nano, Mega
Servo with 3 or 4 wires?
Normally, the servos we use have 3 wires, the reference (GND), the power supply (VCC) and the control wire. When switching on, if we do not know in which position is the servo, we choose one by default. If the servo is not in the right place, it will quickly put itself in position. To avoid this, some add a fourth wire which gives the position and which thus avoids at the power on a sudden movement. This fourth wire is welded to the medium point of the internal potentiometer.
MTobjects offers two servo: MTservo for 3-wire servos with sudden power on, by default at 90°, and MTservo4wires for 4-wire servos, with immobile power on.
Set up
Servos management is done in a hard way using a timer. We are therefore limited in number of servos (2 for the Nano/Uno and 12 for a Mega). To have more servos, you have to go through soft management, what Servo.h does.
The establishment of a timer must be done after the initialization of the card (not before the start of the setup). If the servo is defined before, which is the general case, it cannot be started at this time. This is why that a period of around 16ms is set up. The characteristics of the servo cannot be changed during the first 16 ms. The Functions setTimeFrom0To180(), writeDegree(), et writeMicrosecond() wait the end of these 16ms possibly and can therefore be called immediately without risk.
Speed
MTservo allows you to manage a servomotor with a hard PWM. The resolution could be 0.5µs, but that does not have practical sense. It is easier to keep a resolution of 1µs. We can define a speed by giving the time which is put so that the command goes from 0° to 180°. If we give a significant speed (for example 0ms or 1ms), the servo will go to its maximum speed, but for the software, the servo will have reached the destination long before real arrival.
MTservo for the 3-wires servo
Uncnored servos have 3 wires, one for GND, one for power and one to receive the position to be taken.
MTservo4wires for 4 wires servos
For a 4-wires servo, we will also give the pin of the 4th wire, the values of analogRead() for this pin for 0° and 180°. For reasons of compatibility and although in creation the servo is already in position, a stop is made with a call to onStop() and function pointed out by onStopfunction.