forked from SolidGeek/VescUart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVescUart.h
152 lines (126 loc) · 3.84 KB
/
VescUart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef _VESCUART_h
#define _VESCUART_h
#include <Arduino.h>
#include "datatypes.h"
#include "buffer.h"
#include "crc.h"
class VescUart
{
/** Struct to store the telemetry data returned by the VESC */
struct dataPackage {
float avgMotorCurrent;
float avgInputCurrent;
float dutyCycleNow;
long rpm;
float inpVoltage;
float ampHours;
float ampHoursCharged;
long tachometer;
long tachometerAbs;
};
/** Struct to hold the nunchuck values to send over UART */
struct nunchuckPackage {
int valueX;
int valueY;
bool upperButton; // valUpperButton
bool lowerButton; // valLowerButton
};
public:
/**
* @brief Class constructor
*/
VescUart(void);
/** Variabel to hold measurements returned from VESC */
dataPackage data;
/** Variabel to hold nunchuck values */
nunchuckPackage nunchuck;
/**
* @brief Set the serial port for uart communication
* @param port - Reference to Serial port (pointer)
*/
void setSerialPort(HardwareSerial* port);
/**
* @brief Set the serial port for debugging
* @param port - Reference to Serial port (pointer)
*/
void setDebugPort(Stream* port);
/**
* @brief Sends a command to VESC and stores the returned data
*
* @return True if successfull otherwise false
*/
bool getVescValues(void);
/**
* @brief Sends values for joystick and buttons to the nunchuck app
*/
void setNunchuckValues(void);
/**
* @brief Set the current to drive the motor
* @param current - The current to apply
*/
void setCurrent(float current);
/**
* @brief Set the current to brake the motor
* @param brakeCurrent - The current to apply
*/
void setBrakeCurrent(float brakeCurrent);
/**
* @brief Set the rpm of the motor
* @param rpm - The desired RPM (actually eRPM = RPM * poles)
*/
void setRPM(float rpm);
/**
* @brief Set the duty of the motor
* @param duty - The desired duty (0.0-1.0)
*/
void setDuty(float duty);
/**
* @brief Help Function to print struct dataPackage over Serial for Debug
*/
void printVescValues(void);
private:
/** Variabel to hold the reference to the Serial object to use for UART */
HardwareSerial* serialPort = NULL;
/** Variabel to hold the reference to the Serial object to use for debugging.
* Uses the class Stream instead of HarwareSerial */
Stream* debugPort = NULL;
/**
* @brief Packs the payload and sends it over Serial
*
* @param payload - The payload as a unit8_t Array with length of int lenPayload
* @param lenPay - Length of payload
* @return The number of bytes send
*/
int packSendPayload(uint8_t * payload, int lenPay);
/**
* @brief Receives the message over Serial
*
* @param payloadReceived - The received payload as a unit8_t Array
* @return The number of bytes receeived within the payload
*/
int receiveUartMessage(uint8_t * payloadReceived);
/**
* @brief Verifies the message (CRC-16) and extracts the payload
*
* @param message - The received UART message
* @param lenMes - The lenght of the message
* @param payload - The final payload ready to extract data from
* @return True if the process was a success
*/
bool unpackPayload(uint8_t * message, int lenMes, uint8_t * payload);
/**
* @brief Extracts the data from the received payload
*
* @param message - The payload to extract data from
* @return True if the process was a success
*/
bool processReadPacket(uint8_t * message);
/**
* @brief Help Function to print uint8_t array over Serial for Debug
*
* @param data - Data array to print
* @param len - Lenght of the array to print
*/
void serialPrint(uint8_t * data, int len);
};
#endif