1+ /*
2+ * Copyright (C) 2025 Ilia Kliantsevich <iliawork112005gmail.com>
3+ * This Source Code Form is subject to the terms of the Mozilla Public
4+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+ */
17
2- typedef struct dsdl_com_rl_vibration_Measurement VibrationMeasurement_t ;
8+ #ifndef VIBRATION_H_
9+ #define VIBRATION_H_
310
4- #define COM_RL_VIBRATION_MEASUREMENT_ID DSDL_COM_RL_VIBRATION_MEASUREMENT_ID
5- #define COM_RL_VIBRATION_MEASUREMENT_SIGNATURE DSDL_COM_RL_VIBRATION_MEASUREMENT_SIGNATURE
6- #define COM_RL_VIBRATION_MEASUREMENT_MAX_SIZE DSDL_COM_RL_VIBRATION_MEASUREMENT_MAX_SIZE
7-
8- #include "libdcnode/serialization/uavcan/include/dsdl.com.rl.vibration.Measurement.h"
11+ #include <stdint.h>
12+ #include <stdbool.h>
913#include "libdcnode/dronecan.h"
1014#include "libdcnode/serialization_internal.h"
1115
12- static inline int8_t com_rl_vibration_measurement_serialize (
13- const VibrationMeasurement_t * const obj ,
16+ /**
17+ * @brief uavcan.mpu.Vibration
18+ * @note Mpu vibration measurements based on FFT
19+ */
20+ #define VIBRATION_ID 20105
21+ #define VIBRATION_SIGNATURE 0xCB6C085C090925A8ULL // I used function signature.compute_signature from dronecan.dsdl
22+ #define VIBRATION_MESSAGE_SIZE 7 // 1 + 2 + 2 + 2 = 7 bytes
23+ #define VIBRATION UAVCAN_EXPAND(VIBRATION)
24+
25+ typedef struct {
26+ uint8_t id ; // uint8
27+ float vibration_metrics ; // float16
28+ float dominant_frequency ; // float16
29+ float dominant_snr ; // float16
30+ } Mpu_vibration ;
31+
32+ #ifdef __cplusplus
33+ extern "C" {
34+ #endif
35+
36+ static inline int8_t dronecan_mpu_vibration_serialize (
37+ const Mpu_vibration * const obj ,
1438 uint8_t * const buffer ,
1539 size_t * const inout_buffer_size_bytes )
1640{
17- if ((obj == NULL ) || (buffer == NULL ) || (inout_buffer_size_bytes == NULL ))
41+ if ((obj == NULL ) || (buffer == NULL ) || (inout_buffer_size_bytes == NULL )) {
1842 return -2 ;
19- if (* inout_buffer_size_bytes < COM_RL_VIBRATION_MEASUREMENT_MAX_SIZE )
43+ }
44+
45+ const size_t capacity_bytes = * inout_buffer_size_bytes ;
46+ if (capacity_bytes < VIBRATION_MESSAGE_SIZE ) {
2047 return -3 ;
21-
22- uint32_t byte_count = dsdl_com_rl_vibration_Measurement_encode (
23- (struct dsdl_com_rl_vibration_Measurement * )obj , buffer
24- #if CANARD_ENABLE_TAO_OPTION
25- , true
26- #endif
27- );
28-
29- * inout_buffer_size_bytes = byte_count ;
30- return 0 ;
31- }
48+ }
3249
50+ canardEncodeScalar (buffer , 0 , 8 , & obj -> id );
51+ canardEncodeFloat16 (buffer , 8 , obj -> vibration_metrics );
52+ canardEncodeFloat16 (buffer , 24 , obj -> dominant_frequency );
53+ canardEncodeFloat16 (buffer , 40 , obj -> dominant_snr );
3354
34- static inline int8_t com_rl_vibration_measurement_deserialize (
35- const CanardRxTransfer * transfer ,
36- VibrationMeasurement_t * msg )
37- {
38- if ((transfer == NULL ) || (msg == NULL ))
39- return -2 ;
40-
41- bool decode_failed = dsdl_com_rl_vibration_Measurement_decode (transfer , msg );
42- return decode_failed ? -1 : 0 ;
55+ return 0 ;
4356}
4457
45- // Publish function
46- static inline int8_t dronecan_com_rl_vibration_measurement_publish (
47- const VibrationMeasurement_t * const obj ,
58+ static inline int8_t dronecan_mpu_vibration_publish (
59+ const Mpu_vibration * const obj ,
4860 uint8_t * inout_transfer_id )
4961{
50- if ((obj == NULL ) || (inout_transfer_id == NULL ))
51- return -2 ;
52-
53- uint8_t buffer [COM_RL_VIBRATION_MEASUREMENT_MAX_SIZE ];
54- size_t buffer_size = COM_RL_VIBRATION_MEASUREMENT_MAX_SIZE ;
55-
56- int8_t res = com_rl_vibration_measurement_serialize (obj , buffer , & buffer_size );
57- if (res != 0 )
58- return res ;
59-
60- uavcanPublish (COM_RL_VIBRATION_MEASUREMENT_SIGNATURE ,
61- COM_RL_VIBRATION_MEASUREMENT_ID ,
62+ uint8_t buffer [VIBRATION_MESSAGE_SIZE ];
63+ size_t inout_buffer_size = VIBRATION_MESSAGE_SIZE ;
64+ dronecan_mpu_vibration_serialize (obj , buffer , & inout_buffer_size );
65+ uavcanPublish (VIBRATION_SIGNATURE ,
66+ VIBRATION_ID ,
6267 inout_transfer_id ,
6368 CANARD_TRANSFER_PRIORITY_MEDIUM ,
64- buffer , buffer_size );
69+ buffer ,
70+ VIBRATION_MESSAGE_SIZE );
71+
6572 return 0 ;
6673}
74+
75+ #ifdef __cplusplus
76+ }
77+ #endif
78+
79+ #endif // VIBRATION_H_
0 commit comments