xrp style wpilib comms
Loading...
Searching...
No Matches
xrp-style-wpilib-comms.h
Go to the documentation of this file.
1
4#pragma once
5
6// add includes when you add new message types
14
15#include <Arduino.h>
16#include <WiFi.h>
17#include <WiFiUdp.h>
18#include <cstdint>
19#include <vector>
20
21#include "message_type.h"
22
23#define UDP_PACKET_MAX_SIZE_XRP 1000 // I think the rpi pico xrp firmware uses 8192, but that's absurdly large
24
29class XSWC {
30protected:
36 public:
37 static MessageType* createMessageType(uint8_t tag)
38 {
39 // add cases when you add new message types
40 switch (tag) {
41 case XRP_TAG_MOTOR:
42 return new XrpMotor();
43 case XRP_TAG_SERVO:
44 return new XrpServo();
45 case XRP_TAG_DIO:
46 return new XrpDio();
47 case XRP_TAG_ANALOG:
48 return new XrpAnalog();
49 case XRP_TAG_GYRO:
50 return new XrpGyro();
51 case XRP_TAG_ACCEL:
52 return new XrpAccel();
53 case XRP_TAG_ENCODER:
54 return new XRPEncoder();
55 default:
56 return nullptr;
57 }
58 }
59 };
60
61public:
62 // methods to receive data (add methods when you add new message types)
63
70 bool getData_xrp_motor(xrp_motor_t& data, const uint8_t id)
71 {
72 return getData<xrp_motor_t>(data, id);
73 }
80 float getValue_xrp_motor(const uint8_t id)
81 {
82 xrp_motor_t data;
83 if (getData_xrp_motor(data, id))
84 return data.value;
85 return 0.0f;
86 }
93 bool getData_xrp_servo(xrp_servo_t& data, const uint8_t id)
94 {
95 return getData<xrp_servo_t>(data, id);
96 }
103 float getValue_xrp_servo(const uint8_t id)
104 {
105 xrp_servo_t data;
106 if (getData_xrp_servo(data, id))
107 return data.value;
108 return 0.0f;
109 }
116 bool getData_xrp_dio(xrp_dio_t& data, const uint8_t id)
117 {
118 return getData<xrp_dio_t>(data, id);
119 }
126 bool getValue_xrp_dio(const uint8_t id)
127 {
128 xrp_dio_t data;
129 if (getData_xrp_dio(data, id))
130 return data.value == 1;
131 return false;
132 }
139 bool getData_xrp_analog(xrp_analog_t& data, const uint8_t id)
140 {
141 return getData<xrp_analog_t>(data, id);
142 }
149 float getValue_xrp_analog(const uint8_t id)
150 {
151 xrp_analog_t data;
152 if (getData_xrp_analog(data, id))
153 return data.value;
154 return 0.0f;
155 }
156
157 // methods to send data (add methods when you add new message types)
164 bool sendData_xrp_dio(const xrp_dio_t& data, bool checkUniqueness = false)
165 {
166 return sendData<xrp_dio_t>(data, checkUniqueness);
167 }
175 bool sendValue_xrp_dio(const uint8_t id, bool value, bool checkUniqueness = false)
176 {
177 xrp_dio_t data;
178 data.id = id;
179 data.value = value ? 1 : 0; // 1 for true, 0 for false
180 return sendData_xrp_dio(data, checkUniqueness);
181 }
182
189 bool sendData_xrp_analog(const xrp_analog_t& data, bool checkUniqueness = false)
190 {
191 return sendData<xrp_analog_t>(data, checkUniqueness);
192 }
193
201 bool sendValue_xrp_analog(const uint8_t id, float value, bool checkUniqueness = false)
202 {
203 xrp_analog_t data;
204 data.id = id;
205 data.value = value;
206 return sendData_xrp_analog(data, checkUniqueness);
207 }
208
215 bool sendData_xrp_encoder(const xrp_encoder_t& data, bool checkUniqueness = false)
216 {
217 return sendData<xrp_encoder_t>(data, checkUniqueness);
218 }
219
229 bool sendValue_xrp_encoder(const uint8_t id, int32_t count, int32_t period = 0, int32_t divisor = 1, bool checkUniqueness = false)
230 {
231 xrp_encoder_t data;
232 data.id = id;
233 data.count = count;
234 data.period = period;
235 data.divisor = divisor;
236 return sendData_xrp_encoder(data, checkUniqueness);
237 }
238
246 bool sendData_xrp_gyro(const xrp_gyro_t& data, bool checkUniqueness = false)
247 {
248 return sendData<xrp_gyro_t>(data, checkUniqueness);
249 }
250
263 bool sendValue_xrp_gyro(float xRate, float yRate, float zRate, float roll, float pitch, float yaw, bool checkUniqueness = false)
264 {
265 xrp_gyro_t data;
266 data.rates[0] = xRate;
267 data.rates[1] = yRate;
268 data.rates[2] = zRate;
269 data.angles[0] = roll;
270 data.angles[1] = pitch;
271 data.angles[2] = yaw;
272 return sendData_xrp_gyro(data, checkUniqueness);
273 }
274
282 bool sendData_xrp_accel(const xrp_accel_t& data, bool checkUniqueness = false)
283 {
284 return sendData<xrp_accel_t>(data, checkUniqueness);
285 }
286
296 bool sendValue_xrp_accel(const uint8_t id, float xAccel, float yAccel, float zAccel, bool checkUniqueness = false)
297 {
298 xrp_accel_t data;
299 data.accels[0] = xAccel;
300 data.accels[1] = yAccel;
301 data.accels[2] = zAccel;
302 return sendData_xrp_accel(data, checkUniqueness);
303 }
304
308 XSWC();
309
319 bool begin(void (*_receiveCallback)(void), void (*_sendCallback)(void), uint16_t port = 3540);
320
332 bool begin(const char* ssid, const char* password, void (*_receiveCallback)(void), void (*_sendCallback)(void), const char* hostname = "XRP-XSWC", uint16_t port = 3540);
333
338 bool update();
339
340 bool isConnected();
341 bool isEnabled();
343
344 unsigned long TIMEOUT_MS = 1000;
345 unsigned long MIN_UPDATE_TIME_MS = 50; // 20Hz
346
350 bool useAP = false;
351
352protected:
353 // recall data from list of received messages
354 template <typename T>
355 bool getData(T& data, const uint8_t id)
356 {
357 for (MessageType* msg : receivedMessages) {
358 if (msg->getTag() == TYPE_TO_TAG_VAL(T) && (msg->hasId() == false || msg->getId() == id)) {
359 // Check if we've found a message of the correct type and with the correct ID
360 data = *static_cast<T*>(msg->getData());
361 return true; // Data found
362 }
363 }
364 return false; // No data found
365 }
366
367 // save data into list of messages to send
368 template <typename T>
369 bool sendData(T data, bool checkUniqueness)
370 {
371 MessageType* message = nullptr;
372 if (checkUniqueness) {
373 // search sent messages for a message with tag and id
374 for (MessageType* msg : sentMessages) {
375 if (msg->getTag() == TYPE_TO_TAG_VAL(T)) {
376 if constexpr (HAS_ID(T) == false) {
377 // Found a message of the correct type (and no ID is needed for this type)
378 message = msg; // overwrite it
379 break;
380 } else {
381 if (msg->getId() == data.id) {
382 // Found a message of the correct type and with the correct ID
383 message = msg; // overwrite it
384 break;
385 }
386 }
387 }
388 }
389 }
390 if (message == nullptr) {
391 // No existing message found, create a new one
392 message = MessageTypeFactory::createMessageType(TYPE_TO_TAG_VAL(T));
393 }
394 if (message != nullptr) {
395 message->setData(&data);
396 sentMessages.push_back(message);
397 return true; // Data successfully added to the list
398 }
399 return false;
400 }
401
402 boolean
403 processReceivedBufferIntoMessages(char* buffer, int length);
404 int processMessagesIntoBufferToSend(char* buffer, int length);
405
406 WiFiUDP udp; // UDP instance for communication
407
408 std::vector<MessageType*> receivedMessages;
409 std::vector<MessageType*> sentMessages;
410
411 boolean cmdEnable = false;
412
415
416 uint16_t txSeq = 0;
417
418 char rxBuf[UDP_PACKET_MAX_SIZE_XRP + 1];
419 char txBuf[UDP_PACKET_MAX_SIZE_XRP + 1];
420
421 bool connectedToRemote = false;
422 IPAddress udpRemoteAddr = IPAddress();
423 int32_t udpRemotePort = -1;
424
425 void (*sendCallback)(void);
426 void (*receiveCallback)(void);
427}; // end class XSWC
428
429extern XSWC xswc; // a global instance is created in the .cpp file
Definition message_type.h:3
virtual void setData(const void *data)=0
Definition xrp_encoder.h:20
Factory class to create message types based on their tag. This is only used internally by the XSWC cl...
Definition xrp-style-wpilib-comms.h:35
static MessageType * createMessageType(uint8_t tag)
Definition xrp-style-wpilib-comms.h:37
top level class for the XRP-style WPILib communications This class handles the UDP communication,...
Definition xrp-style-wpilib-comms.h:29
boolean processReceivedBufferIntoMessages(char *buffer, int length)
Definition xrp-style-wpilib-comms.cpp:16
char txBuf[UDP_PACKET_MAX_SIZE_XRP+1]
Definition xrp-style-wpilib-comms.h:419
bool sendValue_xrp_accel(const uint8_t id, float xAccel, float yAccel, float zAccel, bool checkUniqueness=false)
Sends accelerometer values.
Definition xrp-style-wpilib-comms.h:296
bool isConnected()
Definition xrp-style-wpilib-comms.cpp:176
int processMessagesIntoBufferToSend(char *buffer, int length)
Definition xrp-style-wpilib-comms.cpp:50
boolean cmdEnable
Definition xrp-style-wpilib-comms.h:411
bool getData(T &data, const uint8_t id)
Definition xrp-style-wpilib-comms.h:355
void(* sendCallback)(void)
Definition xrp-style-wpilib-comms.h:425
bool sendData_xrp_gyro(const xrp_gyro_t &data, bool checkUniqueness=false)
Sends gyroscope data.
Definition xrp-style-wpilib-comms.h:246
void(* receiveCallback)(void)
Definition xrp-style-wpilib-comms.h:426
float getValue_xrp_servo(const uint8_t id)
Retrieves the value of a specific servo by ID.
Definition xrp-style-wpilib-comms.h:103
bool isConnectedAndEnabled()
Definition xrp-style-wpilib-comms.cpp:186
unsigned long millisWhenLastMessageReceived
Definition xrp-style-wpilib-comms.h:413
IPAddress udpRemoteAddr
Definition xrp-style-wpilib-comms.h:422
int32_t udpRemotePort
Definition xrp-style-wpilib-comms.h:423
unsigned long millisWhenLastSent
Definition xrp-style-wpilib-comms.h:414
bool sendData_xrp_accel(const xrp_accel_t &data, bool checkUniqueness=false)
Sends accelerometer data.
Definition xrp-style-wpilib-comms.h:282
bool getData_xrp_motor(xrp_motor_t &data, const uint8_t id)
Retrieves data for a specific motor by ID.
Definition xrp-style-wpilib-comms.h:70
bool begin(void(*_receiveCallback)(void), void(*_sendCallback)(void), uint16_t port=3540)
begin udp communication on given port
Definition xrp-style-wpilib-comms.cpp:61
bool getData_xrp_analog(xrp_analog_t &data, const uint8_t id)
Retrieves data for a specific analog input by ID.
Definition xrp-style-wpilib-comms.h:139
unsigned long TIMEOUT_MS
Definition xrp-style-wpilib-comms.h:344
bool update()
call this in void loop()
Definition xrp-style-wpilib-comms.cpp:120
bool getData_xrp_servo(xrp_servo_t &data, const uint8_t id)
Retrieves data for a specific servo by ID.
Definition xrp-style-wpilib-comms.h:93
bool getData_xrp_dio(xrp_dio_t &data, const uint8_t id)
Retrieves data for a specific digital input/output by ID.
Definition xrp-style-wpilib-comms.h:116
bool sendValue_xrp_dio(const uint8_t id, bool value, bool checkUniqueness=false)
Send a boolean value for a specific digital input/output by ID.
Definition xrp-style-wpilib-comms.h:175
unsigned long MIN_UPDATE_TIME_MS
Definition xrp-style-wpilib-comms.h:345
bool connectedToRemote
Definition xrp-style-wpilib-comms.h:421
XSWC()
constructor of XSWC class, use the global xswc instance to access this class
Definition xrp-style-wpilib-comms.cpp:9
bool isEnabled()
Definition xrp-style-wpilib-comms.cpp:181
bool sendValue_xrp_gyro(float xRate, float yRate, float zRate, float roll, float pitch, float yaw, bool checkUniqueness=false)
Sends gyroscope values.
Definition xrp-style-wpilib-comms.h:263
char rxBuf[UDP_PACKET_MAX_SIZE_XRP+1]
Definition xrp-style-wpilib-comms.h:418
bool useAP
set to true before calling begin() to skip straight to creating an Access Point
Definition xrp-style-wpilib-comms.h:350
WiFiUDP udp
Definition xrp-style-wpilib-comms.h:406
uint16_t txSeq
Definition xrp-style-wpilib-comms.h:416
bool getValue_xrp_dio(const uint8_t id)
Retrieves the value of a specific digital input/output by ID.
Definition xrp-style-wpilib-comms.h:126
float getValue_xrp_analog(const uint8_t id)
Retrieves the value of a specific analog input by ID.
Definition xrp-style-wpilib-comms.h:149
std::vector< MessageType * > receivedMessages
Definition xrp-style-wpilib-comms.h:408
bool sendData_xrp_dio(const xrp_dio_t &data, bool checkUniqueness=false)
Sends data for a specific digital input by ID.
Definition xrp-style-wpilib-comms.h:164
std::vector< MessageType * > sentMessages
Definition xrp-style-wpilib-comms.h:409
bool sendData(T data, bool checkUniqueness)
Definition xrp-style-wpilib-comms.h:369
bool sendValue_xrp_analog(const uint8_t id, float value, bool checkUniqueness=false)
Sends a float value for a specific analog input by ID.
Definition xrp-style-wpilib-comms.h:201
float getValue_xrp_motor(const uint8_t id)
Retrieves the value of a specific motor by ID.
Definition xrp-style-wpilib-comms.h:80
bool sendData_xrp_analog(const xrp_analog_t &data, bool checkUniqueness=false)
Sends data for a specific analog input by ID.
Definition xrp-style-wpilib-comms.h:189
bool sendData_xrp_encoder(const xrp_encoder_t &data, bool checkUniqueness=false)
Sends data for a specific encoder by ID.
Definition xrp-style-wpilib-comms.h:215
bool sendValue_xrp_encoder(const uint8_t id, int32_t count, int32_t period=0, int32_t divisor=1, bool checkUniqueness=false)
Sends values for a specific encoder by ID.
Definition xrp-style-wpilib-comms.h:229
Definition xrp_accel.h:24
Definition xrp_analog.h:18
Definition xrp_dio.h:18
Definition xrp_gyro.h:32
Definition xrp_motor.h:19
Definition xrp_servo.h:18
Definition xrp_accel.h:5
float accels[3]
Definition xrp_accel.h:7
Definition xrp_analog.h:5
float value
Definition xrp_analog.h:7
uint8_t id
Definition xrp_analog.h:6
Definition xrp_dio.h:5
uint8_t value
Definition xrp_dio.h:7
uint8_t id
Definition xrp_dio.h:6
Definition xrp_encoder.h:5
int32_t count
Definition xrp_encoder.h:7
uint8_t id
Definition xrp_encoder.h:6
int32_t period
Definition xrp_encoder.h:8
int32_t divisor
Definition xrp_encoder.h:9
Definition xrp_gyro.h:5
float angles[3]
Definition xrp_gyro.h:15
float rates[3]
Definition xrp_gyro.h:7
Definition xrp_motor.h:6
float value
Definition xrp_motor.h:8
Definition xrp_servo.h:5
float value
Definition xrp_servo.h:7
XSWC xswc
Definition xrp-style-wpilib-comms.cpp:191