Converting double into custom data type (eg int14)

I'm currently working on a library which will handle the DroneCAN transmission. However, one of the data types which is the uavcan.equipment.esc.RawCommand are using int14 data types which makes it a problem as most byte packs library and even natively, Simulink does not support int14. You can find the definition of this data type from here. I've also included the definition for the RawCommand below:
#
# Raw ESC command normalized into [-8192, 8191]; negative values indicate reverse rotation.
# The ESC should normalize the setpoint into its effective input range.
# Non-zero setpoint value below minimum should be interpreted as min valid setpoint for the given motor.
#
int14[<=20] cmd
I was wondering how should one go about this and try to get it working?
I am also fully aware that I could probably use the S-function, but I could not find much guidance on how I could possibly do this.

5 个评论

But MATLAB does not support an int14 class. Can you use int16 instead?
So, just to give a brief context, the microcontroller which receives the CAN data is expecting CAN data of size 64 bit (8 byte DLC). The way it is configured is:
14 bit + 14 bit + 14 bit + 14 bit + 8 bit = 64 bit
where each 14 bit represents actuator command (e.g to a motor) and 8 bit representing the tail byte.
The thing that I am not entirely sure of using int16 is that by definition, it is still of size 16 bit, where if I compile 4 constant blocks defined as int16, it will give me 64 bit which basically fills the whole CAN data. This will also cause the microcontroller to interpret the CAN data wrongly, since I have no room in including the tail byte.
I've had a looked around the dronecan source code, and notice the following where it has actually been defined as int16. There is some offset calculation that has been done which is implemented here.
RawCommand struct definition
typedef struct
{
// FieldTypes
struct
{
uint8_t len; // Dynamic array length
int16_t* data; // Dynamic Array 14bit[20] max items
} cmd;
} uavcan_equipment_esc_RawCommand;
uint32_t uavcan_equipment_esc_RawCommand_encode_internal(uavcan_equipment_esc_RawCommand* source,
void* msg_buf,
uint32_t offset,
uint8_t CANARD_MAYBE_UNUSED(root_item))
{
uint32_t c = 0;
// Dynamic Array (cmd)
if (! root_item)
{
// - Add array length
canardEncodeScalar(msg_buf, offset, 5, (void*)&source->cmd.len);
offset += 5;
}
// - Add array items
for (c = 0; c < source->cmd.len; c++)
{
canardEncodeScalar(msg_buf,
offset,
14,
(void*)(source->cmd.data + c));// 8191
offset += 14;
}
return offset;
}
/**
* @brief uavcan_equipment_esc_RawCommand_encode
* @param source : Pointer to source data struct
* @param msg_buf: Pointer to msg storage
* @retval returns message length as bytes
*/
uint32_t uavcan_equipment_esc_RawCommand_encode(uavcan_equipment_esc_RawCommand* source, void* msg_buf)
{
uint32_t offset = 0;
offset = uavcan_equipment_esc_RawCommand_encode_internal(source, msg_buf, offset, 1);
return (offset + 7 ) / 8;
}
canard.h file can be found here.
I might implement something similar to this in Simulink as a workaround cause I dont think int14 actually exist.
Do you plan to read in the DSDL definition files and generate the Matlab code for it, or do you want to implement them by hand?
I was planning to simply implement it by hand since it is a lot faster to achieve my end goal for now. I may need to use the former approach if needed in the future, but I dont see any reason in spending time on it now

请先登录,再进行评论。

回答(1 个)

The embedded microcontroller or processing using the data can probably only work with 8-bit or 16-bit integers in doing calculations and storing in memory. There is no need to try to leave it in 14-bit format unless you need to send it back to the CAN network. So when you read this information from CAN, convert it all to 16-bit and perform calculations. Then convert it to 14-bit if you need to transmit it or store it.

类别

帮助中心File Exchange 中查找有关 Simulink Coder 的更多信息

产品

版本

R2020b

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by