Skip to content

Commit 947d4ba

Browse files
authored
Merge pull request #3 from BeyondRobotix/1.1.1
parameter niceness, nodestatus and restart fix
2 parents ec28167 + 0fb74c6 commit 947d4ba

File tree

3 files changed

+99
-21
lines changed

3 files changed

+99
-21
lines changed

src/can.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,11 @@ void CANSend(const CanardCANFrame *CAN_tx_msg)
373373
// The mailbox don't becomes empty while loop
374374
if (CAN1->sTxMailBox[0].TIR & 0x1UL)
375375
{
376-
Serial.println("Send Fail");
377-
Serial.println(CAN1->ESR);
378-
Serial.println(CAN1->MSR);
376+
Serial.print("Send Fail ");
377+
Serial.print(CAN1->ESR);
378+
Serial.print(" ");
379+
Serial.print(CAN1->MSR);
380+
Serial.print(" ");
379381
Serial.println(CAN1->TSR);
380382
}
381383
}

src/dronecan.cpp

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
void DroneCAN::init(CanardOnTransferReception onTransferReceived,
44
CanardShouldAcceptTransfer shouldAcceptTransfer,
5-
const std::vector<parameter>& param_list)
5+
const std::vector<parameter>& param_list,
6+
const char* name)
67
{
78
CANInit(CAN_1000KBPS, 2);
89

10+
strncpy(this->node_name, name, sizeof(this->node_name));
11+
912
canardInit(&canard,
1013
memory_pool,
1114
sizeof(memory_pool),
@@ -31,15 +34,21 @@ void DroneCAN::init(CanardOnTransferReception onTransferReceived,
3134
this->read_parameter_memory();
3235
}
3336

37+
/*
38+
Gets the node id our DNA requests on init
39+
*/
3440
uint8_t DroneCAN::get_preferred_node_id()
3541
{
36-
if (parameters[0].value > 0 && parameters[0].value < 128)
42+
float ret = this->getParameter("NODEID");
43+
if (ret == __FLT_MIN__)
3744
{
38-
return (uint8_t)parameters[0].value;
45+
Serial.println("No NODEID in storage, setting..");
46+
this->setParameter("NODEID", PREFERRED_NODE_ID);
47+
return PREFERRED_NODE_ID;
3948
}
4049
else
4150
{
42-
return PREFERRED_NODE_ID;
51+
return (uint8_t)ret;
4352
}
4453
}
4554

@@ -108,18 +117,18 @@ void DroneCAN::handle_GetNodeInfo(CanardRxTransfer *transfer)
108117
pkt.status = node_status;
109118

110119
// fill in your major and minor firmware version
111-
pkt.software_version.major = 1;
112-
pkt.software_version.minor = 2;
120+
pkt.software_version.major = this->version_major;
121+
pkt.software_version.minor = this->version_minor;
113122
pkt.software_version.optional_field_flags = 0;
114123
pkt.software_version.vcs_commit = 0; // should put git hash in here
115124

116125
// should fill in hardware version
117-
pkt.hardware_version.major = 2;
118-
pkt.hardware_version.minor = 3;
126+
pkt.hardware_version.major = this->hardware_version_major;
127+
pkt.hardware_version.minor = this->hardware_version_minor;
119128

120129
getUniqueID(pkt.hardware_version.unique_id);
121130

122-
strncpy((char *)pkt.name.data, "Beyond Robotix Node", sizeof(pkt.name.data));
131+
strncpy((char *)pkt.name.data, this->node_name, sizeof(pkt.name.data));
123132
pkt.name.len = strnlen((char *)pkt.name.data, sizeof(pkt.name.data));
124133

125134
uint16_t total_size = uavcan_protocol_GetNodeInfoResponse_encode(&pkt, buffer);
@@ -300,6 +309,46 @@ void DroneCAN::read_parameter_memory()
300309
}
301310
}
302311

312+
/*
313+
Get a parameter from storage by name
314+
Only handles float return values
315+
returns __FLT_MIN__ if no parameter found with the provided name
316+
*/
317+
float DroneCAN::getParameter(const char *name)
318+
{
319+
for (size_t i = 0; i < parameters.size(); i++)
320+
{
321+
auto &p = parameters[i];
322+
if (strlen(name) == strlen(p.name) &&
323+
memcmp(name, p.name, strlen(name)) == 0)
324+
{
325+
return parameters[i].value;
326+
}
327+
}
328+
return __FLT_MIN__;
329+
}
330+
331+
/*
332+
Set a parameter from storage by name
333+
Values get stored as floats
334+
returns -1 if storage failed, 0 if good
335+
*/
336+
int DroneCAN::setParameter(const char *name, float value)
337+
{
338+
for (size_t i = 0; i < parameters.size(); i++)
339+
{
340+
auto &p = parameters[i];
341+
if (strlen(name) == strlen(p.name) &&
342+
memcmp(name, p.name, strlen(name)) == 0)
343+
{
344+
parameters[i].value = value;
345+
return 0;
346+
}
347+
}
348+
return -1;
349+
}
350+
351+
303352
/*
304353
handle a DNA allocation packet
305354
*/
@@ -335,7 +384,7 @@ int DroneCAN::handle_DNA_Allocation(CanardRxTransfer *transfer)
335384
// Matching the received UID against the local one
336385
if (memcmp(msg.unique_id.data, my_unique_id, msg.unique_id.len) != 0)
337386
{
338-
Serial.println("Mismatching allocation response\n");
387+
Serial.println("DNA failed this time");
339388
DNA.node_id_allocation_unique_id_offset = 0;
340389
// No match, return
341390
return 0;
@@ -348,9 +397,6 @@ int DroneCAN::handle_DNA_Allocation(CanardRxTransfer *transfer)
348397
// the next stage and updating the timeout.
349398
DNA.node_id_allocation_unique_id_offset = msg.unique_id.len;
350399
DNA.send_next_node_id_allocation_request_at_ms -= UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_MIN_REQUEST_PERIOD_MS;
351-
352-
Serial.print("Matching allocation response: ");
353-
Serial.println(msg.unique_id.len);
354400
}
355401
else
356402
{
@@ -390,7 +436,10 @@ void DroneCAN::request_DNA()
390436
// Structure of the request is documented in the DSDL definition
391437
// See http://uavcan.org/Specification/6._Application_level_functions/#dynamic-node-id-allocation
392438
uint8_t allocation_request[CANARD_CAN_FRAME_MAX_DATA_LEN - 1];
393-
allocation_request[0] = (uint8_t)(this->get_preferred_node_id() << 1U);
439+
uint8_t pref_node_id = (uint8_t)(this->get_preferred_node_id() << 1U);
440+
Serial.print("Requesting ID ");
441+
Serial.println(pref_node_id/2); // not sure why this is over 2 .. something to do with the bit shifting but this is what it actually sets
442+
allocation_request[0] = pref_node_id;
394443

395444
if (DNA.node_id_allocation_unique_id_offset == 0)
396445
{
@@ -711,9 +760,24 @@ void DroneCANonTransferReceived(DroneCAN &dronecan, CanardInstance *ins, CanardR
711760
}
712761
case UAVCAN_PROTOCOL_RESTARTNODE_ID:
713762
{
714-
while (1)
715-
{
716-
}
763+
764+
uavcan_protocol_RestartNodeResponse pkt{};
765+
pkt.ok = true;
766+
uint8_t buffer[UAVCAN_PROTOCOL_RESTARTNODE_RESPONSE_MAX_SIZE];
767+
uint32_t len = uavcan_protocol_RestartNodeResponse_encode(&pkt, buffer);
768+
static uint8_t transfer_id;
769+
canardBroadcast(ins,
770+
UAVCAN_PROTOCOL_RESTARTNODE_RESPONSE_SIGNATURE,
771+
UAVCAN_PROTOCOL_RESTARTNODE_RESPONSE_ID,
772+
&transfer_id,
773+
CANARD_TRANSFER_PRIORITY_LOW,
774+
buffer,
775+
len);
776+
777+
Serial.println("Reset..");
778+
delay(200);
779+
// yeeeeeet
780+
NVIC_SystemReset();
717781
}
718782
case UAVCAN_PROTOCOL_PARAM_GETSET_ID:
719783
{
@@ -770,6 +834,11 @@ bool DroneCANshoudlAcceptTransfer(const CanardInstance *ins,
770834
*out_data_type_signature = UAVCAN_PROTOCOL_FILE_READ_SIGNATURE;
771835
return true;
772836
}
837+
case UAVCAN_PROTOCOL_RESTARTNODE_ID:
838+
{
839+
*out_data_type_signature = UAVCAN_PROTOCOL_RESTARTNODE_ID;
840+
return true;
841+
}
773842
}
774843
}
775844

src/dronecan.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class DroneCAN
5757
parameters = param_list; // Vector automatically handles resizing
5858
}
5959

60-
void init(CanardOnTransferReception onTransferReceived, CanardShouldAcceptTransfer shouldAcceptTransfer, const std::vector<parameter>& param_list);
60+
void init(CanardOnTransferReception onTransferReceived, CanardShouldAcceptTransfer shouldAcceptTransfer, const std::vector<parameter>& param_list, const char* name);
6161
int node_id = 0;
6262

6363
CanardInstance canard;
@@ -79,6 +79,13 @@ class DroneCAN
7979
void processRx();
8080
void cycle();
8181
void debug(const char *msg, uint8_t level);
82+
float getParameter(const char* name);
83+
int setParameter(const char *name, float value);
84+
char node_name[80];
85+
int version_major=0;
86+
int version_minor=0;
87+
int hardware_version_major=0;
88+
int hardware_version_minor=0;
8289

8390
struct dynamic_node_allocation
8491
{

0 commit comments

Comments
 (0)