Skip to content
Snippets Groups Projects
Commit fc522134 authored by Florian Hölzl's avatar Florian Hölzl
Browse files

Rework of Raspberry platform architecture for upcoming lab course.

refs 3079
parent 37d70f35
No related branches found
No related tags found
No related merge requests found
Showing
with 35 additions and 1354 deletions
#ifndef __LIBCANSOCKET_H
#define __LIBCANSOCKET_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#define SR_ERR -1
struct can_socket {
int s;
struct sockaddr_can addr;
struct ifreq ifr;
};
/**
* Opens a can socket on the given interface
*/
struct can_socket* can_socket_open(const char *if_name);
/**
* Closes a given socket
*/
void can_socket_close(struct can_socket* sock);
/**
* Sends a can frame on the specified socket
*/
ssize_t can_socket_send(struct can_socket* sock, struct can_frame* frame);
/**
*
*/
ssize_t can_socket_receive(struct can_socket* sock, struct can_frame* frame);
#endif // __LIBCANSOCKET_H
#ifndef __LIBMAESTRO_H
#define __LIBMAESTRO_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
int maestroRead(int fd, unsigned char channel);
int maestroWrite(int fd, unsigned char channel, unsigned short target);
#endif // __LIBMAESTRO_H
/**
* @file uart.h
* @author Bianca Forkel
*/
#ifndef MBSE_UART_H
#define MBSE_UART_H
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/select.h>
extern int uart_filestream;
char port[13];
int uart_setup(const char*);
int uart_send_byte(char, int);
int uart_send_string(char*, int, int);
int uart_receive(char*, int, int);
void uart_close();
void uart_flush();
#endif
/* generated by AutoFOCUS 3 (2.11.0) on Tue May 16 17:42:09 CEST 2017 */
#include "inc-gen/data_dictionary.h"
/*GEN_TYPE_double abs(GEN_TYPE_double _V){
return _V * sgn(_V);
}
GEN_TYPE_double arctan(GEN_TYPE_double _V){
if (abs(_V) <= 1.0) {
return _V / (1 + (0.28 * _V) * _V);
}
else {
if (_V > 1) {
return pi() / 2.0 - _V / (_V * _V + 0.28);
}
else {
return -(pi() / 2.0) - _V / (_V * _V + 0.28);
}
}
}
GEN_TYPE_double cos(GEN_TYPE_double _V){
return sin((_V + pi() / 2.0));
}
GEN_TYPE_double cot(GEN_TYPE_double _V){
return cos(_V) / sin(_V);
}
GEN_TYPE_double e(){
return 2.718281828459;
}
GEN_TYPE_double exp(GEN_TYPE_double _V){
return ((((1 + _V) + (_V * _V) / 2) + ((_V * _V) * _V) / 6) + (((_V * _V) * _V) * _V) / 24) + ((((_V * _V) * _V) * _V) * _V) / 120;
}
GEN_TYPE_double ln(GEN_TYPE_double _V){
if (_V <= 0) {
return 0;
}
else {
return ((((_V - 1.0) - ((_V - 1.0) * (_V - 1.0)) / 2.0) + (((_V - 1) * (_V - 1)) * (_V - 1)) / 3.0) - ((((_V - 1) * (_V - 1)) * (_V - 1)) * (_V - 1)) / 4.0) + (((((_V - 1) * (_V - 1)) * (_V - 1)) * (_V - 1)) * (_V - 1)) / 5.0;
}
}
GEN_TYPE_double pi(){
return 3.14159265;
}
GEN_TYPE_double sgn(GEN_TYPE_double _V){
if (_V < 0) {
return -1;
}
else {
if (_V == 0) {
return 0;
}
else {
return 1;
}
}
}
GEN_TYPE_double sin(GEN_TYPE_double _V){
if (_V % (2 * pi()) < 0) {
return -sin((-_V));
}
else {
if (_V % (2 * pi()) > pi()) {
return -sin((_V % (2 * pi()) - pi()));
}
else {
if (_V % (2 * pi()) < 0) {
if (1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())) < 0) {
return 0.225 * ((1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi()))) * -(1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi()))) - (1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())))) + (1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())));
}
else {
return 0.225 * ((1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi()))) * (1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi()))) - (1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())))) + (1.27323954 * (_V % (2 * pi())) + (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())));
}
}
else {
if (1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())) < 0) {
return 0.225 * ((1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi()))) * -(1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi()))) - (1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())))) + (1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())));
}
else {
return 0.225 * ((1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi()))) * (1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi()))) - (1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())))) + (1.27323954 * (_V % (2 * pi())) - (0.405284735 * (_V % (2 * pi()))) * (_V % (2 * pi())));
}
}
}
}
}
GEN_TYPE_double sqrt(GEN_TYPE_double _V){
if (_V <= 0) {
return 0;
}
else {
return _sqrt(_V, _sqrt(_V, _sqrt(_V, _sqrt(_V, _sqrt(_V, _sqrt(_V, _sqrt(_V, _sqrt(_V, _sqrt(_V, _sqrt(_V, ((_V + 1.0) / 2.0)))))))))));
}
}
GEN_TYPE_double tan(GEN_TYPE_double _V){
return sin(_V) / cos(_V);
}
GEN_TYPE_double _sqrt(GEN_TYPE_double _X,GEN_TYPE_double _V){
return 0.5 * (_V + _X / _V);
}
GEN_TYPE_double pow(GEN_TYPE_double _V,GEN_TYPE_double _N){
if (_N == 0) {
return 1;
}
else {
if (_N < 0) {
if (_V == 0) {
return 0;
}
else {
return 1.0 / pow(_V, (-_N));
}
}
else {
if (_N == 1) {
return _V;
}
else {
return _V * pow(_V, (_N - 1));
}
}
}
}
GEN_TYPE_double factorial(GEN_TYPE_double _V){
if (_V % 1 != 0) {
return factorial((_V - _V % 1));
}
else {
if (_V == 0 || _V == 1) {
return 1;
}
else {
return factorial((_V - 1)) * _V;
}
}
}
GEN_TYPE_double min(GEN_TYPE_double _a,GEN_TYPE_double _b){
if (_a < _b) {
return _a;
}
return _b;
}
GEN_TYPE_double max(GEN_TYPE_double _a,GEN_TYPE_double _b){
if (_a > _b) {
return _a;
}
return _b;
}
GEN_TYPE_double rad2deg(GEN_TYPE_double _A){
return (_A / pi()) * 180.0;
}
GEN_TYPE_double deg2rad(GEN_TYPE_double _A){
return (_A / 180.0) * pi();
}*/
#include "inc/CanConnector.h"
// constants
const char* CAN_DEVICE_NAME = "can0";
// variables
struct can_socket* can_sock;
// send and receive
void can_send() {
// TODO send and empty buffer
}
void can_send_clock(CAN_CLK_T counter) {
// TODO send clock message with clock counter
}
void can_receive() {
// TODO fill buffer
}
CAN_CLK_T can_receive_clock() {
// TODO receive clock message and return clock counter
}
// init, terminate
void can_init() {
printf("initializing CAN\n");
can_sock = can_socket_open(CAN_DEVICE_NAME);
// TODO init buffers
}
void can_term() {
printf("terminating CAN\n");
can_socket_close(can_sock);
}
void prepare_input_CanConnector() {}
void finish_input_CanConnector() {}
void prepare_output_CanConnector() {}
void finish_output_CanConnector() {}
// noval
GEN_TYPE_boolean can_is_noval(CAN_MSG_T msg) {
return false;
}
GEN_TYPE_boolean can_set_noval(CAN_MSG_T msg) {
return false;
}
// readers
GEN_TYPE_boolean can_read_bool(CAN_MSG_T msg) {
return false; // TODO
}
GEN_TYPE_double can_read_double(CAN_MSG_T msg) {
return 0.0; // TODO
}
GEN_TYPE_int can_read_int(CAN_MSG_T msg) {
return 0; // TODO
}
// writers
void can_write_bool(CAN_MSG_T msg, GEN_TYPE_boolean value) {
// TODO
}
void can_write_double(CAN_MSG_T msg, GEN_TYPE_double value) {
// TODO
}
void can_write_int(CAN_MSG_T msg, GEN_TYPE_int value) {
// TODO
}
#include "inc/GPIO.h"
// init, term
GEN_TYPE_boolean gpio_initialized = false;
GEN_TYPE_boolean gpio_terminated = false;
void gpio_init(GPIO_PIN_T pin) {
// important, since all pins are accessing the same method
if(gpio_initialized) return;
if(wiringPiSetup() == -1) {
perror("cannot setup wiring pi\n");
return;
}
gpio_initialized = true;
}
void gpio_init_in(GPIO_PIN_T pin) {
gpio_init(pin);
pinMode(pin, INPUT);
}
void gpio_init_out(GPIO_PIN_T pin) {
gpio_init(pin);
pinMode(pin, OUTPUT);
}
void gpio_term(GPIO_PIN_T pin) {
// important, since all pins are accessing the same method
if(gpio_terminated) return;
gpio_terminated = false;
}
// noval
GEN_TYPE_boolean gpio_is_noval(GPIO_PIN_T pin) {
return false;
}
void gpio_set_noval(GPIO_PIN_T pin) {
// do nothing
}
// readers
GEN_TYPE_boolean gpio_read(GPIO_PIN_T pin) {
return digitalRead(pin);
}
// writers
void gpio_write(GPIO_PIN_T pin, GEN_TYPE_boolean value) {
digitalWrite(pin, value);
}
#include "inc/Gamepad.h"
// constants
const char* GP_DEVICE_NAME = "/dev/input/js0";
// variables
volatile union input_events ie; // stores the button values
// worker
GEN_TYPE_boolean gp_is_running = true;
void* gp_worker(void* pt_args) {
int fd;
fd = open(GP_DEVICE_NAME, O_RDONLY);
if(fd <= 0) {
perror("Error while connecting to gamepad device. Aborting");
return NULL;
}
struct timeval timeout;
struct js_event jse;
fd_set read_fds, write_fds, except_fds;
// loop until terminated...
while(gp_is_running) {
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(fd, &read_fds);
timeout.tv_sec = 0;
timeout.tv_usec = 25000;
if (select(fd + 1, &read_fds, &write_fds, &except_fds, &timeout) == 1) {
if(read(fd,&jse,sizeof(jse)) == sizeof(jse)) {
jse.type &= ~JS_EVENT_INIT;
if(jse.type == JS_EVENT_AXIS) {
ie.inps[jse.number] = jse.value;
#ifdef DEBUG
printf("Axis %d %d\n", jse.number, jse.value);
#endif // DEBUG
}
if(jse.type == JS_EVENT_BUTTON) {
ie.inps[jse.number + 6] = jse.value;
#ifdef DEBUG
printf("Button %d %d\n",jse.number, jse.value);
#endif // DEBUG
}
}
}
}
close(fd);
return NULL;
}
// init, term
GEN_TYPE_boolean gp_initialized = false;
GEN_TYPE_boolean gp_terminated = false;
void gp_init() {
// important, since all btns are accessing the same method
if(gp_initialized) return;
// Initialize Joystick Inputs to 0
ie.btns.js1_ud = 0;
ie.btns.js2_lr = 0;
ie.btns.js2_ud = 0;
// Initialize Button inputs to NoVal
ie.btns.bt_tri = 2;
ie.btns.bt_cir = 2;
ie.btns.bt_x = 2;
ie.btns.bt_sqr = 2;
ie.btns.bt_l1 = 2;
ie.btns.bt_l2 = 2;
ie.btns.bt_r1 = 2;
ie.btns.bt_r2 = 2;
gp_initialized = true;
}
void gp_term() {
// important, since all btns are accessing the same method
if(gp_terminated) return;
gp_terminated = false;
gp_is_running = false;
}
// readers: Return button values (1 is true, 0 is false, 2 is NoVal)
// important: Reset button values to NoVal after reading
GEN_TYPE_boolean gp_btn1_read() {
if (ie.btns.bt_tri == 1)
{
ie.btns.bt_tri = 2;
return 1;
}
else
{
ie.btns.bt_tri = 2;
return 0;
}
}
GEN_TYPE_boolean gp_btn2_read() {
if (ie.btns.bt_cir == 1)
{
ie.btns.bt_cir = 2;
return 1;
}
else
{
ie.btns.bt_cir = 2;
return 0;
}
}
GEN_TYPE_boolean gp_btn3_read() {
if (ie.btns.bt_x == 1)
{
ie.btns.bt_x = 2;
return 1;
}
else
{
ie.btns.bt_x = 2;
return 0;
}
}
GEN_TYPE_boolean gp_btn4_read() {
if (ie.btns.bt_sqr == 1)
{
ie.btns.bt_sqr = 2;
return 1;
}
else
{
ie.btns.bt_sqr = 2;
return 0;
}
}
GEN_TYPE_boolean gp_btnL1_read() {
if (ie.btns.bt_l1 == 1)
{
ie.btns.bt_l1 = 2;
return 1;
}
else
{
ie.btns.bt_l1 = 2;
return 0;
}
}
GEN_TYPE_boolean gp_btnL2_read() {
if (ie.btns.bt_l2 == 1)
{
ie.btns.bt_l2 = 2;
return 1;
}
else
{
ie.btns.bt_l2 = 2;
return 0;
}
}
GEN_TYPE_boolean gp_btnR1_read() {
if (ie.btns.bt_r1 == 1)
{
ie.btns.bt_r1 = 2;
return 1;
}
else
{
ie.btns.bt_r1 = 2;
return 0;
}
}
GEN_TYPE_boolean gp_btnR2_read() {
if (ie.btns.bt_r2 == 1)
{
ie.btns.bt_r2 = 2;
return 1;
}
else
{
ie.btns.bt_r2 = 2;
return 0;
}
}
GEN_TYPE_double gp_btnLX_read() {
return ie.btns.js1_lr;
}
GEN_TYPE_double gp_btnLY_read() {
return -ie.btns.js1_ud;
}
GEN_TYPE_double gp_btnRX_read() {
return ie.btns.js2_lr;
}
GEN_TYPE_double gp_btnRY_read() {
return -ie.btns.js2_ud;
}
// noval
GEN_TYPE_boolean gp_btn1_is_noval() {
if (ie.btns.bt_tri == 2) return 1;
return 0;
}
GEN_TYPE_boolean gp_btn2_is_noval() {
if (ie.btns.bt_cir == 2) return 1;
return 0;
}
GEN_TYPE_boolean gp_btn3_is_noval() {
if (ie.btns.bt_x == 2) return 1;
return 0;
}
GEN_TYPE_boolean gp_btn4_is_noval() {
if (ie.btns.bt_sqr == 2) return 1;
return 0;
}
GEN_TYPE_boolean gp_btnL1_is_noval() {
if (ie.btns.bt_l1 == 2) return 1;
return 0;
}
GEN_TYPE_boolean gp_btnL2_is_noval() {
if (ie.btns.bt_l2 == 2) return 1;
return 0;
}
GEN_TYPE_boolean gp_btnR1_is_noval() {
if (ie.btns.bt_r1 == 2) return 1;
return 0;
}
GEN_TYPE_boolean gp_btnR2_is_noval() {
if (ie.btns.bt_r2 == 2) return 1;
return 0;
}
GEN_TYPE_boolean gp_btnLX_is_noval() {
return false;
}
GEN_TYPE_boolean gp_btnLY_is_noval() {
return false;
}
GEN_TYPE_boolean gp_btnRX_is_noval() {
return false;
}
GEN_TYPE_boolean gp_btnRY_is_noval() {
return false;
}
#include "inc/LaserRangeFinder.h"
int timeout;
/** Initialize the laser range finder. */
void lrf_init() {
timeout = 0;
char port[100];
FILE* fp = popen("ls -d /dev/serial/by-id/* | grep \"Arduino_Micro\" | tr -d \"\n\"", "r");
fgets(port, sizeof(port), fp);
printf("lrf port=%s\n", port);
pclose(fp);
uart_setup(port);
}
/** Terminate the session. */
void lrf_term() {
uart_close();
}
/** Checking for noVal. */
GEN_TYPE_boolean lrf_is_noval() {
return false;
}
/** Get one distance measurement from the laser range finder. */
GEN_TYPE_int lrf_read() {
if(timeout>1000)
lrf_init();
if (uart_filestream == -1) {
timeout++;
printf("-1: no filestream\n");
return -1;
}
//empty buffer
uart_flush();
//initialize arrays
int sizeB = 10;
int sizeV = 4;
int i;
char bytes[sizeB];
for (i = 0; i < sizeB; i++) {
bytes[i] = 0;
}
int n_read = 0;
char value[sizeV];
for (i = 0; i < sizeV; i++) {
value[i] = 0;
}
int n = 0;
int d;
//receive values
while (1) {
n_read = uart_receive(bytes, sizeB, 0);
if(n_read <= 0) {
timeout++;
printf("-1: n_read=%d\n",n_read);
return -1;
}
for (i = 0; i < n_read; i++) {
if (bytes[i] != 0xd && bytes[i] != 0xa) {
if(n < sizeV)
value[n++] = bytes[i];
else {
timeout++;
printf("-1: too many bytes\n");
return -1;
}
} else {
d = atoi(value);
if (d == 0) {
timeout++;
printf("-1: zero value\n");
return -1;
} else if(d == 1061) { //strange behaviour of one lrf
printf("-1: 1061\n");
return -1;
}
else {
printf("%d\n", d);
timeout = 0;
return d;
}
}
}
}
}
#include "inc/PWM.h"
// constants
const int PWM_SLEEP_TIME = 50000;
const char* PWM_DEVICE_NAME = "/dev/ttyACM0";
const size_t PWM_MAX_NO_CHANNELS = 4;
// worker
GEN_TYPE_boolean pwm_is_running = true;
GEN_TYPE_int* pwm_channel_values;
GEN_TYPE_boolean* pwm_channel_enabled;
void pwm_send(int fd) {
size_t i;
for(i = 0; i < PWM_MAX_NO_CHANNELS; i++) {
if(pwm_channel_enabled[i]) {
maestroWrite(fd, i, pwm_channel_values[i]);
}
}
}
void* pwm_worker(void* pt_args) {
int fd = open(PWM_DEVICE_NAME, O_RDWR | O_NOCTTY);
if (fd == -1) {
perror("Error while connecting to pwm device");
return NULL;
}
struct termios options;
tcgetattr(fd, &options);
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_oflag &= ~(ONLCR | OCRNL);
tcsetattr(fd, TCSANOW, &options);
while(pwm_is_running) {
pwm_send(fd);
usleep(PWM_SLEEP_TIME);
}
close(fd);
return NULL;
}
// init, term
GEN_TYPE_boolean pwm_initialized = false;
GEN_TYPE_boolean pwm_terminated = false;
void pwm_init(PWM_CHANNEL_T channel) {
// important, since all pwms are accessing the same method
if(!pwm_initialized) {
pwm_channel_values = calloc(sizeof(GEN_TYPE_int), PWM_MAX_NO_CHANNELS);
pwm_channel_enabled = calloc(sizeof(GEN_TYPE_boolean), PWM_MAX_NO_CHANNELS);
// start a pwm thread
//pthread_t pt_pwm;
//pthread_create(&pt_pwm, NULL, pwm_worker, NULL);
pwm_initialized = true;
}
pwm_channel_enabled[channel] = true;
}
void pwm_term(PWM_CHANNEL_T channel) {
// important, since all pwms are accessing the same method
if(pwm_terminated) return;
free(pwm_channel_values);
free(pwm_channel_enabled);
pwm_is_running = false;
pwm_terminated = true;
}
// writers
void pwm_write(PWM_CHANNEL_T channel, GEN_TYPE_int value) {
pwm_channel_values[channel] = PWM_ADJUST_VALUE(value);
}
void pwm_set_noval(PWM_CHANNEL_T channel) {
// do nothing, not used
}
#include "inc/io.h"
// init, terminate
void init_left_stick() {
}
void term_left_stick() {
}
void init_right_stick() {
}
void term_right_stick() {
}
// NOVAL
GEN_TYPE_boolean is_noval_left_stick() {
return false;
}
GEN_TYPE_boolean is_noval_right_stick() {
return false;
}
// readers
GEN_TYPE_double read_left_stick() {
return 0.0;
}
GEN_TYPE_double read_right_stick() {
return 0.0;
}
// writers
void write_left_stick(GEN_TYPE_double value) {
}
void write_right_stick(GEN_TYPE_double value) {
}
#include "inc/libcansocket.h"
struct can_socket* can_socket_open(const char *if_name) {
struct can_socket* sock = malloc(sizeof(struct can_socket));
/* open socket */
if ((sock->s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("socket");
return NULL;
}
sock->addr.can_family = AF_CAN;
/* map if name to interface index -> will fail if interface does not exist */
strcpy(sock->ifr.ifr_name, if_name);
if (ioctl(sock->s, SIOCGIFINDEX, &sock->ifr) < 0) {
perror("SIOCGIFINDEX");
return NULL;
}
sock->addr.can_ifindex = sock->ifr.ifr_ifindex;
/* bind socket to interface -> will fail if interface is not up */
if (bind(sock->s, (struct sockaddr *)&sock->addr, sizeof(sock->addr)) < 0) {
perror("bind");
return NULL;
}
return sock;
}
void can_socket_close(struct can_socket* sock) {
close(sock->s);
}
ssize_t can_socket_send(struct can_socket* sock, struct can_frame* frame) {
ssize_t nbytes;
/* send frame */
if ((nbytes = write(sock->s, &frame, sizeof(*frame))) != sizeof(*frame)) {
perror("write");
return SR_ERR;
}
return nbytes;
}
ssize_t can_socket_receive(struct can_socket* sock, struct can_frame* frame) {
ssize_t nbytes;
if ((nbytes = read(sock->s, frame, sizeof(struct can_frame))) != sizeof(*frame)) {
perror("read");
return SR_ERR;
}
return nbytes;
}
#include "inc/libmaestro.h"
// read from channel given a file descriptor
int maestroRead(int fd, unsigned char channel) {
unsigned char command[] = {0x90, channel};
if(write(fd, command, sizeof(command)) == -1)
{
perror("error writing");
return -1;
}
unsigned char response[2];
if(read(fd,response,2) != 2)
{
perror("Error reading from maestro");
return -1;
}
return response[0] + 256*response[1];
}
// write target to a channel given a file descriptor
// the units of 'target' are quarter-microseconds
int maestroWrite(int fd, unsigned char channel, unsigned short target) {
unsigned char command[] = {0x84, channel, (target & 0x7F), (target >> 7 & 0x7F)};
if (write(fd, command, sizeof(command)) == -1)
{
perror("Error writing to maestro");
return -1;
}
return 0;
}
/**
* @file uart.c
* @author Bianca Forkel
*/
#include "inc/libuart.h"
int uart_filestream = -1;
/** Initializing the UART connection. */
int uart_setup(const char *porta) {
strcpy(port, porta);
uart_filestream = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if (uart_filestream < 0) {
fprintf(stderr, "[%s] Error opening port: %s\n", port, strerror(errno));
}
struct termios tty;
memset(&tty, 0, sizeof tty);
if (tcgetattr(uart_filestream, &tty) != 0) {
fprintf(stderr, "[%s] Error from tcgetattr: %s\n", port, strerror(errno));
}
// Baud Rate
cfsetospeed(&tty, (speed_t) B115200);
cfsetispeed(&tty, (speed_t) B115200);
// TTY settings: 8N1
tty.c_cflag &= ~PARENB; //no parity
tty.c_cflag &= ~CSTOPB; // 1 stop bit
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; // 8 data bits
tty.c_cflag &= ~CRTSCTS;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 5;
tty.c_cflag |= CREAD | CLOCAL;
cfmakeraw(&tty);
uart_flush();
if (tcsetattr(uart_filestream, TCSANOW, &tty) != 0) {
fprintf(stderr, "[%s] Error from tcsetattr: %s\n", port, strerror(errno));
}
return uart_filestream;
}
/** Sending one byte via UART. */
int uart_send_byte(char data, int printt) {
if (uart_filestream != -1) {
int n_written = 0;
n_written = write(uart_filestream, &data, 1);
if (n_written < 0) {
if (printt)
fprintf(stderr, "[%s] Error sending byte: %s\n", port, strerror(errno));
} else {
if (printt)
printf("[%s] Sent byte %#02x\n", port, data);
return 0;
}
}
return -1;
}
/** Receive from the UART stream. */
int uart_receive(char *c, int bufLen, int printt) {
fd_set set;
struct timeval timeout;
int rv;
int n_read = -1;
if (uart_filestream != -1) {
FD_ZERO(&set);
FD_SET(uart_filestream, &set);
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
rv = select(uart_filestream + 1, &set, NULL, NULL, &timeout);
if (rv == -1) {
if (printt)
perror("select");
} else if (rv == 0) {
if (printt)
perror("timeout");
} else {
n_read = read(uart_filestream, (void *) c, bufLen);
}
if (n_read < 0) {
if (printt)
fprintf(stderr, "[%s] Error receiving byte: %s\n", port, strerror(errno));
n_read = 0;
} else if (n_read == 0) {
if (printt)
printf("[%s] No data to receive \n", port);
} else {
if (printt) {
printf("[%s] %i Bytes received: 0x", port, n_read);
int i;
for (i = 0; i < n_read; i++)
printf("%02x", c[i]);
printf("\n");
}
}
}
return n_read;
}
/** Close the UART connection. */
void uart_close() {
close(uart_filestream);
}
/** Flush the UART buffer. */
void uart_flush() {
tcflush(uart_filestream, TCIFLUSH);
}
/*
* The main function and everything related to starting the application
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "inc-gen/system.h"
#include "inc/CanConnector.h"
#include "inc/Gamepad.h"
#include "inc/PWM.h"
#define PARAM_MAXCMP 5
#define PARAM_MASTER "master"
#define PARAM_SLAVE "slave"
int main(int argc, char* argv[]) {
// init the system
initialize_system();
// start a gamepad thread
pthread_t pt_gamepad;
pthread_create(&pt_gamepad, NULL, gp_worker, NULL);
// start a pwm thread
pthread_t pt_pwm;
pthread_create(&pt_pwm, NULL, pwm_worker, NULL);
// MASTER MODE (master as parameter)
if(argc >= 2 && strncmp(argv[1], PARAM_MASTER, PARAM_MAXCMP) == 0) {
// TODO do master stuff
}
// SLAVE MODE (slave as parameter)
else if(argc >= 2 && strncmp(argv[1], PARAM_SLAVE, PARAM_MAXCMP) == 0) {
// TODO do slave stuff
}
// DEFAULT MODE (no parameter)
else {
// run endlessly
while(1) {
run_system();
usleep(1000);
}
}
}
......@@ -2,33 +2,24 @@
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="model" nsURI="http://www.fortiss.org/af3/platform/raspberry"
nsPrefix="org-fortiss-af3-platform-raspberry">
<eClassifiers xsi:type="ecore:EClass" name="RaspberryPi" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericExecutionUnit"/>
<eClassifiers xsi:type="ecore:EClass" name="CanBus" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericTransmissionUnit"/>
<eClassifiers xsi:type="ecore:EClass" name="CanConnector" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericTransceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="SensorConnector" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ActuatorConnector" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericTransmitter"/>
<eClassifiers xsi:type="ecore:EClass" name="SensorGamepad" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="SensorIMU" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="SensorLaserRangeFinder" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="SensorLaserScanner" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ActuatorPWM" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericTransmitter"/>
<eClassifiers xsi:type="ecore:EClass" name="GPI" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="GPO" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericTransmitter"/>
<eClassifiers xsi:type="ecore:EClass" name="WheelEncoder" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="RaspberryPi" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//ExecutionUnit"/>
<eClassifiers xsi:type="ecore:EClass" name="CanBus" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//TransmissionUnit"/>
<eClassifiers xsi:type="ecore:EClass" name="CanConnector" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Transceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ActuatorPWM" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Transmitter"/>
<eSubpackages name="gamepad" nsURI="http://www.fortiss.org/af3/platform/raspberry/gamepad"
nsPrefix="org-fortiss-af3-platform-raspberry-gamepad">
<eClassifiers xsi:type="ecore:EClass" name="Button1" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Button2" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Button3" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Button4" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ButtonL1" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ButtonR2" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ButtonL2" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ButtonR1" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Left_StickX_Position" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Left_StickY_Position" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Right_StickX_Position" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Right_StickY_Position" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//generic/GenericReceiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Button1" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Button2" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Button3" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Button4" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ButtonL1" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ButtonR2" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ButtonL2" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="ButtonR1" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Left_StickX_Position" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Left_StickY_Position" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Right_StickX_Position" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
<eClassifiers xsi:type="ecore:EClass" name="Right_StickY_Position" eSuperTypes="platform:/resource/org.fortiss.af3.platform/model/platform.ecore#//Receiver"/>
</eSubpackages>
<eSubpackages name="annotation" nsURI="http://www.fortiss.org/af3/platform/raspberry/annotation"
nsPrefix="org-fortiss-af3-platform-raspberry-annotation">
......
......@@ -10,16 +10,7 @@
<genClasses ecoreClass="raspberry.ecore#//RaspberryPi"/>
<genClasses ecoreClass="raspberry.ecore#//CanBus"/>
<genClasses ecoreClass="raspberry.ecore#//CanConnector"/>
<genClasses ecoreClass="raspberry.ecore#//SensorConnector"/>
<genClasses ecoreClass="raspberry.ecore#//ActuatorConnector"/>
<genClasses ecoreClass="raspberry.ecore#//SensorGamepad"/>
<genClasses ecoreClass="raspberry.ecore#//SensorIMU"/>
<genClasses ecoreClass="raspberry.ecore#//SensorLaserRangeFinder"/>
<genClasses ecoreClass="raspberry.ecore#//SensorLaserScanner"/>
<genClasses ecoreClass="raspberry.ecore#//ActuatorPWM"/>
<genClasses ecoreClass="raspberry.ecore#//GPI"/>
<genClasses ecoreClass="raspberry.ecore#//GPO"/>
<genClasses ecoreClass="raspberry.ecore#//WheelEncoder"/>
<nestedGenPackages prefix="Gamepad" basePackage="org.fortiss.af3.platform.raspberry.model"
disposableProviderFactory="true" ecorePackage="raspberry.ecore#//gamepad">
<genClasses ecoreClass="raspberry.ecore#//gamepad/Button1"/>
......
......@@ -38,7 +38,7 @@
<modelElementCompositor
compositor="org.fortiss.af3.platform.raspberry.compose.RaspberryPiCompositor">
<modelElementClass
modelElementClass="org.fortiss.af3.platform.raspberry.model.RaspberryPi">
modelElementClass="org.fortiss.af3.platform.model.IPlatformArchitectureElement">
</modelElementClass>
</modelElementCompositor>
<modelElementCompositor
......@@ -105,84 +105,6 @@
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.IMUSensorTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.SensorIMU">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.IMUSensorExecutable">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.LaserRangeFinderSensorTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.SensorLaserRangeFinder">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.LaserRangeFinderSensorExecutable">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.LaserScannerSensorTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.SensorLaserScanner">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.LaserScannerSensorExecutable">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.PWMActuatorTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.ActuatorPWM">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.PWMActuatorExecutable">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.CanBusTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.CanBus">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.CanBusExecutable">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.GamepadSensorTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.SensorGamepad">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.GamepadSensorExecutable">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.CanBusTransmissionCatalogTransformation">
<source>
......@@ -209,32 +131,6 @@
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.GPITransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.GPI">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.GPIExecutable">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.GPOTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.GPO">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.GPOExecutable">
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.gamepad.Button1Transformation">
<source>
......@@ -391,34 +287,9 @@
</objectClass>
</target>
</transformationProvider>
<transformationProvider
transformationProvider="org.fortiss.af3.platform.raspberry.generator.transform.WheelEncoderTransformation">
<source>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.model.WheelEncoder">
</objectClass>
</source>
<target>
<objectClass
objectClass="org.fortiss.af3.platform.raspberry.generator.executable.WheelEncoderExecutable">
</objectClass>
</target>
</transformationProvider>
</extension>
<extension
point="org.fortiss.tooling.base.annotation">
<annotation
binding="org.fortiss.af3.platform.annotation.valueprovider.GPIOPinNumberValueProvider">
<modelElementClass
modelElementClass="org.fortiss.af3.platform.raspberry.model.GPI">
</modelElementClass>
</annotation>
<annotation
binding="org.fortiss.af3.platform.annotation.valueprovider.GPIOPinNumberValueProvider">
<modelElementClass
modelElementClass="org.fortiss.af3.platform.raspberry.model.GPO">
</modelElementClass>
</annotation>
<annotation
binding="org.fortiss.af3.platform.raspberry.annotation.PWMChannelNumberValueProvider">
<modelElementClass
......@@ -426,8 +297,4 @@
</modelElementClass>
</annotation>
</extension>
</plugin>
......@@ -22,17 +22,9 @@ import static org.conqat.lib.commons.reflect.ReflectionUtils.isInstanceOfAny;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.af3.platform.compose.PlatformArchitectureElementCompositorBase;
import org.fortiss.af3.platform.model.IPlatformArchitectureElement;
import org.fortiss.af3.platform.raspberry.model.ActuatorConnector;
import org.fortiss.af3.platform.raspberry.model.ActuatorPWM;
import org.fortiss.af3.platform.raspberry.model.CanConnector;
import org.fortiss.af3.platform.raspberry.model.GPI;
import org.fortiss.af3.platform.raspberry.model.GPO;
import org.fortiss.af3.platform.raspberry.model.RaspberryPi;
import org.fortiss.af3.platform.raspberry.model.SensorConnector;
import org.fortiss.af3.platform.raspberry.model.SensorIMU;
import org.fortiss.af3.platform.raspberry.model.SensorLaserRangeFinder;
import org.fortiss.af3.platform.raspberry.model.SensorLaserScanner;
import org.fortiss.af3.platform.raspberry.model.WheelEncoder;
import org.fortiss.af3.platform.raspberry.model.gamepad.Button1;
import org.fortiss.af3.platform.raspberry.model.gamepad.Button2;
import org.fortiss.af3.platform.raspberry.model.gamepad.Button3;
......@@ -45,6 +37,8 @@ import org.fortiss.af3.platform.raspberry.model.gamepad.Left_StickX_Position;
import org.fortiss.af3.platform.raspberry.model.gamepad.Left_StickY_Position;
import org.fortiss.af3.platform.raspberry.model.gamepad.Right_StickX_Position;
import org.fortiss.af3.platform.raspberry.model.gamepad.Right_StickY_Position;
import org.fortiss.tooling.kernel.extension.data.IElementCompositionContext;
import org.fortiss.tooling.kernel.extension.data.Prototype;
/**
*
......@@ -56,19 +50,29 @@ import org.fortiss.af3.platform.raspberry.model.gamepad.Right_StickY_Position;
public class RaspberryPiCompositor extends
PlatformArchitectureElementCompositorBase<IPlatformArchitectureElement> {
/** {@inheritDoc} */
@Override
public boolean canComposePrototype(Prototype prototype) {
return super.canComposePrototype(prototype);
}
/** {@inheritDoc} */
@Override
public boolean canCompose(IPlatformArchitectureElement container, EObject contained,
IElementCompositionContext context) {
return super.canCompose(container, contained, context);
}
/** {@inheritDoc} */
@Override
protected boolean isCorrectElementClass(IPlatformArchitectureElement container,
EObject contained) {
final boolean instanceOfAny2 =
isInstanceOfAny(contained, CanConnector.class, SensorConnector.class,
ActuatorConnector.class, ActuatorPWM.class, SensorLaserRangeFinder.class,
SensorLaserScanner.class, SensorIMU.class, GPI.class, GPO.class,
Button1.class, Button2.class, Button3.class, Button4.class, ButtonL1.class,
isInstanceOfAny(contained, CanConnector.class, ActuatorPWM.class, Button1.class,
Button2.class, Button3.class, Button4.class, ButtonL1.class,
ButtonL2.class, ButtonR1.class, ButtonR2.class, Left_StickX_Position.class,
Left_StickY_Position.class, Right_StickX_Position.class,
Right_StickY_Position.class, WheelEncoder.class);
Right_StickY_Position.class);
if(container != null) {
final boolean instanceOfAny = isInstanceOfAny(container, RaspberryPi.class);
return instanceOfAny && instanceOfAny2;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment