Skip to content

Commit 6a05dd2

Browse files
Merge pull request #462 from inesmaria08/servo_motor_test
Userland app for servomotor
2 parents b85d28e + 28d789a commit 6a05dd2

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

examples/servo/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../..
5+
6+
# Which files to compile.
7+
C_SRCS := $(wildcard *.c)
8+
9+
# Include userland master makefile. Contains rules and flags for actually
10+
# building the application.
11+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk

examples/servo/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Servo Controller App
2+
=====================
3+
4+
A simple app that tests the functionality of a connected servo motor by reading its initial angle, adjusting the angle from 0 to 180 degrees, and verifying the actual angle at each step.
5+
6+
Example Output
7+
--------------
8+
9+
```
10+
The number of available servomotors is: 1
11+
Requested angle 0. Actual current angle is: 0
12+
Requested angle 1. Actual current angle is: 1
13+
Requested angle 2. Actual current angle is: 2
14+
...
15+
Requested angle 180. Actual current angle is: 180
16+
```
17+
18+
If the servo index is out of range:
19+
```
20+
The index number is bigger than the available servomotors
21+
```
22+
23+
If the servo cannot report its angle:
24+
```
25+
This servo cannot return its angle.
26+
```

examples/servo/main.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "servo_syscalls.h"
2+
#include <stdio.h>
3+
4+
bool libtock_servo_exists(void) {
5+
return driver_exists(DRIVER_NUM_SERVO);
6+
}
7+
8+
returncode_t libtock_servo_count(uint32_t* servo_count) {
9+
uint32_t value = 0;
10+
syscall_return_t r = command(DRIVER_NUM_SERVO, 1, 0, 0);
11+
returncode_t ret = tock_command_return_u32_to_returncode(r, &value);
12+
*servo_count = (uint16_t)value;
13+
return ret;
14+
15+
}
16+
17+
returncode_t libtock_servo_set_angle(uint16_t index, uint16_t angle) {
18+
syscall_return_t cval = command(DRIVER_NUM_SERVO, 2, index, angle);
19+
return tock_command_return_novalue_to_returncode(cval);
20+
}
21+
22+
returncode_t libtock_servo_read_angle(uint16_t index, uint16_t* angle) {
23+
uint32_t value = 0;
24+
syscall_return_t r = command(DRIVER_NUM_SERVO, 3, index, 0);
25+
returncode_t ret = tock_command_return_u32_to_returncode(r, &value);
26+
*angle = (uint16_t)value;
27+
return ret;
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "../../tock.h"
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
#define DRIVER_NUM_SERVO 0x90009
10+
11+
// Check if the servo system call driver is available on this board.
12+
bool libtock_servo_exists(void);
13+
// Returns the number of available servomotors.
14+
returncode_t libtock_servo_count(uint32_t* servo_count);
15+
// Change the angle.
16+
returncode_t libtock_servo_set_angle(uint16_t index, uint16_t angle);
17+
// Requests the current angle from the servo.
18+
returncode_t libtock_servo_read_angle(uint16_t index, uint16_t* angle);
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif

0 commit comments

Comments
 (0)