Pedro Campos / Mbed OS SERVOS_V0_3
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers declaraciones.h Source File

declaraciones.h

00001 #ifndef DECLARACIONES_H
00002 #define DECLARACIONES_H
00003 /*
00004  declaraciones.h
00005  PROGRAMA CONTROL DE SERVO DIRECCION BARCO
00006  Copyright (c) 2020 Pedro Campos <pedro@iotelectronic.com>
00007  All rights reserved.
00008 
00009   */
00010 
00011 
00012 #include "mbed.h"
00013 #include "mbed_toolchain.h"
00014 
00015 #include "gps.h"
00016 
00017 #define VERSION "V0.1"
00018 
00019 //#define GPS_OUT
00020 
00021 // TIPO DE TRAMAS ETHERNET
00022 #define TRAMA_ESTADO_MOTOR_1    0x0001
00023 #define TRAMA_ESTADO_MOTOR_2    0x0002
00024 #define TRAMA_ESTADO_SERVO_DIR  0x0010  // Trama estado del servo de direccion
00025 
00026 
00027 // TIPO DE TRAMAS COMUNICACION RS485
00028 #define TIPO_TRAMA_ESTADO_RS485       0x10
00029 #define TIPO_TRAMA_POSICION_RS485     0x11
00030 #define TIPO_TRAMA_ESTADO_MANDO_RS485 0x15
00031 
00032 #define NUL   0x00
00033 #define STX   0x02
00034 #define ETX   0x03
00035 #define EOT   0x04
00036 #define ENQ   0x05
00037 #define ACK   0x06
00038 #define LF    0x0A
00039 #define CR    0x0D
00040 #define NAK   0x15
00041 
00042 
00043 #define TIMONES_HIST 2    // 2% histeresis timones
00044 
00045 // CONTROL SERVO
00046 #define TORQUE_MINIMO 15  // Depende de la resistencia e inercia del volante
00047 #define TORQUE_MAXIMO 70  // Depende de la resistencia e inercia del volante
00048 #define TORQUE_PILOTO 15  // Torque en piloto automático
00049 
00050 #define DESVIACION_MAXIMA       450   // desviacion máxima mando timones
00051 #define TORQUE_MAX_PILOTO_AUTO  300   // Torque maximo leido en piloto automatico
00052 
00053 #define MASK_PULS_RUMBO     0x01
00054 #define MASK_PULS_RADIO     0x02
00055 #define MASK_PULS_BOCINA    0x04
00056 #define MASK_PULS_AUX       0x08
00057 #define MASK_PULS_MOT_BAB   0x10
00058 #define MASK_PULS_MOT_EST   0x20
00059 #define MASK_PULS_ANCLA_UP  0x40
00060 #define MASK_PULS_ANCLA_DN  0x80
00061 
00062 
00063 typedef uint8_t byte;
00064 
00065 
00066 enum enEstadoDireccion { DIR_REPOSO=0, DIR_MASTER=1, DIR_SLAVE=2, DIR_RUMBO=3, DIR_PILOTO=4, DIR_ERROR=5, DIR_END_PILOT=6};
00067 enum enEstadoServo { ESTADO_SERVO_OFF = 0, ESTADO_SERVO_ON = 1, ESTADO_SERVO_ERROR = 2 };
00068 
00069 
00070 
00071 // ESTRUCTURA DE VARIABLES DE ESTADO DEL SERVO DE DIRECCION
00072 struct __attribute__((packed)) st_datos_servo {       
00073   byte    n_servo;
00074   byte    estado;
00075   float   latitud;
00076   float   longitud;
00077   long    fecha_hora;
00078   float   velocidad;          // NUDOS
00079   float   rumbo;
00080   byte    ch_radio;
00081   byte    trim_timon_bab;
00082   byte    trim_trans_bab;
00083   byte    trim_timon_estr;
00084   byte    trim_trans_estr;
00085   byte    timon_babor;        // %de timon babor bajado, para 100% 5s, 1% = 50ms
00086   byte    timon_estribor;     // %de timon estribor bajado, para 100% 5s
00087   float   rumbo_fijado;       // Rumbo fijado al pulsar botón para RUMBO FIJO
00088   float   rumbo_piloto_auto;  // Rumbo fijado por piloto automático
00089   int     posicion_mando;     // Posicion actual del mando
00090 };
00091 
00092 
00093 
00094 /*
00095  * ESTRUCTURA COMUNICACIONES POR RS485
00096  */
00097 struct __attribute__((packed)) st_trama_estado {    // ESTRUCTURA TRAMA DE ESTADO RECIBIDA DESDE SERVOS
00098   uint8_t bStart;    // start trama
00099   uint8_t nBytes;    // numero de bytes de la trama
00100   uint8_t tipo;      // tipo de trama
00101   uint8_t servo;     // Si es servo de babor = 0
00102   float HDOP;
00103   float latitud;
00104   float longitud;
00105   uint32_t timeStamp;
00106   float velocidad;
00107   float rumbo;
00108   uint8_t nSatelites;
00109   uint8_t estado;
00110   uint8_t timBabor;
00111   uint8_t timEstribor;
00112   uint16_t posicion;
00113   uint8_t trim_timon_bab;
00114   uint8_t trim_trans_bab;
00115   uint8_t trim_timon_estr;
00116   uint8_t trim_trans_estr;
00117   float rumbo_fijado;       // Rumbo fijado en RUMBO FIJO o PILOTO AUTOMATICO
00118   uint16_t ch_radio;
00119   uint16_t chk;      // checksum con XOR 0xffff desde b_start (inclusive) a chk
00120   uint8_t bStop;     // stop trama 
00121 };
00122 
00123 
00124 struct __attribute__((packed)) st_trama_posicion {    // ESTRUCTURA TRAMA DE POSICION RECIBIDA DESDE SERVO
00125   uint8_t bStart;    // start trama
00126   uint8_t nBytes;    // numero de bytes de la trama
00127   uint8_t tipo;      // tipo de trama
00128   uint8_t servo;     // Si es servo de babor = 0
00129   uint16_t posicion;
00130   uint8_t estado;
00131   uint16_t chk;   // checksum con XOR 0xffff desde b_start (inclusive) a chk
00132   uint8_t bStop;     // stop trama 
00133 };
00134 
00135 
00136 struct __attribute__((packed)) st_trama_estado_mando {    // ESTRUCTURA TRAMA DE POSICION ENVIADA DESDE MANDOS
00137   uint8_t bStart;    // start trama
00138   uint8_t nBytes;    // numero de bytes de la trama
00139   uint8_t tipo;      // tipo de trama
00140   uint8_t mando;     // Si es mando de babor = 0
00141   uint8_t estado;    // estado del mando
00142   uint8_t rudBab;    // trim del timon babor
00143   uint8_t rudEst;    // trim del timon estribor
00144   uint8_t traBab;    // trim de transmision babor
00145   uint8_t traEst;    // trim de transmision estribor
00146   uint8_t radioCH;   // canal radio
00147   uint8_t pulsado;   // estado pulsadores
00148   uint16_t chk;   // checksum con XOR 0xffff desde b_start (inclusive) a chk
00149   uint8_t bStop;     // stop trama 
00150 };
00151 
00152 
00153 // Time protocol implementation : Address: time.nist.gov UDPPort: 37   
00154 typedef struct {
00155     uint32_t secs;         // Transmit Time-stamp seconds.
00156 }ntp_packet;
00157 
00158 
00159 const char str_estado[7][10] = { "CENTERED", "MASTER", "SLAVE", "HEADING", "AUT PILOT", "ERROR", "END PILOT"};
00160 
00161 const int buffer_size = 255;     // Tamaño del buffer de rx y tx para RS485
00162 
00163 
00164 void epoch_to_time(long epoch);
00165 void grados(float pos, int res[]);  // devuelve grados
00166 
00167 void procesa_servo(void);           // lee estado del servo, procesa torque y actualiza
00168 void posicion_servo(void);          // AJUSTA POSICION SERVO SI ES NECESARIO
00169 bool escribe_parametro_servo(byte tipo, byte numero, long valor);   //  * ENVIA PARAMETRO AL SERVO
00170 
00171 uint16_t checksum(uint8_t *buf, uint16_t nb);   // devuelve checksum de nb bytes del buffer
00172 
00173 void envia_posicion_mando(uint16_t posicion_mando);  // * ENVIA POSICION DEL MANDO
00174 void envia_estado_mando(void);    // ENVIA ESTADO A LOS MANDOS
00175 
00176 void  procesa_posicion_timones(void);   // Pocesa activación y posición timones, llamada cada 100ms
00177 
00178 void onSerialRxRS485(void);     // Procesa interrupcion de RX en RS485
00179 void onSerialTxRS485(void);     // Procesa interrupcion de TX en RS485
00180 void PrintHex8(uint8_t *data, uint8_t length);  // prints 8-bit data in hex with leading zeroes
00181 
00182 void SerialTxRS485(void);    // Procesa envio del buffer
00183 void udp_main(void);        // Hilo de proceso de tramas Ethernet
00184 
00185 void actualiza_timones(void);   // ACTUALIZA TIMONES
00186 
00187 void actualizaFechaHoraGPS(float fecha, float hora);
00188 void getTime(void);
00189 
00190 void procesa_estado(void);      // PROCESA CAMBIO DE ESTADO
00191 unsigned long millis(void);     // devuelve cuenta de tiempo en ms
00192 
00193 void procesa_pulsadores(uint8_t pulsadores);    // Procesa segun estado de los pulsadores del mando
00194 void procesa_piloto_automatico(void);           // procesa piloto automático
00195 
00196 long constrain(long valor, long minimo, long maximo);   // * DEVUELVE VALOR DENTRO DEL LIMITE
00197 long map(long valor, long minimo_in, long maximo_in, long minimo_out, long maximo_out);     // * EXTRAPOLA VALOR EN UN RANGO
00198 
00199 void sincroniza_servos(void);            // SINCRONIZA LOS DOS SERVOS SEGUN SU ESTADO
00200 
00201 uint16_t crc16(uint8_t * buf , uint16_t len);   // CALCULO DE CRC PARA TRAMA MODBUS
00202 
00203 void test_modbus(void);                 // TEST PARA TRAMA MODBUS
00204 void envia_trama_modbus(void);          // ENVIA TRAMA MODBUS RS485
00205 
00206 
00207 #endif