|
| 1 | +#include "../../libtock/interface/syscalls/servo_syscalls.h" |
| 2 | +#include <libtock-sync/services/alarm.h> |
| 3 | +#include <libtock/tock.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +int main(void) { |
| 8 | + // Checks if the driver exists and, if not, returns -1. |
| 9 | + if (!libtock_servo_exists()) { |
| 10 | + printf("There is no available servo\n"); |
| 11 | + return -1; |
| 12 | + } |
| 13 | + returncode_t result = RETURNCODE_EOFF; |
| 14 | + uint32_t servo_count = 0; |
| 15 | + libtock_servo_count(&servo_count); |
| 16 | + printf("The number of available servomotors is: %ld\n", servo_count); |
| 17 | + uint16_t angle = 0; |
| 18 | + uint16_t index = 0; // the first index available. |
| 19 | + |
| 20 | + if (libtock_servo_read_angle(index, &angle) == RETURNCODE_ENODEVICE) { |
| 21 | + printf("\n The index number is bigger than the available servomotors\n"); |
| 22 | + return -1; |
| 23 | + } |
| 24 | + |
| 25 | + // Changes the angle of the servomotor from 0 to 180 degrees (waiting 0.1 ms between every change). |
| 26 | + for (int i = 0; i <= 180; i++) { |
| 27 | + // `result` stores the returned code received from the driver. |
| 28 | + result = libtock_servo_set_angle(index, i); |
| 29 | + if (result == RETURNCODE_SUCCESS) { |
| 30 | + libtocksync_alarm_delay_ms(100); |
| 31 | + // Verifies if the function successfully returned the current angle. |
| 32 | + if (libtock_servo_read_angle(index, &angle) == RETURNCODE_SUCCESS) { |
| 33 | + printf("Requested angle %d. Actual current angle is: %d\n", i, angle); |
| 34 | + } |
| 35 | + } else if (result == RETURNCODE_FAIL) { |
| 36 | + printf("\nAngle %d exceeds the servo's limit angle.\n", i); |
| 37 | + } |
| 38 | + } |
| 39 | + if (libtock_servo_read_angle(index, &angle) != RETURNCODE_SUCCESS) { |
| 40 | + printf("\n This servo cannot return its angle.\n"); |
| 41 | + } |
| 42 | +} |
0 commit comments