2
2
3
3
void DroneCAN::init (CanardOnTransferReception onTransferReceived,
4
4
CanardShouldAcceptTransfer shouldAcceptTransfer,
5
- const std::vector<parameter>& param_list)
5
+ const std::vector<parameter>& param_list,
6
+ const char * name)
6
7
{
7
8
CANInit (CAN_1000KBPS, 2 );
8
9
10
+ strncpy (this ->node_name , name, sizeof (this ->node_name ));
11
+
9
12
canardInit (&canard,
10
13
memory_pool,
11
14
sizeof (memory_pool),
@@ -31,15 +34,21 @@ void DroneCAN::init(CanardOnTransferReception onTransferReceived,
31
34
this ->read_parameter_memory ();
32
35
}
33
36
37
+ /*
38
+ Gets the node id our DNA requests on init
39
+ */
34
40
uint8_t DroneCAN::get_preferred_node_id ()
35
41
{
36
- if (parameters[0 ].value > 0 && parameters[0 ].value < 128 )
42
+ float ret = this ->getParameter (" NODEID" );
43
+ if (ret == __FLT_MIN__)
37
44
{
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;
39
48
}
40
49
else
41
50
{
42
- return PREFERRED_NODE_ID ;
51
+ return ( uint8_t )ret ;
43
52
}
44
53
}
45
54
@@ -108,18 +117,18 @@ void DroneCAN::handle_GetNodeInfo(CanardRxTransfer *transfer)
108
117
pkt.status = node_status;
109
118
110
119
// 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 ;
113
122
pkt.software_version .optional_field_flags = 0 ;
114
123
pkt.software_version .vcs_commit = 0 ; // should put git hash in here
115
124
116
125
// 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 ;
119
128
120
129
getUniqueID (pkt.hardware_version .unique_id );
121
130
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 ));
123
132
pkt.name .len = strnlen ((char *)pkt.name .data , sizeof (pkt.name .data ));
124
133
125
134
uint16_t total_size = uavcan_protocol_GetNodeInfoResponse_encode (&pkt, buffer);
@@ -300,6 +309,46 @@ void DroneCAN::read_parameter_memory()
300
309
}
301
310
}
302
311
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
+
303
352
/*
304
353
handle a DNA allocation packet
305
354
*/
@@ -335,7 +384,7 @@ int DroneCAN::handle_DNA_Allocation(CanardRxTransfer *transfer)
335
384
// Matching the received UID against the local one
336
385
if (memcmp (msg.unique_id .data , my_unique_id, msg.unique_id .len ) != 0 )
337
386
{
338
- Serial.println (" Mismatching allocation response \n " );
387
+ Serial.println (" DNA failed this time " );
339
388
DNA.node_id_allocation_unique_id_offset = 0 ;
340
389
// No match, return
341
390
return 0 ;
@@ -348,9 +397,6 @@ int DroneCAN::handle_DNA_Allocation(CanardRxTransfer *transfer)
348
397
// the next stage and updating the timeout.
349
398
DNA.node_id_allocation_unique_id_offset = msg.unique_id .len ;
350
399
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 );
354
400
}
355
401
else
356
402
{
@@ -390,7 +436,10 @@ void DroneCAN::request_DNA()
390
436
// Structure of the request is documented in the DSDL definition
391
437
// See http://uavcan.org/Specification/6._Application_level_functions/#dynamic-node-id-allocation
392
438
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;
394
443
395
444
if (DNA.node_id_allocation_unique_id_offset == 0 )
396
445
{
@@ -711,9 +760,24 @@ void DroneCANonTransferReceived(DroneCAN &dronecan, CanardInstance *ins, CanardR
711
760
}
712
761
case UAVCAN_PROTOCOL_RESTARTNODE_ID:
713
762
{
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 ();
717
781
}
718
782
case UAVCAN_PROTOCOL_PARAM_GETSET_ID:
719
783
{
@@ -770,6 +834,11 @@ bool DroneCANshoudlAcceptTransfer(const CanardInstance *ins,
770
834
*out_data_type_signature = UAVCAN_PROTOCOL_FILE_READ_SIGNATURE;
771
835
return true ;
772
836
}
837
+ case UAVCAN_PROTOCOL_RESTARTNODE_ID:
838
+ {
839
+ *out_data_type_signature = UAVCAN_PROTOCOL_RESTARTNODE_ID;
840
+ return true ;
841
+ }
773
842
}
774
843
}
775
844
0 commit comments