net.h
Go to the documentation of this file.
1 
10 #ifndef NET_H
11 #define NET_H
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
18 #define P0_SEND_FREQUENCY 300
19 
21 #define P1_SEND_FREQUENCY 1000
22 
24 #define P2_SEND_FREQUENCY 20000
25 
27 #define UPLINK_CHECK_FREQUENCY 100
28 
29 #define BLOCKING_MODE 0
30 
31 #define OUTBOUND_QUEUE_SIZE 30
32 #define INBOUND_QUEUE_SIZE 100
33 
34 #define EDIT_NONE 0
35 #define EDIT_PITCH_GAIN 1
36 #define EDIT_ROLL_GAIN 2
37 #define EDIT_YAW_GAIN 3
38 
39 #define API_HEADER_LENGTH 17
40 #define API_HEADER_PREFIX 3
41 
42 #define RECEIVER_ADDRESS 0x0013A20040B47E6B//37745 //B47E6B
43 
44 #define INCREMENT_DATA_FRAME 0x00
45 #define OPTION_BYTE 0x01 //Disables ACK
46 
47 //FRAME TYPE
48 #define TX_PACKET 0x10
49 #define BROADCAST_RADIUS 1 //0 is infinite number of hops to reach target
50 #define RAW_PACKET_BUFFER_SIZE 16 // Number of buffer regions to allow for incoming commands
51 
52 #define HEARTBEAT_TIMEOUT 10000 //In Milliseconds
53 
54 #define GPS_TIMEOUT 30000 //In Milliseconds
55 
56 #define HEARTBEAT_KILL_TIMEOUT 20000 //In Milliseconds
57 
58 #define UHF_KILL_TIMEOUT 10000
59 
60 typedef enum _p_priority {
61  PRIORITY0 = 0,
62  PRIORITY1 = 1,
63  PRIORITY2 = 2,
64 } p_priority;
65 
66 
67 /* For reference:
68  In MPLAB XC 16 compiler:
69  char : 1 byte
70  int : 2 bytes
71  long int : 4 bytes
72  float : 4 bytes
73  long double : 8 bytes
74  */
75 
76 // 72 bytes
77 struct priority1_block { //High Frequency - Multiple times per second
78  long double lat, lon; // Latitude and longitude from gps // 2x8 Byte
79  long int sysTime; // 4 bytes
80  float UTC; //4 Byte
81  float pitch, roll, yaw;
83  float airspeed;
84  float alt; //4 Byte
85  float gSpeed;
86  int heading; //2 Byte
90 };
91 
92 // 88 bytes
93 struct priority2_block { //Medium Frequency - Once every second
94  float rollKD, rollKP;
95  float pitchKD, pitchKP;
96  float yawKD, yawKP;
97  float pathChecksum; // 4 bytes
98  int lastCommandsSent[4]; //4*2 bytes
99  int batteryLevel1, batteryLevel2; // 2*2 bytes
104  char wirelessConnection; //1 byte
105  char autopilotActive; //1 byte
106  char gpsStatus; //1 Byte
107  char numWaypoints; //1 bytes
108  char waypointIndex; //1 byte
109  char pathFollowing; // 1 byte
110 };
111 
112 // 75 bytes
113 struct priority3_block { //Low Frequency - On update...
114  float rollKI; //4 Bytes
115  float pitchKI;
116  float yawKI;
123  unsigned int startupErrorCodes; //2 bytes
126 };
127 
128 typedef union {
129  struct priority1_block p1_block;
130  struct priority2_block p2_block;
131  struct priority3_block p3_block;
132 } packetPayload;
133 
134 struct telem_block {
135  const int type;
137 };
138 
139 struct telem_buffer {
140  unsigned int sendIndex; // index into telemetry to send
141  unsigned char header[API_HEADER_LENGTH]; // The header for the telem
142  union {
143  struct telem_block *asStruct; // The telemetry block being sent
144  unsigned char *asArray; // The telemetry intepreted as an array
145  } telemetry;
146  unsigned char checksum; // The checksum so far
147 };
148 
149 struct command {
150  unsigned char cmd;
151  unsigned char data_length;
152  unsigned char data[101];
153 };
154 
155 // Initialize the data link
156 int initDataLink(void);
157 
158 //Check whether or not the datalink is connected to the board properly.
160 
161 // Create a telemetry block to use for debugging, only creates one instance
163 
164 // Create a telem block returns null if fails
166 
167 int packetCount();
168 // Destroy a dataBlock
170 
171 // Add a telem_block to the outbound data queue
172 // Returns the position in the queue or -1 if no room in queue
174 // Get the number of items waiting to be sent
175 int getOutboundQueueLength(void);
176 // Clear all telem blocks waiting to be sent
178 
179 // Pop next telem_block from incoming buffer, null if no data
181 
182 // generate the header for the a telemetry block
183 unsigned int generateApiHeader(unsigned char *apiString, char dataFrame);
184 
185 // Stage the given telemetry block
186 void stageTelemetryBlock(struct telem_block *telem);
187 // Do outbound buffer maintenance
188 void outboundBufferMaintenance(void);
189 
190 // Send a block of telemetry immediately, probably shouldn't call this directly
191 int sendTelemetryBlock(struct telem_block *telem);
192 
193 /****************************************************************************
194  * Inbound relevant functions
195  */
196 
197 // Clean up the command
198 void destroyCommand( struct command* cmd );
199 
200 // Get the next command
201 struct command* popCommand();
202 
203 // Queue up another command
204 int pushCommand(struct command* cmd);
205 
206 // Create a new command struct
207 struct command* createCommand( char* rawPacket );
208 
209 // Check and make sure the char array is a valid packet
210 int checkPacket( char* rawPacket);
211 
212 // Handle the inbound buffer
213 void inboundBufferMaintenance(void);
214 
215 #ifdef __cplusplus
216 }
217 #endif
218 
219 #endif /* NET_H */
int cameraStatus
Definition: net.h:102
int pushOutboundTelemetryQueue(struct telem_block *data)
Definition: net_outbound.c:139
int initDataLink(void)
net_outbound.c
Definition: net_common.c:11
int pitchRateSetpoint
Definition: net.h:88
float yaw
Definition: net.h:81
const int type
Definition: net.h:135
unsigned char header[API_HEADER_LENGTH]
Definition: net.h:141
char autopilotActive
Definition: net.h:105
float yawKI
Definition: net.h:116
int headingSetpoint
Definition: net.h:103
long int sysTime
Definition: net.h:79
int ch8Out
Definition: net.h:101
char gpsStatus
Definition: net.h:106
int heading
Definition: net.h:86
int ch7Out
Definition: net.h:101
Definition: net.h:62
int ch4In
Definition: net.h:100
int throttleSetpoint
Definition: net.h:89
Definition: net.h:149
int ch2In
Definition: net.h:100
int ch6Out
Definition: net.h:101
int ch2Out
Definition: net.h:101
int sendTelemetryBlock(struct telem_block *telem)
Definition: net_outbound.c:269
int rollRateSetpoint
Definition: net.h:87
char numWaypoints
Definition: net.h:107
char wirelessConnection
Definition: net.h:104
float yawKD
Definition: net.h:96
float airspeed
Definition: net.h:83
int ch3Out
Definition: net.h:101
float pitchRate
Definition: net.h:82
unsigned char data_length
Definition: net.h:151
float pitchKP
Definition: net.h:95
float headingKD
Definition: net.h:117
float yawRate
Definition: net.h:82
float flapKI
Definition: net.h:120
Definition: net.h:63
void destroyTelemetryBlock(struct telem_block *data)
Definition: net_outbound.c:131
float rollKP
Definition: net.h:94
float altitudeKD
Definition: net.h:118
float flapKP
Definition: net.h:120
struct telem_block * createTelemetryBlock(p_priority packet)
Definition: net_outbound.c:23
int ch6In
Definition: net.h:100
int ch3In
Definition: net.h:100
float pitch
Definition: net.h:81
void destroyCommand(struct command *cmd)
Definition: net_inbound.c:30
int altitudeSetpoint
Definition: net.h:103
int batteryLevel2
Definition: net.h:99
struct telem_block * popOutboundTelemetryQueue(void)
Definition: net_outbound.c:218
packetPayload data
Definition: net.h:136
struct command * createCommand(char *rawPacket)
Definition: net_inbound.c:53
long double lon
Definition: net.h:78
_p_priority
Definition: net.h:60
int yawRateSetpoint
Definition: net.h:103
float pitchKD
Definition: net.h:95
unsigned int startupErrorCodes
Definition: net.h:123
int flapSetpoint
Definition: net.h:103
unsigned char data
Definition: GPS.c:117
float rollKI
Definition: net.h:114
float rollRate
Definition: net.h:82
float pathGain
Definition: net.h:121
int ch1In
Definition: net.h:100
int ch7In
Definition: net.h:100
float rollKD
Definition: net.h:94
long double lat
Definition: net.h:78
int startupSettings
Definition: net.h:124
unsigned char data[101]
Definition: net.h:152
int batteryLevel1
Definition: net.h:99
int ch8In
Definition: net.h:100
float pathChecksum
Definition: net.h:97
enum _p_priority p_priority
unsigned int generateApiHeader(unsigned char *apiString, char dataFrame)
Definition: net_outbound.c:230
int packetCount()
Definition: net_outbound.c:30
void inboundBufferMaintenance(void)
Definition: net_inbound.c:92
int checkDataLinkConnection()
Definition: net_common.c:19
float headingKP
Definition: net.h:117
int pushCommand(struct command *cmd)
Definition: net_inbound.c:44
int ch4Out
Definition: net.h:101
struct telem_block * asStruct
Definition: net.h:143
float throttleKD
Definition: net.h:119
unsigned int sendIndex
Definition: net.h:140
unsigned char checksum
Definition: net.h:146
Definition: net.h:61
float altitudeKP
Definition: net.h:118
unsigned char * asArray
Definition: net.h:144
int rollSetpoint
Definition: net.h:87
unsigned char apiString[100]
Definition: GPS.c:129
unsigned char cmd
Definition: net.h:150
union telem_buffer::@0 telemetry
float alt
Definition: net.h:84
int lastCommandsSent[4]
Definition: net.h:98
int getOutboundQueueLength(void)
Definition: net_outbound.c:150
char probeStatus
Definition: net.h:125
char pathFollowing
Definition: net.h:109
struct command * popCommand()
Definition: net_inbound.c:36
float altitudeKI
Definition: net.h:118
int ch1Out
Definition: net.h:101
float UTC
Definition: net.h:80
struct telem_block * getDebugTelemetryBlock(p_priority packet)
Definition: net_outbound.c:35
float headingKI
Definition: net.h:117
float pitchKI
Definition: net.h:115
int autonomousLevel
Definition: net.h:122
int checkPacket(char *rawPacket)
Definition: net_inbound.c:73
int clearOutboundTelemetryQueue(void)
Definition: net_outbound.c:160
char waypointIndex
Definition: net.h:108
float yawKP
Definition: net.h:96
float flapKD
Definition: net.h:120
float roll
Definition: net.h:81
float gSpeed
Definition: net.h:85
float throttleKP
Definition: net.h:119
#define API_HEADER_LENGTH
Definition: net.h:39
void stageTelemetryBlock(struct telem_block *telem)
Definition: net_outbound.c:208
int pitchSetpoint
Definition: net.h:88
float orbitGain
Definition: net.h:121
void outboundBufferMaintenance(void)
Definition: net_outbound.c:172
int ch5In
Definition: net.h:100
int ch5Out
Definition: net.h:101
float throttleKI
Definition: net.h:119