Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Userland app for servomotor #462

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions examples/servo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../..

# Which files to compile.
C_SRCS := $(wildcard *.c)

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
18 changes: 18 additions & 0 deletions examples/servo/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>

#include <libtock-sync/services/alarm.h>
#include <libtock-sync/interface/servo.c>

int main(void) {
// Checks if the driver exists and, if not, returns -1.
if (!libtock_servo_exists()) {
printf("There is no available servo\n");
return -1;
}
// Changes the angle of the servomotor from 0 to 180 degrees (waiting 0.1 ms between every change).
for (int i = 0; i<=180; i++) {
libtocksync_servo_angle(i);
libtocksync_alarm_delay_ms(100);
}
}
10 changes: 10 additions & 0 deletions libtock-sync/interface/servo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "servo.h"
#include "../../libtock/interface/syscalls/servo_syscalls.h"

returncode_t libtocksync_servo_angle(uint32_t angle) {
int err;
err = libtock_servo_angle(angle);
if (err != RETURNCODE_SUCCESS) return err;

return RETURNCODE_SUCCESS;
}
inesmaria08 marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions libtock-sync/interface/servo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <libtock/tock.h>

#ifdef __cplusplus
extern "C" {
#endif

// ## Arguments
//
// - `angle`: The angle you want the servo to move.
inesmaria08 marked this conversation as resolved.
Show resolved Hide resolved
//
// ## Return Value
//
// A returncode indicating whether the angle was changed successfully.
returncode_t libtocksync_servo_angle(uint32_t angle);

#ifdef __cplusplus
}
#endif
11 changes: 11 additions & 0 deletions libtock/interface/syscalls/servo_syscalls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "servo_syscalls.h"
#include <stdio.h>

bool libtock_servo_exists(void) {
return driver_exists(DRIVER_NUM_SERVO);
}

returncode_t libtock_servo_angle(uint32_t angle) {
syscall_return_t cval = command(DRIVER_NUM_SERVO, 1, angle, 0);
return tock_command_return_novalue_to_returncode(cval);
}
inesmaria08 marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions libtock/interface/syscalls/servo_syscalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "../../tock.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DRIVER_NUM_SERVO 0x90009

// Check if the servo system call driver is available on this board.
bool libtock_servo_exists(void);

// Change the angle.
returncode_t libtock_servo_angle(uint32_t angle);

#ifdef __cplusplus
}
#endif
Loading