Compare commits
5 Commits
151b5160d2
...
4a82ee605f
Author | SHA1 | Date |
---|---|---|
|
4a82ee605f | |
|
0bbf7ad5e4 | |
|
e0987aa83a | |
|
7f2e176f41 | |
|
90f27fa3d0 |
|
@ -17,21 +17,47 @@ RF_LIB=rf24
|
||||||
RF_LIB_DIR=/usr/local/lib
|
RF_LIB_DIR=/usr/local/lib
|
||||||
RF_HEADER_DIR=/usr/local/include/RF24
|
RF_HEADER_DIR=/usr/local/include/RF24
|
||||||
|
|
||||||
|
GPIO_LIB=pigpio
|
||||||
|
|
||||||
# define all programs
|
# define all programs
|
||||||
PROGRAMS = hover-controller
|
PROGRAMS = hover-controller
|
||||||
SOURCES = ${PROGRAMS:=.cpp}
|
SOURCES = ${PROGRAMS:=.cpp}
|
||||||
|
|
||||||
|
PARTS = motors servos sensors dispatcher
|
||||||
|
PARTS_O = ${PARTS:=.o}
|
||||||
|
|
||||||
|
SEN_PARTS = sensors/temperature.o
|
||||||
|
|
||||||
|
|
||||||
all: ${PROGRAMS} tests
|
all: ${PROGRAMS} tests
|
||||||
|
|
||||||
|
|
||||||
${PROGRAMS}: ${SOURCES}
|
#${PROGRAMS}: ${SOURCES}
|
||||||
$(CXX) $(CFLAGS) -I$(RF_HEADER_DIR) -L$(RF_LIB_DIR) -l$(RF_LIB) $@.cpp -o $@
|
# $(CXX) $(CFLAGS) -I$(RF_HEADER_DIR) -L$(RF_LIB_DIR) -l$(RF_LIB) $@.cpp -o $@
|
||||||
|
|
||||||
|
hover-controller: hover-controller.cpp ${PARTS_O}
|
||||||
|
$(CXX) $(CFLAGS) -I$(RF_HEADER_DIR) -L$(RF_LIB_DIR) -l$(RF_LIB) -l$(GPIO_LIB) -lrt $@.cpp \
|
||||||
|
${SEN_PARTS} ${PARTS_O} -o $@
|
||||||
|
|
||||||
|
dispatcher.o: dispatcher.cpp
|
||||||
|
$(CXX) $(CFLAGS) dispatcher.cpp -o $@ -c
|
||||||
|
|
||||||
|
servos.o: servos.cpp
|
||||||
|
$(CXX) $(CFLAGS) servos.cpp -o $@ -c
|
||||||
|
|
||||||
|
motors.o: motors.cpp
|
||||||
|
$(CXX) $(CFLAGS) motors.cpp -o $@ -c
|
||||||
|
|
||||||
|
sensors.o: sensors.cpp ${SEN_PARTS}
|
||||||
|
$(CXX) $(CFLAGS) sensors.cpp -o $@ -c
|
||||||
|
|
||||||
|
sensors/%.o: sensors/%.cpp
|
||||||
|
$(CXX) $(CFLAGS) $< -o $@ -c
|
||||||
|
|
||||||
tests:
|
tests:
|
||||||
|
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean remake
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(PROGRAMS) *.o
|
rm -rf $(PROGRAMS) $(PARTS_O) *.o sensors/*.o
|
||||||
|
remake: clean all
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef __PIN_CONFIG_H__
|
||||||
|
#define __PIN_CONFIG_H__
|
||||||
|
|
||||||
|
#define RF_CE_PIN RPI_V2_GPIO_P1_22
|
||||||
|
|
||||||
|
#define SERVO_MAX 2400 //2300
|
||||||
|
#define SERVO_MIN 500
|
||||||
|
|
||||||
|
#define LEFT_RUDDER_GPIO 23
|
||||||
|
#define LEFT_RUDDER_MAX SERVO_MAX
|
||||||
|
#define LEFT_RUDDER_MIN SERVO_MIN
|
||||||
|
|
||||||
|
#define RIGHT_RUDDER_GPIO 24
|
||||||
|
#define RIGHT_RUDDER_MAX SERVO_MAX
|
||||||
|
#define RIGHT_RUDDER_MIN SERVO_MIN
|
||||||
|
|
||||||
|
|
||||||
|
#define ESC_MAX
|
||||||
|
#define ESC_MIN
|
||||||
|
#define ESC_REVERSE
|
||||||
|
|
||||||
|
#define RIGHT_PROP_GPIO
|
||||||
|
#define LEFT_PROP_GPIO
|
||||||
|
|
||||||
|
#endif // __PIN_CONFIG_H__
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "dispatcher.h"
|
||||||
|
|
||||||
|
|
||||||
|
void process_msg(char*msg) {
|
||||||
|
|
||||||
|
char msg_cmd[3+1];
|
||||||
|
|
||||||
|
strncpy(msg_cmd, msg, 3);
|
||||||
|
msg_cmd[3]='\0';
|
||||||
|
|
||||||
|
|
||||||
|
if (strcmp(msg_cmd, "ESC") == 0) {
|
||||||
|
|
||||||
|
} else
|
||||||
|
if (strcmp(msg_cmd, "SER") == 0) {
|
||||||
|
servo_process(msg);
|
||||||
|
} else
|
||||||
|
if (strcmp(msg_cmd, "SEN") == 0) {
|
||||||
|
sensor_process(msg);
|
||||||
|
} else {
|
||||||
|
eprintf("CMD not valid");
|
||||||
|
strcpy(msg, "CMD_NOK");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef __DISPACHER_H__
|
||||||
|
#define __DISPACHER_H__
|
||||||
|
|
||||||
|
#include "../rfudp/TxTunnel.h"
|
||||||
|
#include "PinConfig.h"
|
||||||
|
#include "sensors.h"
|
||||||
|
#include "servos.h"
|
||||||
|
#include "motors.h"
|
||||||
|
|
||||||
|
void process_msg(char*msg);
|
||||||
|
|
||||||
|
#endif // __DISPACHER_H__
|
|
@ -14,6 +14,8 @@ TMRh20 2014
|
||||||
|
|
||||||
#include "../rfudp/TxTunnel.h"
|
#include "../rfudp/TxTunnel.h"
|
||||||
|
|
||||||
|
#include "dispatcher.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
//
|
//
|
||||||
// Hardware configuration
|
// Hardware configuration
|
||||||
|
@ -25,7 +27,8 @@ using namespace std;
|
||||||
// See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.
|
// See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.
|
||||||
|
|
||||||
// Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
|
// Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
|
||||||
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_32MHZ);
|
//RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_32MHZ);
|
||||||
|
RF24 radio(RF_CE_PIN, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_32MHZ);
|
||||||
|
|
||||||
|
|
||||||
// Radio pipe addresses for the 2 nodes to communicate. From "TxTunnel.h"
|
// Radio pipe addresses for the 2 nodes to communicate. From "TxTunnel.h"
|
||||||
|
@ -69,7 +72,13 @@ int main(int argc, char** argv){
|
||||||
radio.setRetries(2,10); // Optionally, increase the delay between retries & # of retries
|
radio.setRetries(2,10); // Optionally, increase the delay between retries & # of retries
|
||||||
radio.setCRCLength(RF24_CRC_8); // Use 8-bit CRC for performance
|
radio.setCRCLength(RF24_CRC_8); // Use 8-bit CRC for performance
|
||||||
|
|
||||||
|
radio.printDetails();
|
||||||
|
|
||||||
|
/* GPIO setup */
|
||||||
|
if(gpioInitialise()<0) {
|
||||||
|
perror("[pigpio] gpioInitialise error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// This opens two pipes for these two nodes to communicate
|
// This opens two pipes for these two nodes to communicate
|
||||||
// back and forth.
|
// back and forth.
|
||||||
|
@ -90,6 +99,8 @@ int main(int argc, char** argv){
|
||||||
uint8_t pipeNo;
|
uint8_t pipeNo;
|
||||||
memset(buffer, 0, sizeof(buffer));
|
memset(buffer, 0, sizeof(buffer));
|
||||||
|
|
||||||
|
printf("Starting listenning for messages\n");
|
||||||
|
|
||||||
do{
|
do{
|
||||||
// GET DATA FROM CLIENT
|
// GET DATA FROM CLIENT
|
||||||
if(radio.available(&pipeNo)){
|
if(radio.available(&pipeNo)){
|
||||||
|
@ -109,10 +120,13 @@ int main(int argc, char** argv){
|
||||||
|
|
||||||
|
|
||||||
/* START process Part */
|
/* START process Part */
|
||||||
|
process_msg(buffer);
|
||||||
|
n = strlen(buffer);
|
||||||
|
|
||||||
/* END process Part */
|
/* END process Part */
|
||||||
|
|
||||||
|
printf("ANS: \'%s\'\n", buffer);
|
||||||
|
|
||||||
// Answer the client
|
// Answer the client
|
||||||
#if ACK_MODE //ACK MODE
|
#if ACK_MODE //ACK MODE
|
||||||
radio.writeAckPayload(pipeNo, buffer, n );
|
radio.writeAckPayload(pipeNo, buffer, n );
|
||||||
|
@ -125,13 +139,17 @@ int main(int argc, char** argv){
|
||||||
radio.startListening();
|
radio.startListening();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
//clean buffer
|
||||||
|
//buffer[0]='\0';
|
||||||
|
//memset(buffer, 0, sizeof(buffer));
|
||||||
|
|
||||||
}while(loop_on);
|
}while(loop_on);
|
||||||
|
|
||||||
// Power down the antenna
|
// Power down the antenna
|
||||||
eprintf("Powering down the antenna");
|
eprintf("Powering down the antenna");
|
||||||
radio.powerDown();
|
radio.powerDown();
|
||||||
|
eprintf("Terminating PiGPIO");
|
||||||
|
gpioTerminate();
|
||||||
eprintf("\n");
|
eprintf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef __MOTORS_H__
|
||||||
|
#define __MOTORS_H__
|
||||||
|
|
||||||
|
#include "servos.h"
|
||||||
|
|
||||||
|
#endif // __MOTORS_H__
|
|
@ -0,0 +1,51 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "servos.h"
|
||||||
|
#include "sensors/temperature.h"
|
||||||
|
|
||||||
|
void sensor_gyro();
|
||||||
|
void sensor_acel();
|
||||||
|
void sensor_ulson();
|
||||||
|
void sensor_laser();
|
||||||
|
void sensor_temp(char*msg, char index) {
|
||||||
|
float temp;
|
||||||
|
if (index == 'C') {
|
||||||
|
temp = temp_cpu();
|
||||||
|
} else
|
||||||
|
if (index == 'G') {
|
||||||
|
temp = temp_gpu();
|
||||||
|
} else {
|
||||||
|
eprintf("[sensor:temp] class not valid (%c)\n", index);
|
||||||
|
strcpy(msg, "SEN_TMP_CLS_NOK");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sprintf(msg, "SEN T%c %.2f", index, temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sensor_process(char*msg) {
|
||||||
|
char sensor_type = msg[4];
|
||||||
|
char sensor_index = msg[5];
|
||||||
|
int lv;
|
||||||
|
|
||||||
|
sscanf(&msg[6], " %i ", &lv );
|
||||||
|
|
||||||
|
if (sensor_type == 'G') {
|
||||||
|
|
||||||
|
} else
|
||||||
|
if (sensor_type == 'A') {
|
||||||
|
|
||||||
|
} else
|
||||||
|
if (sensor_type == 'U') {
|
||||||
|
|
||||||
|
} else
|
||||||
|
if (sensor_type == 'L') {
|
||||||
|
|
||||||
|
} else
|
||||||
|
if (sensor_type == 'T') {
|
||||||
|
sensor_temp(msg, sensor_index);
|
||||||
|
} else {
|
||||||
|
eprintf("[sensor] type not valid (%c)\n", sensor_type);
|
||||||
|
strcpy(msg, "SEN_TYP_NOK");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef __SENSORS_H__
|
||||||
|
#define __SENSORS_H__
|
||||||
|
|
||||||
|
#ifdef __arm__
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void sensor_process(char*msg);
|
||||||
|
|
||||||
|
#endif // __SENSORS_H__
|
|
@ -0,0 +1,38 @@
|
||||||
|
#include "temperature.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
//#include <string.h>
|
||||||
|
|
||||||
|
float temp_cpu(){
|
||||||
|
float systemp;
|
||||||
|
float millideg;
|
||||||
|
FILE *thermal;
|
||||||
|
|
||||||
|
thermal = fopen("/sys/class/thermal/thermal_zone0/temp","r");
|
||||||
|
fscanf(thermal,"%f",&millideg);
|
||||||
|
fclose(thermal);
|
||||||
|
systemp = millideg / 1000;
|
||||||
|
|
||||||
|
fprintf(stderr, "[sensor:temp:cpu] CPU temperature is %f degrees C\n",systemp);
|
||||||
|
|
||||||
|
return systemp;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
float temp_gpu(){
|
||||||
|
float systemp = -127;
|
||||||
|
#ifdef __Pi__
|
||||||
|
FILE *fp;
|
||||||
|
fp = popen("/opt/vc/bin/vcgencmd measure_temp | cut -c6-9", "r");
|
||||||
|
if(fp == NULL){
|
||||||
|
perror("[sensor:temp:gpu] GPU command failed!\nAre you on a Pi?\n");
|
||||||
|
return -127;
|
||||||
|
}
|
||||||
|
fscanf(fp,"%f", &systemp);
|
||||||
|
pclose(fp);
|
||||||
|
#else
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return systemp;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef __SENSORS_TEMP_H__
|
||||||
|
#define __SENSORS_TEMP_H__
|
||||||
|
|
||||||
|
#ifdef __arm__
|
||||||
|
#if __has_include("/opt/vc/include/bcm_host.h")
|
||||||
|
#define __Pi__
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
float temp_cpu();
|
||||||
|
float temp_gpu();
|
||||||
|
|
||||||
|
#endif // __SENSORS_TEMP_H__
|
|
@ -0,0 +1,70 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "PinConfig.h"
|
||||||
|
#include "servos.h"
|
||||||
|
|
||||||
|
|
||||||
|
void servo_mv_r(unsigned int pin, int lv) {
|
||||||
|
gpioSetMode(pin, PI_OUTPUT);
|
||||||
|
gpioServo(pin, lv);
|
||||||
|
}
|
||||||
|
|
||||||
|
int map(int input, int input_start, int input_end, int output_start, int output_end) {
|
||||||
|
return output_start + ((output_end - output_start) / (input_end - input_start)) * (input - input_start);
|
||||||
|
}
|
||||||
|
|
||||||
|
int conver_pc_raw(unsigned pin, int lv) {
|
||||||
|
int lv_new;
|
||||||
|
switch(pin) {
|
||||||
|
case LEFT_RUDDER_GPIO:
|
||||||
|
lv_new = map(lv, 0, 100, LEFT_RUDDER_MIN, LEFT_RUDDER_MAX);
|
||||||
|
break;
|
||||||
|
case RIGHT_RUDDER_GPIO:
|
||||||
|
lv_new=map(lv, 0, 100, RIGHT_RUDDER_MIN, RIGHT_RUDDER_MAX);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return lv_new;
|
||||||
|
}
|
||||||
|
|
||||||
|
void servo_mv(char*msg, char* mode, unsigned int pin, int *lv) {
|
||||||
|
if (*mode == ' ')
|
||||||
|
*mode = ( *lv < 500 ) ? '%':'R';
|
||||||
|
|
||||||
|
if (*mode == '%') {
|
||||||
|
*lv=conver_pc_raw(pin, *lv);
|
||||||
|
} else
|
||||||
|
if (*mode == 'R') {
|
||||||
|
} else {
|
||||||
|
eprintf("[servos] mode not valid (%c)\n", *mode);
|
||||||
|
strcpy(msg, "SER_MODE_NOK");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
servo_mv_r(pin, *lv);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void servo_process(char*msg) {
|
||||||
|
char servo_index = msg[4];
|
||||||
|
char servo_mode = msg[5];
|
||||||
|
int lv;
|
||||||
|
|
||||||
|
sscanf(&msg[6], " %i ", &lv );
|
||||||
|
|
||||||
|
if (servo_index == 'L') {
|
||||||
|
servo_mv(msg, &servo_mode, LEFT_RUDDER_GPIO, &lv);
|
||||||
|
} else
|
||||||
|
if (servo_index == 'R') {
|
||||||
|
servo_mv(msg, &servo_mode, RIGHT_RUDDER_GPIO, &lv);
|
||||||
|
} else {
|
||||||
|
eprintf("[servos] index not valid (%c)\n", servo_index);
|
||||||
|
strcpy(msg, "SER_INDX_NOK");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sprintf(msg, "SER %c%c %i", servo_index, servo_mode, lv);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef __SERVOS_H__
|
||||||
|
#define __SERVOS_H__
|
||||||
|
|
||||||
|
#include <pigpio.h>
|
||||||
|
|
||||||
|
#include "../rfudp/TxTunnel.h"
|
||||||
|
#include "PinConfig.h"
|
||||||
|
|
||||||
|
void servo_process(char*msg);
|
||||||
|
|
||||||
|
#endif // __SERVOS_H__
|
|
@ -84,6 +84,7 @@ int main(int argc, char** argv){
|
||||||
|
|
||||||
|
|
||||||
/* RF setup */
|
/* RF setup */
|
||||||
|
|
||||||
radio.begin(); // Setup and configure rf radio
|
radio.begin(); // Setup and configure rf radio
|
||||||
radio.powerUp();
|
radio.powerUp();
|
||||||
#ifdef RF_CHANNEL
|
#ifdef RF_CHANNEL
|
||||||
|
@ -153,6 +154,10 @@ int main(int argc, char** argv){
|
||||||
inet_ntoa(cliaddr.sin_addr), // addrress
|
inet_ntoa(cliaddr.sin_addr), // addrress
|
||||||
ntohs(cliaddr.sin_port) // port
|
ntohs(cliaddr.sin_port) // port
|
||||||
);
|
);
|
||||||
|
if ( n ==0 ) {
|
||||||
|
printf("empty msg, discarded\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
printf("MSG: \'%s\'\n", buffer);
|
printf("MSG: \'%s\'\n", buffer);
|
||||||
|
|
||||||
|
|
||||||
|
@ -179,7 +184,7 @@ int main(int argc, char** argv){
|
||||||
loop_start = millis();
|
loop_start = millis();
|
||||||
radio.startListening();
|
radio.startListening();
|
||||||
while ( !radio.available() && (millis() - loop_start) < 100) {
|
while ( !radio.available() && (millis() - loop_start) < 100) {
|
||||||
// Serial.println(F("waiting."));
|
// wait till receive or timeout
|
||||||
}
|
}
|
||||||
if (millis() - loop_start >= 100) {
|
if (millis() - loop_start >= 100) {
|
||||||
eprintf("Not Response.\n\r");
|
eprintf("Not Response.\n\r");
|
||||||
|
@ -192,7 +197,6 @@ int main(int argc, char** argv){
|
||||||
radio.stopListening();
|
radio.stopListening();
|
||||||
#endif
|
#endif
|
||||||
buffer[n] = '\0';
|
buffer[n] = '\0';
|
||||||
|
|
||||||
/* END RF Part */
|
/* END RF Part */
|
||||||
|
|
||||||
printf("ANS: \'%s\'\n", buffer);
|
printf("ANS: \'%s\'\n", buffer);
|
||||||
|
@ -203,6 +207,10 @@ int main(int argc, char** argv){
|
||||||
(const struct sockaddr *) &cliaddr, cliaddr_len
|
(const struct sockaddr *) &cliaddr, cliaddr_len
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//clean buffer
|
||||||
|
//buffer[0]='\0';
|
||||||
|
//memset(buffer, 0, sizeof(buffer));
|
||||||
|
|
||||||
}while(loop_on);
|
}while(loop_on);
|
||||||
|
|
||||||
// Power down the antenna
|
// Power down the antenna
|
||||||
|
|
|
@ -63,5 +63,6 @@ do{
|
||||||
|
|
||||||
buffer[n] = '\0';
|
buffer[n] = '\0';
|
||||||
printf("ANS: \'%s\'\n", buffer);
|
printf("ANS: \'%s\'\n", buffer);
|
||||||
|
//buffer[0]='\0';
|
||||||
}while(loop_on);
|
}while(loop_on);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue