Skip to content
Snippets Groups Projects
Unverified Commit 55b09635 authored by Jake Drahos's avatar Jake Drahos
Browse files

Began writing header files for common

parent c9879e71
No related branches found
No related tags found
No related merge requests found
#ifndef _controller_h
#define _controller_h
enum ControllerID {
ROLL_ID, // 00 - Roll PID
PITCH_ID, // 01 - Pitch PID
YAW_ID, // 02 - Yaw PID
ROLL_RATE_ID, // 03 - Roll rate PID
PITCH_RATE_ID, // 04 - Pitch rate PID
YAW_RATE_ID, // 05 - Yaw rate PID
LOCAL_X_ID, // 06 - Local X PID
LOCAL_Y_ID, // 07 - Local Y PID
ALT_ID, // 08 - Altitude PID
};
enum ControllerValueID{
KP_ID, // 00 - P constant
KI_ID, // 01 - I constant
KD_ID, // 02 - D constant
SP_ID, // 03 - Setpoint value
};
struct controller_message {
enum ControllerID id;
enum ControllerValueID value_id;
float value;
};
#endif
#include "packet.h"
#include "setcontrol.h"
#include "commands.h"
#include <sys/types.h>
static int next_msg_id = 0;
int SendSetControl()
{
struct metadata m;
struct controller_message cm;
cm.id = ROLL_ID; /* Roll controller */
cm.value_id = KI_ID; /* I coefficient */
cm.value = 42.00f; /* An appropriate value */
uint8_t data[64];
m.msg_id = next_msg_id++;
/* Fills in the rest of the metadata automatically */
if (EncodeSetcontrol(&m, data, 64, &cm) < 0) {
return -1;
}
uint8_t packet[128];
ssize_t length;
if ((length = EncodePacket(packet, 128, &m, data)) < 0) {
return -1;
}
/* Now you can send the packet
* sendThePacket(packet, length);
*/
return 0;
}
/* Process incoming packets */
int receive_packet(uint8_t * packet, size_t length)
{
struct metadata m;
uint8_t data[128];
/* Will fail if checksum is bad, etc */
if (DecodePacket(&m, data, 128, packet, length) < 0) {
return -1;
}
/* Let's pretend that the callbacks have signature
* void cb(
* const struct metadata * m,
* const uint8_t * data);
*
* Call the callback.
*/
(*MessageTypes[m.msg_type].functionPtr)(&m, data);
return 0;
}
/* An example of a setcontrol callback. Arguments
* aren't important, as long as we have access
* to the packet bytes and the length
*/
void cb_setcontrol(const struct metadata *m, const uint8_t *data)
{
struct controller_message cm;
DecodeSetcontrol(&cm, m, data);
/* cm now has populated controller ID, value ID, and value */
}
#ifndef _packet_h
#define _packet_h
#include <stdint.h>
#include <sys/types.h>
struct metadata {
int msg_type;
int msg_id;
int data_len;
};
/* Combine metadata and data to form a wire-sendable packet.
* Returns the size of the encoded packet
*/
ssize_t EncodePacket(
uint8_t * packet, /* Buffer to encode into */
size_t packet_size, /* Max buffer size */
const struct metadata * m, /* Metadata to encode */
const uint8_t * data); /* Data to encode */
/* Break apart packet, populating metadata. Data is copied
* into the space pointed to by data.
* Returns the size of the data.
*/
ssize_t DecodePacket(
struct metadata * m, /* Decoded metadata (includes data_len)*/
uint8_t * data, /* Data is copied into this buffer */
size_t data_size, /* Max buffer size */
const uint8_t * packet, /* Packet to decode */
size_t packet_size); /* Size of packet to decode */
#endif
#ifndef _setcontrol_h
#define _setcontrol_h
#include "packet.h"
#include "controller.h"
#include <sys/types.h>
/* Creates data and metadata for a setcontrol packet
* Returns data size.
*/
ssize_t EncodeSetcontrol(
struct metadata * m, /* data_len and msg_type will be populated*/
uint8_t * data, /* Output buffer */
size_t data_size, /* Max buffer size */
const struct controller_message * cm); /* Message to encode */
/* Decode a metadata and data to populate a controller.
* Returns 0 on success, -1 on failure.
*/
int DecodeSetcontrol(
struct controller_message * cm, /* Decoded controller message */
const struct metadata * m, /* Metadata to aid in decoding */
const uint8_t * data); /* Data to decode */
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment