Commit e5c896c2 authored by Philippe Gaussier's avatar Philippe Gaussier
Browse files

Small test program without graphical interface.

parent 90e0f9c8
......@@ -9,7 +9,7 @@
* http://www.termsys.demon.co.uk/vtansi.htm
*/
#include "../include/PixhawkServer.h"
#include "include/PixhawkServer.h"
#include <iostream>
#include <string>
......@@ -32,7 +32,7 @@ int main(int argc, char *argv[])
// General object
shared_ptr<Serial_Port_ReadingThread> readingThread;
shared_ptr<Serial_Port_WritingThread> writingThread;
shared_ptr<Display_IHM> ihm;
// shared_ptr<Display_IHM> ihm;
drone1 = shared_ptr<Drone>(new Drone(device_path, device_baudrate));
readingThread = shared_ptr<Serial_Port_ReadingThread>(new Serial_Port_ReadingThread(500, 200, drone1));
......
/**
* @author Sylvain Colomer
* @date 17/08/18.
*/
#ifndef ABSTRACT_THREADCLASS_H
#define ABSTRACT_THREADCLASS_H
#include <iostream>
#include <string>
#include <iostream>
#include <unistd.h>
#include <termios.h>
#include <thread>
#include <mutex>
/**
* A thread have multiple state during is life
* TO_INIT -> Not initialised thread
* INIT -> Thread in initialisation
* READY -> Thread ready to be start
* RUN -> Thread which work
* STOP -> Thread stopped
* QUIT -> Thread in quit process
* PROBLEM -> Blocked thread in reason of a problem
* DEADLINE_EXCEEDED -> thread
*
*/
enum LifeCoreState {
TO_INIT,
INIT,
READY,
RUN,
STOP,
QUIT,
PROBLEM,
DEADLINE_EXCEEDED
};
/**
* Abstract class which convey to implement a thread brick
* Use a real time context
*/
class Abstract_ThreadClass {
protected:
// REAL TIME VAR
/**
* timeval that save the begin of the running method
*/
struct timeval begin;
/**
* timeval use to control the period of the real time thread
*/
struct timeval front_checkpoint;
/**
* timeval use to control the period of the real time thread
*/
struct timeval end_checkpoint;
/**
* Represent the period between all task in ms
*/
int task_period = 1000;
/**
* Var use to define when a task is overcome is execution delay
*/
int task_deadline = 1000; //ms
// RUN VAR
/**
* Mutex of the run flag
*/
std::mutex runFlag_mutex;
/**
* Run flag : convey to loop the system
*/
bool runFlag = true;
// MAIN THREAD VAR
/**
* The principal thread of the system
*/
std::thread principalThread;
/**
* Var use to share the state of the system
*/
LifeCoreState currentState=LifeCoreState::TO_INIT;;
public :
/**
* Default constructor : take in parameters the task period time and the task deadline time
* @param task_period period between all task execution
* @param task_deadline alert limit use to send an alert message when the task are too long (see the notion of real time context)
*/
Abstract_ThreadClass(int task_period, int task_deadline);
/**
* Default destructor
*/
virtual ~Abstract_ThreadClass();
/**
* Method which convey to initialise the thread
* Don't overwrite it without knowledge of the method goal
*/
void init();
/**
* Start method : use it to first start the thread (only one time)
* It create the thread associated to the run method
*/
void start();
/**
* Convey to stop the thread.
*/
void stop();
/**
* Stop function : convey to stop the thread without join()
* Warning : it usage is not recommended if the user don't understand it interest. (usually to exit the main core)
*/
void lazyStop();
/**
* Play method : convey to restart the thread before a first start and after a pause
*/
void play();
/**
* Pause method : convey to pause the thread without quit it
*/
void pause();
/**
* Method wich allow to join the thread
* Warning : don't double join a thread
*/
void join();
/**
* Method main loop. Implement a soft real time context
* Overwrite it to create your real time thread
*/
virtual void run() = 0;
// GETTER AND SETTERS
/**
* Only a setter
* @return
*/
bool isRunFlag() const;
/**
* Setter of the run mutex : need to unlock the mutex
* @param runFlag
*/
void setRunFlag(bool runFlag);
/**
* Only a getter
* @return
*/
LifeCoreState getCurrentState() const;
};
#endif
/**
* @author Sylvain Colomer
* @date 17/08/18.
*/
#ifndef SERIAL_PORT_WRITINGTHREAD_H
#define SERIAL_PORT_WRITINGTHREAD_H
#include <iostream>
#include <string>
#include <iostream>
#include <unistd.h>
#include <termios.h>
#include <thread>
#include <mutex>
#include <sys/time.h>
#include "Abstract_ThreadClass.h"
#include "Drone.h"
#include "Display_IHM.h"
#include "blc_channel.h"
#include "blc_program.h"
class Serial_Port_WritingThread : public Abstract_ThreadClass {
protected:
/**
* Drone buffer
*/
std::shared_ptr<Drone> drone1;
// Different buffer use for get the data
int time_stamps;
//Channels use to arm the drone
std::shared_ptr<blc_channel> blc_control_arm; // 1=on, 0=off
//Channels use to control manually the drone
std::shared_ptr<blc_channel> blc_control_remote_vector; //x, y, z, r
public:
/**
* Default constructor of the class. Need to have the
*/
Serial_Port_WritingThread(int task_period, int task_deadline, std::shared_ptr<Drone> drone1);
~Serial_Port_WritingThread();
void run();
/**
* Direct writing method command
*/
int main_loop();
};
#endif //VEHICLEWATCHER_Abstract_ThreadClass_H
/**
* @brief Tool class which convey to open a serial port
* @file serial_port.cpp
*
* @author Sylvain Colomer
* @date 19/04/19
* @version 1.1
*/
#ifndef COMM_SERIAL_H
#define COMM_SERIAL_H
// The following two non-standard baudrates should have been defined by the system
// If not, just fallback to number
#ifndef B460800
#define B460800 460800
#endif
#ifndef B921600
#define B921600 921600
#endif
// Status flags
#define SERIAL_PORT_OPEN 1
#define SERIAL_PORT_CLOSED 0
#define SERIAL_PORT_ERROR -1
#include <common/mavlink.h>
#include <cstdlib>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <pthread.h>
#include <signal.h>
#include <string>
/*
* Serial Port Class
*
* This object handles the opening and closing of the offboard computer's
* serial port over which we'll communicate. It also has methods to write
* a byte stream buffer. MAVlink is not used in this object yet, it's just
* a serialization interface. To help with read and write pthreading, it
* gaurds any port operation with a pthread mutex.
*/
class Serial_Port
{
public:
bool debug;
std::string uart_name;
int baudrate;
int status;
public:
Serial_Port();
Serial_Port(std::string uart_name, int baudrate);
~Serial_Port();
void initialize_defaults();
int read_message(mavlink_message_t &message);
int write_message(const mavlink_message_t &message);
int open_serial();
void close_serial();
void start();
void stop();
void handle_quit();
private:
int fd;
mavlink_status_t lastStatus;
pthread_mutex_t lock;
int _open_port(const char* port);
bool _setup_port(int baud);
int _read_port(uint8_t &cp);
int _write_port(char *buf, unsigned len);
};
#endif // SERIAL_PORT_H_
/**
* @author Sylvain Colomer
* @date 17/08/18.
*/
#ifndef SERIAL_PORT_READINGTHREAD_H
#define SERIAL_PORT_READINGTHREAD_H
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <iostream>
#include <unistd.h>
#include <termios.h>
#include <thread>
#include <mutex>
#include <sys/time.h>
#include "Abstract_ThreadClass.h"
#include "Data_Drone.h"
#include "blc_channel.h"
#include "blc_program.h"
class Serial_Port_ReadingThread : public Abstract_ThreadClass {
protected:
/**
* Serial port use with the drone
*/
std::shared_ptr<Serial_Port> serial1;
/**
* Drone buffer
*/
std::shared_ptr<Drone> drone1;
/**
* Mavlink message dictionnary
*/
std::map<int, std::string> mavlink_dic;
//Different blc channel use to publish data
std::shared_ptr<blc_channel> blc_heartbeat; //Hearbeat message
std::shared_ptr<blc_channel> blc_highres_imu;
public:
Serial_Port_ReadingThread(int task_period, int task_deadline, std::shared_ptr<Drone> drone1);
~Serial_Port_ReadingThread();
void run();
uint64_t get_time_usec();
/**
* Reading function of mavlink message and diffusion by shared memory
*/
void read_messages(mavlink_message_t message);
int testAck(mavlink_command_ack_t *ack, std::string message);
void handle_command_ack(mavlink_command_ack_t *ack);
};
#endif //VEHICLEWATCHER_Abstract_ThreadClass_H
/**
* @author Sylvain Colomer
* @date 17/08/18.
*/
#ifndef SERIAL_PORT_WRITINGTHREAD_H
#define SERIAL_PORT_WRITINGTHREAD_H
#include <iostream>
#include <string>
#include <iostream>
#include <unistd.h>
#include <termios.h>
#include <thread>
#include <mutex>
#include <sys/time.h>
#include "Abstract_ThreadClass.h"
#include "Data_Drone.h"
#include "blc_channel.h"
#include "blc_program.h"
class Serial_Port_WritingThread : public Abstract_ThreadClass {
protected:
/**
* Drone buffer
*/
std::shared_ptr<Drone> drone1;
// Different buffer use for get the data
int time_stamps;
//Channels use to control manually the drone
std::shared_ptr<blc_channel> blc_control_motors; //x, y, z, r
std::shared_ptr<blc_channel> blc_control_commands; // 1=on, 0=off
public:
/**
* Default constructor of the class. Need to have the
*/
Serial_Port_WritingThread(int task_period, int task_deadline, std::shared_ptr<Drone> drone1);
~Serial_Port_WritingThread();
void run();
/**
* Direct writing method command
*/
int main_loop();
};
#endif //VEHICLEWATCHER_Abstract_ThreadClass_H
/**
* @brief Tool class which convey to open a serial port
* @file serial_port.cpp
*
* @author Sylvain Colomer
* @date 19/04/19
* @version 1.1
*/
#ifndef COMM_WIFI_H
#define COMM_WIFI_H
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <arpa/inet.h>
#include <string.h>
#include <common/mavlink.h>
#define BUFFER_LENGTH 2041
/*
* Wifi port class
*
*/
class Wifi_Port
{
private:
int sock;
struct sockaddr_in targetAddr; // The socket of the target address
struct sockaddr_in locAddr; // The socket of the local address
uint16_t len;
uint8_t buf[BUFFER_LENGTH];
int bytes_sent;
char target_ip[100]= "192.168.1.36"; // default simulation ip
int local_port = 14550; //default simulation port
public:
Wifi_Port();
~Wifi_Port();
int initialize_defaults(int *sock, struct sockaddr_in *locAddr, struct sockaddr_in *targetAddr, int local_port, char *target_ip);
int read_message(mavlink_message_t *message);
int write_message(const mavlink_message_t *message);
private:
};
#endif
\ No newline at end of file
/**
* @brief Tool class which convey to open a serial port
* @file serial_port.cpp
*
* @author Sylvain ColomerJoystickModeAttitude
* @date 19/04/19
* @version 1.1
*/
#ifndef DRONE_H
#define DRONE_H
#include <common/mavlink.h>
#include <iostream>
#include <string>
#include <iostream>
#include <unistd.h>
#include <termios.h>
#include <thread>
#include <mutex>
#include <sys/time.h>
#include "Com_Serial.h"
#include "Com_Wifi.h"
/**
* Define about drone communication protocol. it's use to automatic open specific communication
*/
enum Drone_Communication {
DRONE_WIFI,
DRONE_SERIAL,
DRONE_DUAL
};
/**
* define about drone mode
*/
enum Drone_Motors{
ARM,
UNARM
};
/**
* define about drone mode
*/
enum Drone_mode{
DRONE_OFF,
DRONE_MANUAL_DIRECT // Mode manual with direct control of the motors
};
/**
* Drone classes
*/
class Drone
{
public:
Drone_Communication drone_communication;
Drone_Motors motors=UNARM;
Drone_mode mode=DRONE_OFF;
//Communication part
Drone_Communication communication=DRONE_SERIAL;
std::shared_ptr<Serial_Port> serial1;
std::shared_ptr<Wifi_Port> wifi1;