xrp style wpilib comms
Loading...
Searching...
No Matches
xrp_motor.h
Go to the documentation of this file.
1#pragma once
2#define XRP_TAG_MOTOR 0x12
3
4#include <Arduino.h>
5
6typedef struct {
7 uint8_t id;
8 float value; // -1 to 1
10
11template <>
13 static constexpr uint8_t value = XRP_TAG_MOTOR;
14 static constexpr bool hasId = true;
15};
16
17#include "byteutils.h"
18
19class XrpMotor : public MessageType {
20public:
22 : data({ 0, 0.0f }) // Initialize with default values
23 {
24 }
25 XrpMotor(uint8_t id, float value)
26 : data({ id, value })
27 {
28 }
29
30 int getTag() override
31 {
32 return XRP_TAG_MOTOR;
33 }
34 bool hasId() override
35 {
36 return true;
37 }
38 uint8_t getId() override
39 {
40 return data.id;
41 }
42 void setData(const void* dataPtr) override
43 {
44 if (dataPtr != nullptr) {
45 data = *static_cast<const xrp_motor_t*>(dataPtr);
46 }
47 }
48 void* getData() override
49 {
50 return &data;
51 }
52 int toNetworkBuffer(char* buffer, int pos, int end) override
53 {
54 return 0; // not implemented, it doesn't make much sense to send a motor command to the computer from the robot
55 }
56 int fromNetworkBuffer(char* buf, int pos, int end) override
57 {
58 if (end - pos < 6) {
59 return 0;
60 }
61 // buf[pos] is the tag, which should have already been confirmed to be XRP_TAG_MOTOR
62 int channelID = (uint8_t)buf[pos + 1];
63 float value = networkToFloat(buf, pos + 2);
64 data.id = channelID;
65 data.value = value;
66 return 6; // 1 byte for tag, 1 byte for id, 4 bytes for value
67 }
68
69 // the default destructor is fine
70
71protected:
73};
float networkToFloat(char *buf, int offset)
Definition byteutils.cpp:9
Definition message_type.h:3
Definition xrp_motor.h:19
XrpMotor(uint8_t id, float value)
Definition xrp_motor.h:25
xrp_motor_t data
Definition xrp_motor.h:72
int getTag() override
Definition xrp_motor.h:30
void * getData() override
Definition xrp_motor.h:48
bool hasId() override
Definition xrp_motor.h:34
int fromNetworkBuffer(char *buf, int pos, int end) override
Definition xrp_motor.h:56
void setData(const void *dataPtr) override
Definition xrp_motor.h:42
int toNetworkBuffer(char *buffer, int pos, int end) override
Definition xrp_motor.h:52
XrpMotor()
Definition xrp_motor.h:21
uint8_t getId() override
Definition xrp_motor.h:38
Definition message_type.h:18
static constexpr uint8_t value
Definition message_type.h:19
Definition xrp_motor.h:6
uint8_t id
Definition xrp_motor.h:7
float value
Definition xrp_motor.h:8