# Calibration Procedures Overview
As the position control loop of the joint Q1 and Q2 are using relative encoders, there are two different calibration procedure.
* The //runtime// calibration, that is runned each time the motordriver board are reseted. Its purpose is to match the current relative encoder position with an absolute recorded position. Since the relative position is lost at each electronic reset, this procedure has to be re-runned. The driver from [[https://c4science.ch/source/liboncilla | liboncilla]] ensure this task is done every-time the robot is started.
* The //factory// or //mechanical// calibration, whose purpose is to record in the motordriver memory the absolute position needed by the //runtime calibration//. This procedure should be performed every time:
* a mechanical part as been repaired or changed
* a leg is re-assembled,
* every few hours of robot use because of mechanical wearing. Sessions up to 5 or 6 hours are usually fine
In order to use the robot in a proper way, we have to calibrate the robot. You can find Tools to calibrate and debug oncilla here : [[oncilla/software-tools]]
# Runtime Calibration
WARNING: This calibration has to be performed after every electronic reset. Security Routine in the motordriver board firmware will prevent any position/velocity or torque control if the runtime calibration has not been performed.
The runtime calibration state and start of the procedure is automatically checked and handled by the [[https://c4science.ch/source/liboncilla | liboncilla ]] driver.
During runtime calibration, the robot should not touch the ground. The knee will be first completely extended and then flexed a little bit to retrieve the exact point of full extension. If the legs are flexed during that procedure, the corresponding points will be miscalculated and thus the legs will always remains flexed until the next electronic reset.
# Factory Calibration
First of all you should test, if the magnetic encoders working correctly by reading them out.
```lang=bash
$ sudo oncilla-motor-reader
```
## L1 and L2 Calibration [BLDC motors]
The calibration of the L1 and L2 motor is done using the ##oncilla-motor-calibrator## program.
Before going into the calibration procedure, check if the knee cables are in the right setup (( [[ oncilla/checklist ]] )
Calibration of the motors connected to each motor board is done separately.
From the ssh terminal, go to ##~/devel/oncilla-motor-calibrator## and run:
```lang=bash
$ sudo build/src/oncilla-motor-calibrator -h
```
This gives you a reminder of all required option needed by the calibration program. You need to provide :
* the motor board ID
* the leg calibration template, which can be ##left_fore##, ##right_fore##, ##left_hind## or ##right_hind##.
Once you are ready to calibrate a leg, run the command:
```lang=bash
$ sudo build/src/oncilla-motor-calibrator -i <BOARD_ID> -t <LEG_TEMPLATE>
```
and the calibration procedure begins for the corresponding motorboard/leg.
1. The program will first ask you to move the L1 joint. move the leg (sample video [[ https://redmine.amarsi-project.eu/attachments/download/528/L1.AVI | here ]] ).
2. Now put the leg back in the vertical standing posture, and proceed as the program says.
3. Next you have to move the L2 motor. Flex the leg freely (sample video [[ https://redmine.amarsi-project.eu/attachments/download/529/L2.AVI | here ]]).
4. Put the leg back in the vertical standing posture. Make sure the cable is not loose and it can start to pull. Continue as the program says.
You should repeat this procedure for all motorboards/legs.
If the program stopped on an exception when calibrating (as for example the legs wasn't moved enough during the sampling period), just redo the whole calibration again for that join.
A full video of the calibration of one leg can be found [[ https://redmine.amarsi-project.eu/attachments/download/533/OncillaCalibrationProcessTake2.m4v | here ]]
## L0 Calibration (abduction/ adduction) [Servo motor]
The calibration of the hip servo motors can be done with the program rbio-servo-calibrator. Please notice that the program does not provide a -h option. Before you start the calibration procedure, be sure to be in the right directory [[oncilla/software-tools ]]
Now run the program and provide a new file to store the calibration values (e.g. here: ##servo-example-calibration.config##):
```lang=bash
$ sudo rbio-servo-calibrator servo-example-calibration.config
```
A new terminal with a grey background is opened. This program has its own prompt and command. Press TAB several times to get an overview of the existing commands.
In the beginning you have to check that the servos are connected to the right channel of the RB-110 (see [[ oncilla/part-description/ ]] . If it's not, select the servo motor (e.g left_fore servo):
```lang=bash
$ select left_fore
```
and assign the new Channelnumber. (*Note:* You can only assign free channels!)
```lang=bash
$ channel = x
```
where x denotes a number = [1 ... 15]
Now you can start to calibrate the servo.
IMPORTANT : Check that no moving parts touch each other during the calibration procedure and leave enough safety space. (e.g. link from servo to the hip joint!
Start with the max_position of the servo.
```lang=bash
$ start_max_positon
```
Press + or ' to adjust the current position of the servo motor. You can increase/decrease the stepsize of the motor by setting the pulse length.
```lang=bash
$ set_pulse_increment <value>
```
NOTE: The maximum value of the servo motor is always outside the robot. If it is reversed, you have to correct this by using the reversed command.
```lang=bash
$ reversed = true
```
After setting the calibrating position, you have to use the command stop_moving, to save the new position value.
```lang=bash
$ stop_moving
```
Now repeat the procedure for the minimum position of the servo with ##start_min_position##
```lang=bash
$ start_min_position
```
After calibrating the motor, stop moving to save the calibration value.
```lang=bash
$ stop_moving
```
When you have placed the minimal reachable position for the leg, unselect the servomotor, and proceed with the remaining servos like above.
```
$ unselect
$ select [left-fore | right-fore | left-hind | right-hind]
```
NOTE: There is a default config file which is used by the robot, which is the file named ##rbio-servo-calibration.config## in the current working directory where you run ##rbio-servo-calibrator## or the program using ##liboncilla##. If you want to use your new calibration values, please replace this default file by your own file created using ##rbio-servo-calibrator##. If this file doesn't exist, then no servo will be defined in ##rbio-servo-calibrator## and ##liboncilla## will return an exception.