Skip to content

Commit 34cd740

Browse files
committed
First Release for 3DRacers Lib
Includes improved examples, IR sensor gates, improved Update Mode for HM-10, added mode for reduced flash memory footprint.
1 parent 5d13250 commit 34cd740

20 files changed

+2481
-364
lines changed

3DRacers.cpp

+432-180
Large diffs are not rendered by default.

3DRacers.h

+48-28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
////////
2-
//
3-
//Written by Marco D'Alia and Davide Marcoccio
4-
//2015
5-
//
6-
//
7-
////////
8-
91
#ifndef THREEDRACERS_H
102
#define THREEDRACERS_H
113

@@ -15,14 +7,18 @@
157

168
#include <Servo.h>
179
#include <Adafruit_NeoPixel.h>
10+
#include <sha204_library.h>
1811

1912
#include "3DRacers_Packets.h"
2013
#include "BleHM10Driver.h"
14+
#include "IRSensor.h"
2115

2216
//#include "variants/3DRacers_Pins_1.1.0.h"
2317
//#include "variants/3DRacers_Pins_1.2.0.h"
2418
//#include "variants/3DRacers_Pins_1.4.5.h"
25-
#include "variants/3DRacers_Pins_1.5.6.h"
19+
//#include "variants/3DRacers_Pins_1.5.6.h"
20+
//#include "variants/3DRacers_Pins_1.6.1.h"
21+
#include "variants/3DRacers_Pins_1.6.2.h"
2622
//#include "variants/3DRacers_Pins_Breadboard.h"
2723
#include "Config.h"
2824

@@ -48,8 +44,8 @@ typedef struct CarInfo
4844

