mirror of https://github.com/rusefi/bldc.git
Added custom data comm example
This commit is contained in:
parent
dd297db4b1
commit
3f1bb63259
|
@ -0,0 +1,7 @@
|
|||
TARGET = example
|
||||
|
||||
SOURCES = code.c buffer.c
|
||||
|
||||
VESC_C_LIB_PATH=../../
|
||||
include $(VESC_C_LIB_PATH)rules.mk
|
||||
|
|
@ -0,0 +1,179 @@
|
|||
/*
|
||||
Copyright 2016 Benjamin Vedder benjamin@vedder.se
|
||||
|
||||
This file is part of the VESC firmware.
|
||||
|
||||
The VESC firmware is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
The VESC firmware is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "buffer.h"
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void buffer_append_int16(uint8_t* buffer, int16_t number, int32_t *index) {
|
||||
buffer[(*index)++] = number >> 8;
|
||||
buffer[(*index)++] = number;
|
||||
}
|
||||
|
||||
void buffer_append_uint16(uint8_t* buffer, uint16_t number, int32_t *index) {
|
||||
buffer[(*index)++] = number >> 8;
|
||||
buffer[(*index)++] = number;
|
||||
}
|
||||
|
||||
void buffer_append_int32(uint8_t* buffer, int32_t number, int32_t *index) {
|
||||
buffer[(*index)++] = number >> 24;
|
||||
buffer[(*index)++] = number >> 16;
|
||||
buffer[(*index)++] = number >> 8;
|
||||
buffer[(*index)++] = number;
|
||||
}
|
||||
|
||||
void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index) {
|
||||
buffer[(*index)++] = number >> 24;
|
||||
buffer[(*index)++] = number >> 16;
|
||||
buffer[(*index)++] = number >> 8;
|
||||
buffer[(*index)++] = number;
|
||||
}
|
||||
|
||||
void buffer_append_float16(uint8_t* buffer, float number, float scale, int32_t *index) {
|
||||
buffer_append_int16(buffer, (int16_t)(number * scale), index);
|
||||
}
|
||||
|
||||
void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *index) {
|
||||
buffer_append_int32(buffer, (int32_t)(number * scale), index);
|
||||
}
|
||||
|
||||
/*
|
||||
* See my question:
|
||||
* http://stackoverflow.com/questions/40416682/portable-way-to-serialize-float-as-32-bit-integer
|
||||
*
|
||||
* Regarding the float32_auto functions:
|
||||
*
|
||||
* Noticed that frexp and ldexp fit the format of the IEEE float representation, so
|
||||
* they should be quite fast. They are (more or less) equivalent with the following:
|
||||
*
|
||||
* float frexp_slow(float f, int *e) {
|
||||
* if (f == 0.0) {
|
||||
* *e = 0;
|
||||
* return 0.0;
|
||||
* }
|
||||
*
|
||||
* *e = ceilf(log2f(fabsf(f)));
|
||||
* float res = f / powf(2.0, (float)*e);
|
||||
*
|
||||
* if (res >= 1.0) {
|
||||
* res -= 0.5;
|
||||
* *e += 1;
|
||||
* }
|
||||
*
|
||||
* if (res <= -1.0) {
|
||||
* res += 0.5;
|
||||
* *e += 1;
|
||||
* }
|
||||
*
|
||||
* return res;
|
||||
* }
|
||||
*
|
||||
* float ldexp_slow(float f, int e) {
|
||||
* return f * powf(2.0, (float)e);
|
||||
* }
|
||||
*
|
||||
* 8388608.0 is 2^23, which scales the result to fit within 23 bits if sig_abs < 1.0.
|
||||
*
|
||||
* This should be a relatively fast and efficient way to serialize
|
||||
* floating point numbers in a fully defined manner.
|
||||
*/
|
||||
void buffer_append_float32_auto(uint8_t* buffer, float number, int32_t *index) {
|
||||
// Set subnormal numbers to 0 as they are not handled properly
|
||||
// using this method.
|
||||
if (fabsf(number) < 1.5e-38) {
|
||||
number = 0.0;
|
||||
}
|
||||
|
||||
int e = 0;
|
||||
float sig = frexpf(number, &e);
|
||||
float sig_abs = fabsf(sig);
|
||||
uint32_t sig_i = 0;
|
||||
|
||||
if (sig_abs >= 0.5) {
|
||||
sig_i = (uint32_t)((sig_abs - 0.5f) * 2.0f * 8388608.0f);
|
||||
e += 126;
|
||||
}
|
||||
|
||||
uint32_t res = ((e & 0xFF) << 23) | (sig_i & 0x7FFFFF);
|
||||
if (sig < 0) {
|
||||
res |= 1U << 31;
|
||||
}
|
||||
|
||||
buffer_append_uint32(buffer, res, index);
|
||||
}
|
||||
|
||||
int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index) {
|
||||
int16_t res = ((uint16_t) buffer[*index]) << 8 |
|
||||
((uint16_t) buffer[*index + 1]);
|
||||
*index += 2;
|
||||
return res;
|
||||
}
|
||||
|
||||
uint16_t buffer_get_uint16(const uint8_t *buffer, int32_t *index) {
|
||||
uint16_t res = ((uint16_t) buffer[*index]) << 8 |
|
||||
((uint16_t) buffer[*index + 1]);
|
||||
*index += 2;
|
||||
return res;
|
||||
}
|
||||
|
||||
int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index) {
|
||||
int32_t res = ((uint32_t) buffer[*index]) << 24 |
|
||||
((uint32_t) buffer[*index + 1]) << 16 |
|
||||
((uint32_t) buffer[*index + 2]) << 8 |
|
||||
((uint32_t) buffer[*index + 3]);
|
||||
*index += 4;
|
||||
return res;
|
||||
}
|
||||
|
||||
uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index) {
|
||||
uint32_t res = ((uint32_t) buffer[*index]) << 24 |
|
||||
((uint32_t) buffer[*index + 1]) << 16 |
|
||||
((uint32_t) buffer[*index + 2]) << 8 |
|
||||
((uint32_t) buffer[*index + 3]);
|
||||
*index += 4;
|
||||
return res;
|
||||
}
|
||||
|
||||
float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index) {
|
||||
return (float)buffer_get_int16(buffer, index) / scale;
|
||||
}
|
||||
|
||||
float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index) {
|
||||
return (float)buffer_get_int32(buffer, index) / scale;
|
||||
}
|
||||
|
||||
float buffer_get_float32_auto(const uint8_t *buffer, int32_t *index) {
|
||||
uint32_t res = buffer_get_uint32(buffer, index);
|
||||
|
||||
int e = (res >> 23) & 0xFF;
|
||||
uint32_t sig_i = res & 0x7FFFFF;
|
||||
bool neg = res & (1U << 31);
|
||||
|
||||
float sig = 0.0;
|
||||
if (e != 0 || sig_i != 0) {
|
||||
sig = (float)sig_i / (8388608.0 * 2.0) + 0.5;
|
||||
e -= 126;
|
||||
}
|
||||
|
||||
if (neg) {
|
||||
sig = -sig;
|
||||
}
|
||||
|
||||
return ldexpf(sig, e);
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Copyright 2016 Benjamin Vedder benjamin@vedder.se
|
||||
|
||||
This file is part of the VESC firmware.
|
||||
|
||||
The VESC firmware is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
The VESC firmware is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef BUFFER_H_
|
||||
#define BUFFER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void buffer_append_int16(uint8_t* buffer, int16_t number, int32_t *index);
|
||||
void buffer_append_uint16(uint8_t* buffer, uint16_t number, int32_t *index);
|
||||
void buffer_append_int32(uint8_t* buffer, int32_t number, int32_t *index);
|
||||
void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index);
|
||||
void buffer_append_float16(uint8_t* buffer, float number, float scale, int32_t *index);
|
||||
void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *index);
|
||||
void buffer_append_float32_auto(uint8_t* buffer, float number, int32_t *index);
|
||||
int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index);
|
||||
uint16_t buffer_get_uint16(const uint8_t *buffer, int32_t *index);
|
||||
int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index);
|
||||
uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index);
|
||||
float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index);
|
||||
float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index);
|
||||
float buffer_get_float32_auto(const uint8_t *buffer, int32_t *index);
|
||||
|
||||
#endif /* BUFFER_H_ */
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
Copyright 2022 Benjamin Vedder benjamin@vedder.se
|
||||
|
||||
This file is part of the VESC firmware.
|
||||
|
||||
The VESC firmware is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
The VESC firmware is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "vesc_c_if.h"
|
||||
#include "buffer.h"
|
||||
|
||||
HEADER
|
||||
|
||||
typedef struct {
|
||||
int msg_cnt;
|
||||
float msg_val;
|
||||
lib_thread thread;
|
||||
} my_data;
|
||||
|
||||
// This thread sends the message counter and the last received data
|
||||
// back every second. The Commands-signal onCustomAppDataReceived
|
||||
// is received every time VESC_IF->send_app_data is used.
|
||||
static void thd(void *arg) {
|
||||
volatile my_data *d = (my_data*)arg;
|
||||
|
||||
while (!VESC_IF->should_terminate()) {
|
||||
int32_t ind = 0;
|
||||
uint8_t buffer[10];
|
||||
buffer_append_int32(buffer, d->msg_cnt, &ind);
|
||||
buffer_append_float32_auto(buffer, d->msg_val, &ind);
|
||||
VESC_IF->send_app_data(buffer, ind);
|
||||
VESC_IF->sleep_ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
// This callback is called every time mCommands.sendCustomAppData is used
|
||||
// with the data it sends. The VESC buffer functions are compatible with
|
||||
// the JavaScript DataView, so we can even communicate floats as in this
|
||||
// example. Here we decode and save the data and increase a message
|
||||
// counter.
|
||||
static void data_rx_cb(unsigned char *data, unsigned int len) {
|
||||
volatile my_data *d = (my_data*)ARG;
|
||||
int32_t ind = 0;
|
||||
d->msg_val = buffer_get_float32_auto(data, &ind);
|
||||
d->msg_cnt++;
|
||||
VESC_IF->printf("Received %d bytes, number %.2f", len, (double)d->msg_val);
|
||||
}
|
||||
|
||||
// Called when code is stopped
|
||||
static void stop(void *arg) {
|
||||
my_data *d = (my_data*)arg;
|
||||
VESC_IF->request_terminate(d->thread);
|
||||
VESC_IF->set_app_data_handler(0); // Unregisted callback-function
|
||||
VESC_IF->printf("Terminated");
|
||||
VESC_IF->free(d);
|
||||
}
|
||||
|
||||
INIT_FUN(lib_info *info) {
|
||||
INIT_START
|
||||
|
||||
my_data *d = VESC_IF->malloc(sizeof(my_data));
|
||||
d->msg_val = 0.0;
|
||||
d->msg_cnt = 0.0;
|
||||
d->thread = VESC_IF->spawn(thd, 2048, "LibThd", d);
|
||||
|
||||
info->stop_fun = stop;
|
||||
info->arg = d;
|
||||
|
||||
// Register callback function that is used when app data is received
|
||||
VESC_IF->set_app_data_handler(data_rx_cb);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
Copyright 2022 Benjamin Vedder benjamin@vedder.se
|
||||
|
||||
This file is part of the VESC firmware.
|
||||
|
||||
The VESC firmware is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
The VESC firmware is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import QtQuick 2.5
|
||||
import QtQuick.Controls 2.2
|
||||
import QtQuick.Layouts 1.3
|
||||
|
||||
import Vedder.vesc.commands 1.0
|
||||
import Vedder.vesc.configparams 1.0
|
||||
|
||||
Item {
|
||||
id: container
|
||||
anchors.fill: parent
|
||||
anchors.margins: 10
|
||||
|
||||
property Commands mCommands: VescIf.commands()
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
|
||||
DoubleSpinBox {
|
||||
id: sb
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
|
||||
Item {
|
||||
Layout.fillHeight: true
|
||||
}
|
||||
}
|
||||
|
||||
// Send the value of the spinbox every second
|
||||
Timer {
|
||||
running: true
|
||||
repeat: true
|
||||
interval: 1000
|
||||
|
||||
onTriggered: {
|
||||
var buffer = new ArrayBuffer(4)
|
||||
var dv = new DataView(buffer)
|
||||
var ind = 0
|
||||
dv.setFloat32(ind, sb.realValue); ind += 4
|
||||
mCommands.sendCustomAppData(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
// Print the message counter and value that we sent the last time
|
||||
Connections {
|
||||
target: mCommands
|
||||
|
||||
onCustomAppDataReceived: {
|
||||
var dv = new DataView(data)
|
||||
var ind = 0
|
||||
var msg_cnt = dv.getInt32(ind); ind += 4
|
||||
var msg_val = dv.getFloat32(ind); ind += 4
|
||||
console.log(msg_cnt + " " + msg_val)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue