xrp style wpilib comms
Loading...
Searching...
No Matches
xrp_accel.h
Go to the documentation of this file.
1#pragma once
2#define XRP_TAG_ACCEL 0x17
3#include "message_type.h"
4
5typedef struct {
6 union {
7 float accels[3];
8 struct {
9 float x; // Acceleration in X/forward direction
10 float y; // Acceleration in Y/left direction
11 float z; // Acceleration in Z/up direction
12 };
13 };
15
16template <>
18 static constexpr uint8_t value = XRP_TAG_ACCEL;
19 static constexpr bool hasId = false;
20};
21
22#include "byteutils.h"
23
24class XrpAccel : public MessageType {
25public:
27 : data({ { 0.0f, 0.0f, 0.0f } }) // Initialize with default values
28 {
29 }
30
31 XrpAccel(float xAccel, float yAccel, float zAccel)
32 : data({ { xAccel, yAccel, zAccel } })
33 {
34 }
35
36 int getTag() override
37 {
38 return XRP_TAG_ACCEL;
39 }
40 bool hasId() override
41 {
42 return false; // Acceleration data does not have an ID
43 }
44 uint8_t getId() override
45 {
46 return 255; // No ID for acceleration data
47 }
48 void setData(const void* dataPtr) override
49 {
50 if (dataPtr != nullptr) {
51 data = *static_cast<const xrp_accel_t*>(dataPtr);
52 }
53 }
54 void* getData() override
55 {
56 return &data;
57 }
58 int toNetworkBuffer(char* buffer, int pos, int end) override
59 {
60 if (end - pos < 14) { // 1 for size, 1 for tag, 12 for accelerations
61 return 0;
62 }
63 buffer[pos] = 13; // size excluding size byte itself
64 buffer[pos + 1] = XRP_TAG_ACCEL;
65 floatToNetwork(data.x, buffer, pos + 2);
66 floatToNetwork(data.y, buffer, pos + 6);
67 floatToNetwork(data.z, buffer, pos + 10);
68 return 14; // 1 for size, 1 for tag, 3 floats (4 bytes each)
69 }
70 int fromNetworkBuffer(char* buf, int pos, int end) override
71 {
72 return 0; // not implemented, it doesn't make much sense to get an acceleration reading from the computer
73 // if (end - pos < 13) { // Minimum size is 13 bytes
74 // return 0;
75 // }
76 // // buf[pos] is the tag, which should have already been confirmed to be XRP_TAG_ACCEL
77 // data.x = networkToFloat(buf, pos + 1);
78 // data.y = networkToFloat(buf, pos + 5);
79 // data.z = networkToFloat(buf, pos + 9);
80 // return 13; // Size of the message in bytes
81 }
82
83 // default destructor is fine
84
85protected:
87};
void floatToNetwork(float num, char *buf, int offset)
Definition byteutils.cpp:44
Definition message_type.h:3
Definition xrp_accel.h:24
XrpAccel(float xAccel, float yAccel, float zAccel)
Definition xrp_accel.h:31
bool hasId() override
Definition xrp_accel.h:40
XrpAccel()
Definition xrp_accel.h:26
int toNetworkBuffer(char *buffer, int pos, int end) override
Definition xrp_accel.h:58
xrp_accel_t data
Definition xrp_accel.h:86
void setData(const void *dataPtr) override
Definition xrp_accel.h:48
int getTag() override
Definition xrp_accel.h:36
uint8_t getId() override
Definition xrp_accel.h:44
int fromNetworkBuffer(char *buf, int pos, int end) override
Definition xrp_accel.h:70
void * getData() override
Definition xrp_accel.h:54
Definition message_type.h:18
static constexpr uint8_t value
Definition message_type.h:19
Definition xrp_accel.h:5
float y
Definition xrp_accel.h:10
float z
Definition xrp_accel.h:11
float x
Definition xrp_accel.h:9