# Page History

## Key

• This line was removed.
• Formatting was changed.

In this article we will turn Zubax Babel into a simple UAVCAN publisher. All the code here includes the code from the previous tutorials and extends it.

# Goal

Out Our goal is to publish a couple of different values to via UAVCAN and to check if everything works properly by plotting graphs in the UAVCAN GUI Tool's plotter utility.

# Implementation

We will are going to use the UAVCAN message uavcan.protocol.debug.KeyValue. The UAVCAN specification says says that float32 values can be broadcasted this way. We can use it to broadcast some custom sensor data, ADC data, or just any named value. For the sake of simplicity, in this tutorial we will broadcast sin sine values. But, assuming that the MCU resources are quite constrained, we will take the values from a lookup table. We will also broadcast the second value - – the current table index.

Code Block
title canard_publish
#define PUBLISHER_PERIOD_mS                            25
#define UAVCAN_PROTOCOL_DEBUG_KEYVALUE_ID              16370
#define UAVCAN_PROTOCOL_DEBUG_KEYVALUE_SIGNATURE       0xe02f25d6e0c98ae0
#define UAVCAN_PROTOCOL_DEBUG_KEYVALUE_MESSAGE_SIZE    62

void publishCanard(void)
{
static uint32_t publish_time = 0;
static int step = 0;
if(getUptime() < publish_time + PUBLISHER_PERIOD_mS) { return; } // to enter this// functionrate onlylimiting
once a period     publish_time = getUptime();

uint8_t buffer[UAVCAN_PROTOCOL_DEBUG_KEYVALUE_MESSAGE_SIZE];
memset(buffer,0x00,UAVCAN_PROTOCOL_DEBUG_KEYVALUE_MESSAGE_SIZE);
step++;
if (step == 256)
{
step = 0;
}

float val = sine_wave[step];
static uint8_t transfer_id = 0;
canardEncodeScalar(buffer, 0, 32, &val);
memcpy(&buffer[4], "sin", 3);
UAVCAN_PROTOCOL_DEBUG_KEYVALUE_SIGNATURE,
UAVCAN_PROTOCOL_DEBUG_KEYVALUE_ID,
&transfer_id,
CANARD_TRANSFER_PRIORITY_LOW,
&buffer[0],
7);
memset(buffer,0x00,UAVCAN_PROTOCOL_DEBUG_KEYVALUE_MESSAGE_SIZE);

val = step;
canardEncodeScalar(buffer, 0, 32, &val);
memcpy(&buffer[4], "stp", 3);
UAVCAN_PROTOCOL_DEBUG_KEYVALUE_SIGNATURE,
UAVCAN_PROTOCOL_DEBUG_KEYVALUE_ID,
&transfer_id,
CANARD_TRANSFER_PRIORITY_LOW,
&buffer[0],
7);
}

Important note. Integer and float values have very different bit-structure. As libcanard expects a float data type for uavcan.protocol.debug.KeyValue it , it is important to give it exactly what it wants - a float value. So, despite the fact that we have an unsigned integer (in fact even uint8_t) sinus typed sine table, it is important to provide provide the canardEncodeScalar function with with a float type parameter.

Now, let's add our publisher to main(), so now it looks like this:

Code Block
title main
int main(void)
{
/*!< At this stage the microcontrollersmicrocontroller's clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f37x.s) before branching to branchthe to application's main().
To reconfigure the default setting of SystemInit() function, refer to the
system_stm32f37x.c file */
RCC_GetClocksFreq(&RCC_Clocks); //To make sure RCC is initialised properly
hwInit();
swInit();
SysTick_Config(SystemCoreClock / 1000);
while(1)
{
sendCanard();
spinCanard();
publishCanard();
}
}

Now its it's time to flash the firmware to Babel and check if everything is working as expected. You should find a bunch of new messages in the Bus monitor panel:

Now lets go to let's open UAVCAN Plotter which may , which can be found in in the Tools -> Plotter menu.

Firstly lets , let's just plot every message of of type uavcan.protocol.debug.KeyValue type. For that, open the Plotter and hit that '+' button  button in bottom left corner of the window. Fill in the plot details as shown below and hit okOK:

Now you should see something like this:

note You
Tip
title Important
 note

You may adjust the scale of time and value axes by moving the mouse while holding the right button pressed.

The plot looks pretty weird because the plotter tries to display all values of of type debug.KeyValue type , and Babel transmitss transmits two diifferent values simultaneously: sinus the sine value and the step. Lets Let's delete this plot and add two more, one for each separate value:

Now the graphs look properly: