Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 2104 → Rev 2105

/RaspberryPi/PiMoteSwitch/Makefile
0,0 → 1,23
OBJ = main.o calibration.o file.o frame.o gamepad.o gpio.o uart.o
DEPS = main.h calibration.h file.h frame.h gamepad.h gpio.h uart.h types.h
LIBS = -lncurses
CFLAGS = -std=c99 -Wall
CC = gcc
EXTENSION = .c
 
#define a rule that applies to all files ending in the .o suffix, which says that the .o file depends upon the .c version of the file and all the .h files included in the DEPS macro. Compile each object file
%.o: %$(EXTENSION) $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
 
#Combine them into the output file
#Set your desired exe output file name here
all: PiMote clean
 
PiMote: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
 
#Cleanup
.PHONY: clean
 
clean:
rm -f *.o *~ core *~
/RaspberryPi/PiMoteSwitch/PiMote
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes:
Added: svn:mime-type
+application/octet-stream
\ No newline at end of property
/RaspberryPi/PiMoteSwitch/calibration.c
0,0 → 1,122
#include "main.h"
#include "calibration.h"
#include "frame.h"
#include "file.h"
#include "gpio.h"
 
 
void calibrate(void){
frame_print("Please calibrate. Begin with Layout, assign buttons and axis:\n", 1, 0);
int i = 0;
while(i < NUMBER_OF_AXES_USED){
frame_print("\n (Axis) ", 0, 0);
frame_print(LablesOfAxis[i], 0, 0);
int u = -1;
int axisDefault = 0;
while(1){if(!value_sum(1)){break;}} //Wait until all buttons released
while(u == -1){
for (int p = 0; p < NumberOfAxesAvailiable; p++)
{
read_gamepad_data();
if(u == -1 && Axis[p] != axisDefault){u = p;}
}
}
GamepadLayout[i] = u;
i++;
frame_print(" check", 0, 0);
buzzer_short(1);
}
while(i < (NUMBER_OF_BUTTONS_USED + NUMBER_OF_AXES_USED)){
frame_print("\n (Button) ", 0, 0);
frame_print(LablesOfButtons[i - NUMBER_OF_AXES_USED], 0, 0);
int u = -1;
while(1){if(!value_sum(0)){break;}}
while(u == -1){
u = check_if_button_pushed();
}
GamepadLayout[i] = u;
i++;
frame_print(" check", 0, 0);
buzzer_short(1);
}
while(1){if(!value_sum(0)){break;}}
frame_print("Press Button 1 to continue...", 1, 1);
while(!check_if_button_pushed() == 0){};
buzzer_short(1);
frame_print("To calibrate the maximum value of the axis, \n pull and hold the assigned CamNick axis at the maximum \n and store the value by pressing any button.\n\n Maximum Value of Nick axis: ", 1, 0);
while(1){if(!value_sum(0)){break;}}
while(1){
read_gamepad_data();
if(value_sum(0))
{
MaxAxisValue = Axis[GamepadLayout[0]];
break;
}
}
buzzer_short(1);
fflush(stdout);
frame_print(" check", 0, 0);
write_file();
frame_print("", 1, 0);
}
 
//>> Ensuring that no button is being pushed and no axis moved
//------------------------------------------------------------------------------------------------------
int value_sum(int q){
read_gamepad_data();
int tmp = 0;
if(q)
{
for (int i = 0; i < NumberOfAxesAvailiable; ++i)
{
tmp += Axis[i];
}
}
if(!q)
{
for (int i = 0; i < NumberOfButtonsAvailiable; ++i)
{
tmp += Button[i];
}
}
return(tmp);
}
 
//>> Initializing calibration of Gamepad
//------------------------------------------------------------------------------------------------------
void calibration_init(){
if(FileExists){
frame_stay("Found Profile!\n\n Pad connected:\n %s\n %d Axis\n %d Buttons", GamepadConnected, NUMBER_OF_AXES_USED, NUMBER_OF_BUTTONS_USED);
frame_print("Press Button 1 for recalibration .", 1, 1);
int i = 0;
buzzer_short(1);
while(i < 1000){
if(check_if_button_pushed() == 0){
i = 1;
buzzer_short(3);
break;
}
usleep(5000);
if(!(i%100)){
frame_print(" .", 0, 1);
buzzer_short(1);
}
i++;
}
if(i == 1)
{
calibrate();
frame_print("START", 1, 1);
}else
{
while(1){if(!value_sum(0)){break;}
}
frame_print("START", 1, 1);
}
}else{
buzzer_short(5);
frame_stay("No Profile found!\n\n Pad connected:\n %s\n %d Axis\n %d Buttons", GamepadConnected, NUMBER_OF_AXES_USED, NUMBER_OF_BUTTONS_USED);
calibrate();
frame_print("START", 1, 1);
}
}
/RaspberryPi/PiMoteSwitch/calibration.h
0,0 → 1,7
#ifndef _CALIBRATION_H
#define _CALIBRATION_H
 
extern void calibration_init();
extern int value_sum(int q);
 
#endif //_CALIBRATION_H
/RaspberryPi/PiMoteSwitch/configf.txt
0,0 → 1,26
#Gamepad Configuration File
 
#layout
 
camNick: 3
camRoll: 2
camGier: 0
camZoom: 1
extNick: 3
extRoll: 2
extGier: 0
extGas: 1
startRecord: 0
stopRecord: 3
trigger: 7
programOne: 4
programTwo: 5
motorStart: 9
motorStop: 8
sensPlus: 7
sensMinus: 6
activate: 2
 
#Max value of axis
 
MaxAxisValue: 32767
/RaspberryPi/PiMoteSwitch/file.c
0,0 → 1,81
#include "main.h"
#include "file.h"
 
int MaxAxisValue;
u8 GamepadLayout[30];
u8 FileExists;
 
const char* LablesOfAxis[NUMBER_OF_AXES_USED] =
{
"camNick:", // GamepadLayout Position [0] Nick-Axis of Camera-Gimbal
"camRoll:", // GamepadLayout Position [1] Roll-Axis of Camera-Gimbal
"camGier:", // GamepadLayout Positoin [2] Yaw (Gier)-Axis of MikroKopter (for Camera Control)
"camZoom:", // GamepadLayout Position [3] Camera-Zoom
"extNick:", // GamepadLayout Position [4] Nick-Axis of MikroKopter
"extRoll:", // GamepadLayout Position [5] Roll-Axis of MikroKopter
"extGier:", // GamepadLayout Position [6] Yaw(Gier)-Axis of MikroKopter
"extGas:" // GamepadLayout Position [7] Gas of MikroKopter
};
const char* LablesOfButtons[NUMBER_OF_BUTTONS_USED] =
{
"startRecord:", // GamepadLayout Position [8] Start Recording Camcorder
"stopRecord:", // GamepadLayout Position [9] Stop Recording Camcorder
"trigger:", // GamepadLayout Position [10] Trigger a Camera
"programOne:", // GamepadLayout Position [11] Program 1: Remote Control Camera
"programTwo:", // GamepadLayout Position [12] Program 2: Remote Control MikroKopter
"motorStart:", // GamepadLayout Position [13] Start Motors
"motorStop:", // GamepadLayout Position [14] Stop Motors
"sensPlus:", // GamepadLayout Position [15] Increase sensitivity of the controls
"sensMinus:", // GamepadLayout Position [16] Decrease sensitivity of the controls
"activate:" // GamepadLayout Position [17] Activate the controls
};
 
//>> Reading or creating configuration file
//------------------------------------------------------------------------------------------------------
int file_init(){
char line[50];
FILE *fp = fopen("/home/pi/PiMoteSwitch/configf.txt","r");
if(fp)
{
while( fgets(line,50,fp) ) {
for (int i = 0; i < NUMBER_OF_AXES_USED; i++)
{
if(strstr(line, LablesOfAxis[i]) != NULL){
char *token = strtok(line, " ");
token = strtok(NULL, " ");
GamepadLayout[i] = token[0] - '0';
}
}
for (int i = 0; i < NUMBER_OF_BUTTONS_USED; i++)
{
if(strstr(line, LablesOfButtons[i]) != NULL){
char *token = strtok(line, " ");
token = strtok(NULL, " ");
GamepadLayout[i + NUMBER_OF_AXES_USED] = token[0] - '0';
}
}
if(strstr(line, "MaxAxisValue") != NULL){
char *token = strtok(line, " ");
token = strtok(NULL, " ");
MaxAxisValue = strtol(token, (char **)NULL, 10);
}
memset(line, 0, 49);
}
fclose(fp);
return(1);
}
return(0);
}
 
//>> Write new configuration to file
//------------------------------------------------------------------------------------------------------
void write_file(void){
FILE *fp = fopen("/home/pi/PiMoteSwitch/configf.txt","w+");
fprintf(fp, "#Gamepad Configuration File\r\n\n");
fprintf(fp, "#layout\r\n\n");
for (int i = 0; i < NUMBER_OF_AXES_USED; i++){fprintf(fp, "%s %d\r\n", LablesOfAxis[i], GamepadLayout[i]);}
for (int i = 0; i < NUMBER_OF_BUTTONS_USED; i++){fprintf(fp, "%s %d\r\n", LablesOfButtons[i], GamepadLayout[i + NUMBER_OF_AXES_USED]);}
fprintf(fp, "\n#Max value of axis\r\n\nMaxAxisValue: ");
fprintf(fp, "%d", MaxAxisValue);
fclose(fp);
}
/RaspberryPi/PiMoteSwitch/file.h
0,0 → 1,15
#ifndef _FILE_H
#define _FILE_H
 
#define NUMBER_OF_AXES_USED 8
#define NUMBER_OF_BUTTONS_USED 10
 
extern u8 GamepadLayout[30];
extern int MaxAxisValue;
extern const char* LablesOfAxis[NUMBER_OF_AXES_USED];
extern const char* LablesOfButtons[NUMBER_OF_BUTTONS_USED];
extern int file_init();
extern void write_file(void);
extern u8 FileExists;
 
#endif //_FILE_H
/RaspberryPi/PiMoteSwitch/frame.c
0,0 → 1,61
#include <curses.h>
#include "main.h"
#include "frame.h"
 
int main_y,main_x,sub_y,sub_x;
WINDOW *main_window;
WINDOW *stay_window;
WINDOW *sub_window;
 
void frame_print(const char *msg, int clear, int center){
if(clear){
wclear(sub_window);
if(center){
mvwprintw(sub_window,(main_y/4)-1,2,msg);
}else{
mvwprintw(sub_window,2,2,msg);
}
}else{
wprintw(sub_window, msg);
}
frame_refresh();
}
 
void frame_values(char *msg, int row, int line){
mvwprintw(sub_window,line,row,msg);
frame_refresh();
}
 
void frame_stay(char *msg, char* pad_frame, int axis_frame, int buttons_frame){
mvwprintw(stay_window,2,2,msg,pad_frame,axis_frame,buttons_frame);
wrefresh(stay_window);
frame_refresh();
}
 
void frame_refresh(){
box(sub_window,0,0);
wrefresh(sub_window);
}
 
void frame_init(){
initscr();
noecho();
curs_set(FALSE);
getmaxyx(stdscr,main_y,main_x);
sub_y = main_y/3;
sub_x = main_x/4;
main_window = newwin(main_y, main_x, 0, 0);
stay_window = newwin(main_y/4, main_x/8, main_y/16, main_x/2-main_x/16);
sub_window = newwin(main_y,main_x/2,sub_y,sub_x);
start_color();
init_pair(1, COLOR_RED, COLOR_WHITE);
wbkgd(main_window,COLOR_PAIR(1));
wbkgd(stay_window,COLOR_PAIR(1));
wbkgd(sub_window,COLOR_PAIR(1));
box(main_window,0,0);
box(sub_window,0,0);
mvwprintw(main_window,0,(main_x/2)-strlen(TITLE)/2,TITLE);
mvwprintw(main_window,main_y-1,(main_x/2)-strlen(COPY)/2,COPY);
wrefresh(main_window);
wrefresh(sub_window);
}
/RaspberryPi/PiMoteSwitch/frame.h
0,0 → 1,13
#ifndef _FRAME_H
#define _FRAME_H
 
#define TITLE "| MIKROKOPTER Camera Control |"
#define COPY "| (C)HiSystems |"
 
extern void frame_init();
extern void frame_print(const char *msg, int clear, int center);
extern void frame_values(char *msg, int row, int line);
extern void frame_stay(char *msg, char *pad_frame, int axis_frame, int buttons_frame);
extern void frame_refresh();
 
#endif //_FRAME_H
/RaspberryPi/PiMoteSwitch/gamepad.c
0,0 → 1,194
#include <linux/joystick.h>
#include "main.h"
#include "gamepad.h"
#include "frame.h"
 
volatile int *Axis = NULL;
volatile char *Button = NULL;
volatile float Sensitivity = 1.0;
int NumberOfAxesAvailiable;
int NumberOfButtonsAvailiable;
char GamepadConnected[30];
u8 IsActive = 0;
u8 ProgramSelect = 0;
 
int recordingButtonTemp = 125;
int padFd;
struct js_event js;
 
struct str_ExternControl externControl; //struct for controlling MikroKopter
struct str_ExternControl externControlGier; //struct for controlling Yaw (Gier) only
 
u8 gamepad_values_convert_for_cam(int tmpvalue){
u8 tmp;
double tmpdouble = tmpvalue;
tmp = ((((tmpdouble/(MaxAxisValue))-1)*125)-131)*-1;
if(tmp == 0){tmp = 255;}
return(tmp);
}
 
//>> Converting gamepad values to values readable by a MikroKopter
//------------------------------------------------------------------------------------------------------
u8 gamepad_values_convert_for_kopter(int tmpvalue){
u8 tmp;
double tmpdouble = tmpvalue;
tmp = ((((tmpdouble/(MaxAxisValue*Sensitivity))-1)*125)-131)*-1;
/*Increase values exponentially
if(tmpvalue < 0){
tmpdouble = tmpvalue*-1;
}else{tmpdouble = tmpvalue;}
tmp = ((30497*tmpdouble*tmpdouble*tmpdouble)/11187226521286200+(10667778*tmpdouble*tmpdouble)/532725072442200+(2592340312*tmpdouble)/11187226521286);
if(tmpvalue < 0){
tmp = (tmp*-1)+255;
}
*/
return(tmp);
}
 
u8 gamepad_values_convert_for_kopter_gas(int tmpvalue){
u8 tmp;
double tmpdouble = tmpvalue;
tmp = ((((tmpdouble/(MaxAxisValue))-1)*125)-131)*-1;
return(tmp);
}
 
int check_if_button_pushed(){
read_gamepad_data();
int buttonpushed = -1;
for (int i = 0; i < NumberOfButtonsAvailiable; i++)
{
if(Button[i] != 0){
buttonpushed = i;
}
}
return(buttonpushed);
}
 
//>> Check if controls are active
//------------------------------------------------------------------------------------------------------
void check_if_controls_active(){
if(check_if_button_pushed() == GamepadLayout[17] && IsActive == 0){
IsActive = 1;
while(1){if(!value_sum(0)){break;};}
}else if(check_if_button_pushed() == GamepadLayout[17] && IsActive == 1){
IsActive = 0;
while(1){if(!value_sum(0)){break;};}
}
}
//>> Check if program has been switched
//------------------------------------------------------------------------------------------------------
void check_program_switch(){
if(check_if_button_pushed() == GamepadLayout[11]){
frame_print("", 1, 0);
ProgramSelect = 1;
}else if(check_if_button_pushed() == GamepadLayout[12]){
frame_print("", 1, 0);
ProgramSelect = 2;
}
}
 
//>> Check if sensitivity of the controls has been adjusted
//------------------------------------------------------------------------------------------------------
void get_sensitivity(){
if(check_if_button_pushed() == GamepadLayout[15] && Sensitivity <= 2.0){
if(Sensitivity < 2){buzzer_short(1);}
Sensitivity += 0.2;
while(1){if(!value_sum(0)){break;};}
}else if(check_if_button_pushed() == GamepadLayout[16] && Sensitivity > 1.0){
if(Sensitivity > 1){buzzer_short(1);}
Sensitivity -= 0.2;
while(1){if(!value_sum(0)){break;};}
}
}
 
//>> Preparing Data for remotely controlling a Camera-Gimbal
//------------------------------------------------------------------------------------------------------
void create_data_camera(serial_data_struct* pdata_package_camera_control, serial_data_struct* pdata_package_camera_control_gier){
pdata_package_camera_control->data[0] = gamepad_values_convert_for_cam(Axis[GamepadLayout[0]]); //Nick
pdata_package_camera_control->data[1] = gamepad_values_convert_for_cam(Axis[GamepadLayout[1]]); //Roll
pdata_package_camera_control->data[2] = gamepad_values_convert_for_cam(Axis[GamepadLayout[3]]); //
if(Button[GamepadLayout[8]] > 0){recordingButtonTemp = 131;}else if(Button[GamepadLayout[9]] > 0){recordingButtonTemp = 125;}
pdata_package_camera_control->data[3] = recordingButtonTemp;
if(Button[GamepadLayout[10]]){pdata_package_camera_control->data[4] = 131;}else{pdata_package_camera_control->data[4] = 125;}
int i = 5;
while(i < 12){
pdata_package_camera_control->data[i] = 0;
i++;
}
//Add Yaw (Gier)-funcionality
externControlGier.Gier = gamepad_values_convert_for_kopter(Axis[GamepadLayout[2]]);
externControlGier.Config = EC_VALID | EC_GAS_ADD;
externControlGier.Frame = 'z'; //character for echo
u8 *tmpData = (unsigned char *) &externControlGier;
for (int i = 0; i < pdata_package_camera_control_gier->cmdLength; ++i)
{
pdata_package_camera_control_gier->data[i] = tmpData[i];
}
 
//Print out values on screen
char tmpbuffer[255];
sprintf(tmpbuffer, "Nick: %d \n Roll: %d \n Gier: %d \n Zoom: %d \n Aufnahme: %d \n Auslösung: %d", pdata_package_camera_control->data[0], pdata_package_camera_control->data[1], pdata_package_camera_control_gier->data[2], pdata_package_camera_control->data[2], pdata_package_camera_control->data[3], pdata_package_camera_control->data[4]);
frame_values("VALUES", 5, 2);
frame_values(tmpbuffer, 5, 4);
}
 
//>> Preparing Data for remotely controlling a MikroKopter
//------------------------------------------------------------------------------------------------------
void create_data_kopter(serial_data_struct* pdata_package_kopter_control){
if(check_if_button_pushed() == GamepadLayout[13]){ //Motor start
externControl.Nick = 131;
externControl.Roll = 131;
externControl.Gier = 131;
externControl.Gas = 131;
}else if(check_if_button_pushed() == GamepadLayout[14]){ //Motor stop
externControl.Nick = 131;
externControl.Roll = 125;
externControl.Gier = 125;
externControl.Gas = 131;
}else{
externControl.Nick = gamepad_values_convert_for_kopter(Axis[GamepadLayout[4]]);
externControl.Roll = gamepad_values_convert_for_kopter(Axis[GamepadLayout[5]]);
externControl.Gier = gamepad_values_convert_for_kopter(Axis[GamepadLayout[6]]);
externControl.Gas = gamepad_values_convert_for_kopter_gas(Axis[GamepadLayout[7]]);
}
externControl.Config = EC_VALID | EC_GAS_ADD | EC_IGNORE_RC;
externControl.Frame = 'z'; //character for echo
u8 *tmpData = (unsigned char *) &externControl;
for (int i = 0; i < pdata_package_kopter_control->cmdLength; ++i)
{
pdata_package_kopter_control->data[i] = tmpData[i];
}
//Print out values on screen
char tmpbuffer[255];
sprintf(tmpbuffer, "Nick: %d \n Roll: %d \n Gier: %d \n Gas : %d \n", pdata_package_kopter_control->data[0], pdata_package_kopter_control->data[1], pdata_package_kopter_control->data[2], pdata_package_kopter_control->data[3]);
frame_values("VALUES", 5, 2);
frame_values(tmpbuffer, 5, 4);
}
 
void read_gamepad_data(){
read(padFd, &js, sizeof(struct js_event));
switch (js.type & ~JS_EVENT_INIT)
{
case JS_EVENT_AXIS:
Axis [ js.number ] = js.value;
break;
case JS_EVENT_BUTTON:
Button [ js.number ] = js.value;
break;
}
}
 
//>> Initialize Gamepad
//------------------------------------------------------------------------------------------------------
void gamepad_init(){
if((padFd = open(GAME_PAD, O_RDONLY)) == -1)
{
printf( "Couldn't open gamepad\n" );
}
ioctl( padFd, JSIOCGAXES, &NumberOfAxesAvailiable );
ioctl( padFd, JSIOCGBUTTONS, &NumberOfButtonsAvailiable );
ioctl( padFd, JSIOCGNAME(80), &GamepadConnected );
Axis = (int *) calloc( NumberOfAxesAvailiable, sizeof( int ) );
Button = (char *) calloc( NumberOfButtonsAvailiable, sizeof( char ) );
fcntl( padFd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */
}
/RaspberryPi/PiMoteSwitch/gamepad.h
0,0 → 1,29
#ifndef _GAMEPAD_H
#define _GAMEPAD_H
 
#define GAME_PAD "/dev/input/js0"
 
//Defines for external control
#define EC_VALID 0x01 // only valid if this is 1
#define EC_GAS_ADD 0x02 // if 1 -> use the GAS Value not as MAX
#define EC_IGNORE_RC 0x80 // if 1 -> for Flying without RC-Control
 
extern void gamepad_init();
extern void read_gamepad_data();
extern void create_data_camera(serial_data_struct* pdata_package_camera_control, serial_data_struct* pdata_package_camera_control_gier);
extern void create_data_kopter(serial_data_struct* pdata_package_kopter_control);
extern int check_if_button_pushed();
extern void check_if_controls_active();
extern void check_program_switch();
extern void get_sensitivity();
 
extern volatile int *Axis;
extern volatile char *Button;
extern volatile float Sensitivity;
extern int NumberOfAxesAvailiable;
extern int NumberOfButtonsAvailiable;
extern char GamepadConnected[30];
extern u8 IsActive;
extern u8 ProgramSelect;
 
#endif //_GAMEPAD_H
/RaspberryPi/PiMoteSwitch/gpio.c
0,0 → 1,59
#include "main.h"
#include "gpio.h"
 
// IO Acces
struct bcm2835_peripheral {
unsigned long addr_p;
int mem_fd;
void *map;
volatile unsigned int *addr;
};
struct bcm2835_peripheral gpio = {GPIO_BASE};
 
 
int map_peripheral(struct bcm2835_peripheral *p)
{
// Open /dev/mem
if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("Failed to open /dev/mem, try checking permissions.\n");
return -1;
}
p->map = mmap(
NULL,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED,
p->mem_fd, // File descriptor to physical memory virtual file '/dev/mem'
p->addr_p // address in physical map that we want this memory block to expose
);
if (p->map == MAP_FAILED) {
perror("mmap");
return -1;
}
p->addr = (volatile unsigned int *)p->map;
return 0;
}
void unmap_peripheral(struct bcm2835_peripheral *p) {
munmap(p->map, BLOCK_SIZE);
close(p->mem_fd);
}
 
void buzzer_short(int seq){
INP_GPIO(4);
OUT_GPIO(4);
for (int i = 0; i < seq; ++i)
{
if(seq > 1){usleep(50000);}
GPIO_SET = 1 << 4;
usleep(50000);
GPIO_CLR = 1 << 4;
}
}
 
void gpio_init(){
if(map_peripheral(&gpio) == -1)
{
printf("Failed to map the physical GPIO registers into the virtual memory space.\n");
}
}
/RaspberryPi/PiMoteSwitch/gpio.h
0,0 → 1,22
#ifndef _GPIO_H
#define _GPIO_H
 
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller
#define BLOCK_SIZE (4*1024)
 
extern struct bcm2835_peripheral gpio;
 
// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x)
#define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0
#define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g))
 
// Exposes the physical address defined in the passed structure using mmap on /dev/mem
extern void gpio_init();
extern void buzzer_short(int seq);
 
#endif //_GPIO_H
/RaspberryPi/PiMoteSwitch/main.c
0,0 → 1,41
#include "main.h"
 
 
u8 FileExists;
serial_data_struct data_package_camera_control; //Data Package for controlling the Camera
serial_data_struct data_package_camera_control_gier; //Data Package for controlling the Gier-Axis of the MikroKopter
serial_data_struct data_package_kopter_control; //Data Package for controlling the MikroKopter
 
int main(void){
frame_init();
FileExists = file_init();
gamepad_init();
gpio_init();
calibration_init();
uart_init();
buzzer_short(3);
for (;;)
{
check_program_switch();
if(ProgramSelect == 1){
read_gamepad_data();
create_data_camera(&data_package_camera_control, &data_package_camera_control_gier);
create_serial_frame(1, 'y', 12, &data_package_camera_control); //address, Command ID, Serial Command Length, data
create_serial_frame(1, 'b', sizeof(struct str_ExternControl), &data_package_camera_control_gier); //Gier
transmit_data(&data_package_camera_control);
transmit_data(&data_package_camera_control_gier);
}
if(ProgramSelect == 2){
check_if_controls_active();
if(IsActive){
get_sensitivity();
read_gamepad_data();
create_data_kopter(&data_package_kopter_control);
create_serial_frame(1, 'b', sizeof(struct str_ExternControl), &data_package_kopter_control);
transmit_data(&data_package_kopter_control);
}
}
usleep(5000); //200hz
}
}
/RaspberryPi/PiMoteSwitch/main.h
0,0 → 1,26
#ifndef _MAIN_H
#define _MAIN_H
 
#include <curses.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "frame.h"
#include "types.h"
#include "file.h"
#include "uart.h"
#include "gamepad.h"
#include "calibration.h"
#include "gpio.h"
 
extern void usleep(int);
 
#endif //_MAIN_H
/RaspberryPi/PiMoteSwitch/types.h
0,0 → 1,40
#ifndef _TYPES_H
#define _TYPES_H
 
typedef unsigned char u8;
typedef unsigned short u16;
 
typedef struct
{
u8 address;
u8 cmdID;
u8 data[1024];
u8 txrxdata[1024];
u8 collecting;
u8 position_package;
u16 cmdLength;
 
}__attribute__((packed)) serial_data_struct;
 
 
typedef struct
{
u8 c;
u8 data[1024];
u16 position_rx;
u16 count;
}__attribute__((packed)) data_rx_st;
 
 
struct str_ExternControl
{
signed char Nick;
signed char Roll;
signed char Gier;
signed char Gas;
unsigned char Frame; // will return a confirm frame with this value
unsigned char Config;
unsigned char free;
};
 
#endif //_TYPES_H
/RaspberryPi/PiMoteSwitch/uart.c
0,0 → 1,91
#include "main.h"
#include "uart.h"
 
int uart0_filestream = -1;
int usb_stream = -1;
int data_length; //length of transmitting data string
 
 
//>> Adding checksum and transmitting data
//------------------------------------------------------------------------------------------------------
void transmit_data(serial_data_struct* pdata_package){
if (uart0_filestream != -1)
{
int count = write(uart0_filestream, pdata_package->txrxdata, pdata_package->cmdLength);
if (count < 0)
{
printf("UART TX error\n");
}
}
}
 
//>> Adding address and command ID, encoding and checksum
//------------------------------------------------------------------------------------------------------
void create_serial_frame(u8 address, u8 cmdID, u16 cmdLength, serial_data_struct* pdata_package){
pdata_package->txrxdata[0] = '#';
pdata_package->txrxdata[1] = address + 'a';
pdata_package->txrxdata[2] = cmdID;
 
//encode data
u8 a,b,c;
int counter = 0;
int u = 3;
while(cmdLength){
if(cmdLength)
{
cmdLength--;
a = pdata_package->data[counter++];
}else{a = 0; counter++;};
if(cmdLength)
{
cmdLength--;
b = pdata_package->data[counter++];
}else{b = 0; counter++;};
if(cmdLength)
{
cmdLength--;
c = pdata_package->data[counter++];
}else{c = 0; counter++;};
pdata_package->txrxdata[u++] = '=' + (a >> 2);
pdata_package->txrxdata[u++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
pdata_package->txrxdata[u++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
pdata_package->txrxdata[u++] = '=' + (c & 0x3f);
}
 
//add Checksum
u16 crc = 0;
u8 crc1, crc2;
for (int i = 0; i < u; i++)
{
crc += pdata_package->txrxdata[i];
}
crc %= 4096;
crc1 = '=' + crc / 64;
crc2 = '=' + crc % 64;
pdata_package->txrxdata[u++] = crc1;
pdata_package->txrxdata[u++] = crc2;
pdata_package->txrxdata[u++] = '\r';
 
pdata_package->cmdLength = u;
}
 
 
//>> Initializing serial interface
//------------------------------------------------------------------------------------------------------
void uart_init(){
uart0_filestream = open(SERIAL, O_RDWR | O_NOCTTY | O_NDELAY);
if (uart0_filestream == -1)
{
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
}
//UART Options
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B57600 | CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);
//UART Options
}
/RaspberryPi/PiMoteSwitch/uart.h
0,0 → 1,24
#ifndef _UART_H
#define _UART_H
 
//addresses
#define FC_address 1 //(b)
#define NC_address 2 //(c)
#define MK3MAG_address 3 //(d)
#define BL_CTRL_address 5 //(f)
 
//Command IDs
#define SERIAL_CHANNELS 'y'
#define EXTERNAL_CONTROL 'b'
 
 
#define SERIAL "/dev/ttyAMA0"
 
//struct js_event e;
 
extern void create_serial_frame(u8 address, u8 cmdID, u16 cmdLength, serial_data_struct* pdata_package);
extern void transmit_data(serial_data_struct* pdata_package);
extern void encode_data(serial_data_struct* pdata_package);
extern void uart_init();
 
#endif //_UART_H