Skip to content

Commit 698d753

Browse files
committed
servo test
1 parent b85d28e commit 698d753

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-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/main.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include <libtock-sync/services/alarm.h>
5+
#include <libtock-sync/interface/servo.c>
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+
// Changes the angle of the servomotor from 0 to 180 degrees (waiting 0.1 ms between every change).
14+
for (int i = 0; i<=180; i++) {
15+
libtocksync_servo_angle(i);
16+
libtocksync_alarm_delay_ms(100);
17+
}
18+
}

libtock-sync/interface/servo.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "servo.h"
2+
#include "../../libtock/interface/syscalls/servo_syscalls.h"
3+
4+
returncode_t libtocksync_servo_angle(uint32_t angle) {
5+
int err;
6+
err = libtock_servo_angle(angle);
7+
if (err != RETURNCODE_SUCCESS) return err;
8+
9+
return RETURNCODE_SUCCESS;
10+
}

libtock-sync/interface/servo.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
#include <libtock/tock.h>
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
// ## Arguments
9+
//
10+
// - `angle`: The angle you want the servo to move.
11+
//
12+
// ## Return Value
13+
//
14+
// A returncode indicating whether the angle was changed successfully.
15+
returncode_t libtocksync_servo_angle(uint32_t angle);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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_angle(uint32_t angle) {
9+
syscall_return_t cval = command(DRIVER_NUM_SERVO, 1, angle, 0);
10+
return tock_command_return_novalue_to_returncode(cval);
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
14+
// Change the angle.
15+
returncode_t libtock_servo_angle(uint32_t angle);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif

0 commit comments

Comments
 (0)