4945
CarInfo()
5046
{
51-
maxBatteryLevel = 4200;
52-
minBatteryLevel = 2800;
47+
maxBatteryLevel = 3330;
48+
minBatteryLevel = 3200;
5349

5450
throttle = 0;
5551
steerAngle = 45;
@@ -64,24 +60,25 @@ class ThreeDRacers
6460

6561
public:
6662

67-
int gateMinDelay;
68-
int gatePower;
69-
7063
int servoPin;
7164
int sensorPin;
7265
int ledPin;
7366

7467
bool netDebug;
7568
Servo servo;
7669

70+
#if !REDUCED_FEATURES
7771
Adafruit_NeoPixel ledStrip;
72+
#endif
7873

74+
atsha204Class identity;
75+
void GetId(uint8_t* rx_buffer);
7976

8077
ThreeDRacers();
8178

8279
//Use to start in your setup() function
8380
void Begin(HardwareSerial &bleSerial, Serial_ &serial);
84-
81+
8582
//Call it repeatly in your loop() function:
8683
void Process();
8784

@@ -100,21 +97,26 @@ class ThreeDRacers
10097
//to be used for low level control, use Motor* functions if possible
10198
void MotorControl(int controlSpeed, bool brake);
10299

103-
100+
char input[BLE_CMD_SIZE];
101+
104102
//CALLBACKS
105103

106104
//Set this callbacks to your functions to customize the bot behaviour:
107-
void OnConnect(void(*f)(CarInfo&, ConfigCommand&)){ processConnectCommand = f; }
108-
void OnDriveCommand(void(*f)(DriveCommand&, CarInfo&)){ processDriveCommand = f; }
109-
void OnConfigCommand(void(*f)(ConfigCommand&, CarInfo&)){ processConfigCommand = f; }
110-
void OnRaw1Command(void(*f)(void*, CarInfo&)){ processRaw1Command = f; }
111-
// void (*OnGateDetected)(CarInfo& car){}
105+
void OnConnect(void(*f)(CarInfo&, ConfigCommand&)) { processConnectCommand = f; }
106+
void OnDriveCommand(void(*f)(DriveCommand&, CarInfo&)) { processDriveCommand = f; }
107+
void OnConfigCommand(void(*f)(ConfigCommand&, CarInfo&)) { processConfigCommand = f; }
108+
void OnRaw1Command(void(*f)(void*, CarInfo&)) { processRaw1Command = f; }
109+
void OnGateDetected(void(*f)(AckCommand& cmd, CarInfo& car)) { processOnGateDetected = f; }
112110

113111
//TX towards central device
114-
void OnAckNotification();
115-
112+
void SendAckNotification();
116113

114+
void Send(void* packet, unsigned int objSize);
115+
116+
BleHM10Driver wireless;
117+
117118
private:
119+
unsigned int tickCount;
118120
int connectionLedLoop;
119121
bool wasConnected;
120122

@@ -124,17 +126,21 @@ class ThreeDRacers
124126
const int EEPROM_INVERT_STEERING;
125127
const int EEPROM_INVERT_THROTTLE;
126128

127-
char input[BLE_CMD_SIZE];
128-
unsigned long packetCount;
129-
unsigned long lastPacketCount;
129+
130+
unsigned int packetCount;
131+
unsigned int lastPacketCount;
130132
unsigned long nextConnectionCheck;
131-
BleHM10Driver wireless;
132133

134+
IRSensor sensor;
135+
133136
CarInfo carInfo;
134137
DriveCommand driveCmd;
135138
ConfigCommand configCmd;
136139
AckCommand ackCmd;
137140
NameCommand nameCmd;
141+
IdentityCommand idCmd;
142+
143+
uint8_t idCurrentNonce[32];
138144

139145
//
140146
/*------------------------------------------------------*/
@@ -154,8 +160,11 @@ class ThreeDRacers
154160
void(*processConnectCommand)(CarInfo& car, ConfigCommand& config);
155161
void(*processDriveCommand)(DriveCommand& cmd, CarInfo& car);
156162
void(*processConfigCommand)(ConfigCommand& cmd, CarInfo& car);
163+
void(*processOnGateDetected)(AckCommand& cmd, CarInfo& car);
157164
void(*processRaw1Command)(void* cmd, CarInfo& car);
158165

166+
void SendIdResponse(short msgPart, uint8_t* temp_message);
167+
159168
//called inside Begin()
160169
void motorSetup();
161170
//unify 2 bytes into an int16
@@ -167,7 +176,7 @@ class ThreeDRacers
167176
void ConnectionStatus();
168177
void connectionChanged(bool connected);
169178
void debugConfigPck();
170-
bool packetCheck(char* input);
179+
bool packetCheck(char* input, unsigned int objSize);
171180

172181
//structs update funcs, called right before it's function ptr companion
173182
bool updateDriveCommand(char* input);
@@ -177,7 +186,18 @@ class ThreeDRacers
177186
void showInfoShellCommand();
178187
void checkMotorsCommand();
179188

189+
190+
void processSerial();
191+
void processPackets();
192+
180193
long readVcc();
194+
195+
void printId();
196+
void calculateSignature(uint8_t* temp_message, uint8_t* current_nonce);
197+
198+
byte CRC8(const uint8_t* message, int len);
199+
200+
//bool setPin(String& command, bool value);
181201
};
182202

183203
#endif

3DRacers_Packets.h

+50-11
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
#define CONFIG_CMD_ID 1
44
#define ACK_CMD_ID 2
55
#define NAME_CMD_ID 3
6+
#define IDENTITY_CMD_ID 4
67

7-
//NB: avr-gcc on Atmega32u4 seems to use already a 1byte alignment for structs, however to be sure we'll set the packed attribute here.
8+
9+
//NB: avr-gcc on Atmega32u4 seems to use already a 1byte alignment for structs, however to be sure we'll set the ((packed)) attribute on each struct.
810

911
//15bytes
1012
struct __attribute__ ((packed)) DriveCommand
1113
{
1214
char id;
13-
unsigned long packetCount;
15+
unsigned int packetCount;
16+
byte crc;
1417

1518
short throttle;//From 0 to 1024 //2B
1619
short steerAngle;//From -90 to 90 //2B
@@ -26,6 +29,7 @@ struct __attribute__ ((packed)) DriveCommand
2629
{
2730
id = DRIVE_CMD_ID;
2831
packetCount = 0;
32+
crc = 0x00;
2933
throttle = 0;
3034
steerAngle = 45;
3135
brake = false;
@@ -39,23 +43,27 @@ struct __attribute__ ((packed)) DriveCommand
3943
};
4044
typedef struct DriveCommand DriveCommand;
4145

42-
//13bytes
46+
//17bytes
4347
struct __attribute__ ((packed)) AckCommand
4448
{
4549
char id;
46-
unsigned long packetCount;
50+
unsigned int packetCount;
51+
byte crc;
4752

4853
short batteryLevel;
4954
short sensorLevel;
50-
unsigned long lastGateDetected; //NB: it seems that there are some problems with unsigned longs in c#
55+
unsigned long lastGateDetected;
56+
unsigned long lastGateDuration;
5157

5258
AckCommand()
5359
{
5460
id = ACK_CMD_ID;
5561
packetCount = 0;
62+
crc = 0x00;
5663
batteryLevel = 0;
5764
sensorLevel = 0;
5865
lastGateDetected = 0;
66+
lastGateDuration = 0;
5967
}
6068

6169
};
@@ -77,12 +85,13 @@ typedef union {
7785
byte raw;
7886
} flags_cfg;
7987

80-
//10bytes
88+
//8bytes
8189
struct __attribute__ ((packed)) ConfigCommand
8290
{
8391

8492
char id;
85-
unsigned long packetCount;
93+
unsigned int packetCount;
94+
byte crc;
8695

8796
byte version;
8897
byte minorVersion;
@@ -92,21 +101,24 @@ struct __attribute__ ((packed)) ConfigCommand
92101

93102
short steerCenter; //2B
94103
short steerMax; //2B
95-
104+
short sensorThreshold; //2B
96105

97106
ConfigCommand()
98107
{
99108
id = CONFIG_CMD_ID;
100109
packetCount = 0;
110+
crc = 0x00;
101111

102112
steerCenter = 90;
103113
steerMax = 10;
114+
sensorThreshold = 900;
115+
104116
flags.steerCenterChanged = false;
105117
flags.steerMaxChanged = false;
106118
version = 0;
107119
minorVersion = 0;
108120
protocolVersion = 0;
109-
flags.enableGateSensor = false;
121+
flags.enableGateSensor = true;
110122
flags.gateSensorDebug = false;
111123
flags.calibrated = false;
112124
flags.invertSteering = false;
@@ -120,15 +132,42 @@ struct __attribute__ ((packed)) NameCommand
120132
{
121133

122134
char id;
123-
unsigned long packetCount;
135+
unsigned int packetCount;
136+
byte crc;
124137

125138
char name[12];
126139

127140
NameCommand()
128141
{
129142
id = NAME_CMD_ID;
130143
packetCount = 0;
144+
crc = 0x00;
145+
}
146+
147+
};
148+
typedef struct NameCommand NameCommand;
149+
150+
//With this msgpart this command request/contains the SHA signature id
151+
#define IDENTITY_GET_SIGNATURE_MSG_PART 10
152+
153+
struct __attribute__ ((packed)) IdentityCommand
154+
{
155+
156+
char id;
157+
unsigned int packetCount;
158+
byte crc;
159+
160+
byte msgPart;
161+
162+
char payload[12];
163+
164+
IdentityCommand()
165+
{
166+
id = IDENTITY_CMD_ID;
167+
packetCount = 0;
168+
crc = 0x00;
169+
msgPart = 0;
131170
}
132171

133172
};
134-
typedef struct NameCommand NameCommand;
173+
typedef struct IdentityCommand IdentityCommand;

0 commit comments

Comments
 (0)