├── synchronized_lighting.jpg
├── .gitignore
├── synchronized_lighting_new.zip
├── lib
├── bldc_uart
│ ├── crc.h
│ ├── bldc_interface_uart.h
│ ├── vesc_uart.h
│ ├── arduino_uart_wrapper.h
│ ├── packet.h
│ ├── arduino_uart_wrapper.cpp
│ ├── buffer.h
│ ├── bldc_interface_uart.c
│ ├── crc.c
│ ├── bldc_interface.h
│ ├── vesc_uart.c
│ ├── packet.c
│ ├── buffer.c
│ ├── datatypes.h
│ └── bldc_interface.c
├── colors_conversions
│ ├── hsv_to_rgb.h
│ └── hsv_to_rgb.cpp
├── vesc_standby_lights
│ ├── generate_random_hue.h
│ ├── breathing_lights.h
│ ├── generate_random_hue.cpp
│ ├── cie1931.h
│ └── breathing_lights.cpp
├── color_shifting_logic
│ ├── color_shifting.h
│ └── color_shifting.cpp
├── led_strip_apa102
│ ├── led_strip_apa102.cpp
│ └── led_strip_apa102.h
└── color_palletes
│ ├── color_palletes.cpp
│ └── color_palletes.h
├── src
└── main.cpp
├── README.md
└── LICENSE
/synchronized_lighting.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fadh3r/synchronized_lighting_for_vesc/HEAD/synchronized_lighting.jpg
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .pioenvs
2 | .piolibdeps
3 | .clang_complete
4 | .gcc-flags.json
5 | platformio.ini
6 | .travis.yml
7 | readme.txt
8 |
--------------------------------------------------------------------------------
/synchronized_lighting_new.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fadh3r/synchronized_lighting_for_vesc/HEAD/synchronized_lighting_new.zip
--------------------------------------------------------------------------------
/lib/bldc_uart/crc.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Benjamin Vedder benjamin@vedder.se
3 |
4 | This file is part of the VESC firmware.
5 |
6 | The VESC firmware is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | The VESC firmware is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #ifndef CRC_H_
21 | #define CRC_H_
22 |
23 | /*
24 | * Functions
25 | */
26 | unsigned short crc16(unsigned char *buf, unsigned int len);
27 |
28 | #endif /* CRC_H_ */
29 |
--------------------------------------------------------------------------------
/lib/colors_conversions/hsv_to_rgb.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #ifndef HSV_TO_RGB_H
20 | #define HSV_TO_RGB_H
21 |
22 | #include
23 |
24 | rgb_color hsvToRgb(uint16_t h, uint8_t s, uint8_t v);
25 |
26 | #endif /* HSV_TO_RGB_H */
27 |
--------------------------------------------------------------------------------
/lib/vesc_standby_lights/generate_random_hue.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #ifndef GENERATE_RANDOM_HUE
20 | #define GENERATE_RANDOM_HUE
21 |
22 | #include
23 |
24 | int16_t generate_random_hue(void);
25 |
26 | #endif /* GENERATE_RANDOM_HUE */
27 |
--------------------------------------------------------------------------------
/lib/vesc_standby_lights/breathing_lights.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #ifndef BREATHING_LIGHTS_H
20 | #define BREATHING_LIGHTS_H
21 |
22 | extern const uint16_t STANDBY_TIMEOUT;
23 |
24 | void vesc_standby_lights(void);
25 | bool vesc_standby(void);
26 |
27 | #endif /* BREATHING_LIGHTS_H */
28 |
--------------------------------------------------------------------------------
/lib/color_shifting_logic/color_shifting.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #ifndef COLOR_SHIFTING_H
20 | #define COLOR_SHIFTING_H
21 |
22 | #include
23 |
24 | extern const uint8_t WHEEL_SIZE_RATIO;
25 | extern const uint8_t STRIP_DIRECTION;
26 |
27 | void update_strip_colors(void);
28 |
29 |
30 | #endif /* COLOR_SHIFTING_H */
31 |
--------------------------------------------------------------------------------
/lib/led_strip_apa102/led_strip_apa102.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include "led_strip_apa102.h"
20 |
21 | // Create an object for writing to LED strip.
22 | APA102 led_strip;
23 | // Create a buffer for holding colors (3 bytes per color).
24 | rgb_color led_strip_colors[LED_COUNT];
25 |
26 |
27 | void update_strip() {
28 | led_strip.write(led_strip_colors, LED_COUNT, LED_STRIP_BRIGHTNESS);
29 | }
30 |
--------------------------------------------------------------------------------
/lib/vesc_standby_lights/generate_random_hue.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include
20 | #include "generate_random_hue.h"
21 |
22 | int16_t recent_random_color;
23 |
24 | int16_t generate_random_hue(void) {
25 | int16_t random_hue;
26 | while(1) {
27 | random_hue = random(360);
28 | if (abs(recent_random_color - random_hue) > 60) {
29 | recent_random_color = random_hue;
30 | return random_hue;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/lib/bldc_uart/bldc_interface_uart.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2015 Benjamin Vedder benjamin@vedder.se
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * bldc_interface_uart.h
20 | *
21 | * Created on: 9 okt 2015
22 | * Author: benjamin
23 | */
24 |
25 | #ifndef BLDC_INTERFACE_UART_H_
26 | #define BLDC_INTERFACE_UART_H_
27 |
28 | // Includes
29 | #include "packet.h" // For the MAX_PACKET_LEN define
30 |
31 | // Functions
32 | void bldc_interface_uart_init(void(*func)(unsigned char *data, unsigned int len));
33 | void bldc_interface_uart_process_byte(unsigned char b);
34 | void bldc_interface_uart_run_timer(void);
35 |
36 | #endif /* BLDC_INTERFACE_UART_H_ */
37 |
--------------------------------------------------------------------------------
/lib/color_palletes/color_palletes.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include "color_palletes.h"
20 | #include "led_strip_apa102.h"
21 | #include "hsv_to_rgb.h"
22 |
23 |
24 | rgb_color get_pallete_color(uint32_t position) {
25 | uint16_t hue;
26 | rgb_color rgb_return_val;
27 | if (position != 0)
28 | position = position % CURRENT_PALLETE_WIDTH;
29 | hue = pgm_read_word_near(CURRENT_PALLETE + position);
30 | rgb_return_val = hsvToRgb(hue, 255, 255);
31 | return rgb_return_val;
32 | }
33 |
--------------------------------------------------------------------------------
/lib/bldc_uart/vesc_uart.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #ifndef VESC_UART_H
20 | #define VESC_UART_H
21 |
22 | //declare global "mc_values_ptr" pointer of type "mc_values" for received from VESC data
23 | //dox - https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files
24 | #include "bldc_interface.h"
25 | extern mc_values *mc_values_ptr;
26 |
27 | void bldc_get_values(void);
28 | void comm_uart_init(void);
29 | long unsigned int bldc_get_tachometer_value(void);
30 |
31 | #endif /* VESC_UART_H */
32 |
--------------------------------------------------------------------------------
/lib/bldc_uart/arduino_uart_wrapper.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #ifndef ARDUINO_UART_H
20 | #define ARDUINO_UART_H
21 |
22 | #ifdef __cplusplus
23 | extern "C" {
24 | #endif
25 | void serial_begin(long speed);
26 | int serial_available(void);
27 | int serial_write(unsigned char *data, unsigned int length);
28 | int serial_read(void);
29 | unsigned int serial_print(char *data);
30 | unsigned int serial_println(char *data);
31 | unsigned int serial_println_int(int data);
32 | void do_delay(unsigned long time);
33 | #ifdef __cplusplus
34 | }
35 | #endif
36 |
37 | #endif/* ARDUINO_UART_H */
38 |
--------------------------------------------------------------------------------
/lib/led_strip_apa102/led_strip_apa102.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #ifndef LED_STRIP_APA102_H
20 | #define LED_STRIP_APA102_H
21 |
22 | #include "stdint.h"
23 | #include
24 | #define APA102_USE_FAST_GPIO
25 | #include
26 |
27 | /* define here Arduino pins connected to APA102 strip */
28 | #define DATA_PIN 11
29 | #define CLOCK_PIN 12
30 | /* define here number of leds on strip */
31 | #define LED_COUNT 42
32 | /* define APA102 brightness - 31 max */
33 | #define LED_STRIP_BRIGHTNESS 31
34 |
35 | extern rgb_color led_strip_colors[];
36 |
37 | void update_strip(void);
38 |
39 | #endif /* LED_STRIP_APA102_H */
40 |
--------------------------------------------------------------------------------
/lib/bldc_uart/packet.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Benjamin Vedder benjamin@vedder.se
3 |
4 | This file is part of the VESC firmware.
5 |
6 | The VESC firmware is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | The VESC firmware is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #ifndef PACKET_H_
21 | #define PACKET_H_
22 |
23 | #include
24 |
25 | // Settings
26 | #define PACKET_RX_TIMEOUT 1000
27 | // #define PACKET_HANDLERS 2
28 | #define PACKET_HANDLERS 1
29 | // #define PACKET_MAX_PL_LEN 1024
30 | #define PACKET_MAX_PL_LEN 64
31 |
32 |
33 | // Functions
34 | void packet_init(void (*s_func)(unsigned char *data, unsigned int len),
35 | void (*p_func)(unsigned char *data, unsigned int len), int handler_num);
36 | void packet_process_byte(uint8_t rx_data, int handler_num);
37 | void packet_timerfunc(void);
38 | void packet_send_packet(unsigned char *data, unsigned int len, int handler_num);
39 |
40 | #endif /* PACKET_H_ */
41 |
--------------------------------------------------------------------------------
/lib/bldc_uart/arduino_uart_wrapper.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include
20 | #include "arduino_uart_wrapper.h"
21 |
22 |
23 | //https://www.arduino.cc/reference/en/language/functions/communication/serial/read/
24 | //https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.h
25 |
26 | void serial_begin(long speed) {
27 | return Serial.begin(speed);
28 | }
29 |
30 | int serial_available(void) {
31 | return Serial.available();
32 | }
33 |
34 | int serial_write(unsigned char *data, unsigned int length) {
35 | return Serial.write(data, length);
36 | }
37 |
38 |
39 | int serial_read(void) {
40 | return Serial.read();
41 | }
42 |
43 | unsigned int serial_print(char *data) {
44 | return Serial.print(data);
45 | }
46 |
47 | unsigned int serial_println(char *data) {
48 | return Serial.println(data);
49 | }
50 |
51 | void do_delay(unsigned long time) {
52 | return delay(time);
53 | }
54 |
--------------------------------------------------------------------------------
/lib/colors_conversions/hsv_to_rgb.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include
20 | /* Converts a color from HSV to RGB.
21 | * h is hue, as a number between 0 and 360.
22 | * s is the saturation, as a number between 0 and 255.
23 | * v is the value, as a number between 0 and 255. */
24 | /* This function from APA102 Arduino examples library */
25 | rgb_color hsvToRgb(uint16_t h, uint8_t s, uint8_t v) {
26 | uint8_t f = (h % 60) * 255 / 60;
27 | uint8_t p = (255 - s) * (uint16_t)v / 255;
28 | uint8_t q = (255 - f * (uint16_t)s / 255) * (uint16_t)v / 255;
29 | uint8_t t = (255 - (255 - f) * (uint16_t)s / 255) * (uint16_t)v / 255;
30 | uint8_t r = 0, g = 0, b = 0;
31 | switch((h / 60) % 6){
32 | case 0: r = v; g = t; b = p; break;
33 | case 1: r = q; g = v; b = p; break;
34 | case 2: r = p; g = v; b = t; break;
35 | case 3: r = p; g = q; b = v; break;
36 | case 4: r = t; g = p; b = v; break;
37 | case 5: r = v; g = p; b = q; break;
38 | }
39 | return rgb_color(r, g, b);
40 | }
41 |
--------------------------------------------------------------------------------
/src/main.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include
20 | #include
21 | #include "breathing_lights.h"
22 | #include "color_shifting.h"
23 |
24 | // set "STRIP_DIRECTION" to zero if you want to reverse color movement
25 | const uint8_t STRIP_DIRECTION = 1;
26 | // tune "WHEEL_SIZE_RATIO" for velocity of color_pallete change
27 | const uint8_t WHEEL_SIZE_RATIO = 20;
28 | // time in standby before breating lights
29 | const uint16_t STANDBY_TIMEOUT = 5000;
30 |
31 | /* extern "C" - allows to use C header files with C++ code
32 | for more info see - https://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c */
33 | extern "C" {
34 | #include "vesc_uart.h"
35 | };
36 |
37 |
38 | // SETUP
39 | void setup() {
40 | // UART Initialize
41 | comm_uart_init();
42 | // delay for vesc startup
43 | delay(2000);
44 |
45 | }// END-OF-SETUP
46 |
47 |
48 | void loop() {
49 | // get values from VESC
50 | bldc_get_values();
51 | // bldc_interface_setters works only with disconnected PPM
52 | //bldc_interface_set_current(2.00);
53 | //bldc_interface_set_rpm(1000);
54 |
55 | if (vesc_standby())
56 | vesc_standby_lights();
57 | else
58 | update_strip_colors();
59 |
60 | }// end-of-loop
61 |
--------------------------------------------------------------------------------
/lib/color_shifting_logic/color_shifting.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include "color_shifting.h"
20 | #include "led_strip_apa102.h"
21 | #include "color_palletes.h"
22 |
23 |
24 | /* extern "C" - allows to use C header files with C++ code
25 | for more info see - https://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c */
26 | extern "C" {
27 | #include "vesc_uart.h"
28 | };
29 |
30 |
31 | void update_strip_colors_forward(void) {
32 | uint32_t current_tacho_value;
33 | uint32_t pallete_travel;
34 | current_tacho_value = bldc_get_tachometer_value();
35 | pallete_travel = current_tacho_value / WHEEL_SIZE_RATIO;
36 | for (uint8_t i; i < LED_COUNT; i++) {
37 | led_strip_colors[i] = get_pallete_color(pallete_travel + i);
38 | }
39 | }
40 |
41 |
42 | void update_strip_colors_reverse(void) {
43 | uint32_t current_tacho_value;
44 | uint32_t pallete_travel;
45 | current_tacho_value = bldc_get_tachometer_value();
46 | pallete_travel = current_tacho_value / WHEEL_SIZE_RATIO;
47 | for (uint8_t i; i < LED_COUNT; i++) {
48 | led_strip_colors[i] = get_pallete_color(pallete_travel - i);
49 | }
50 | }
51 |
52 |
53 | void update_strip_colors(void) {
54 | if (STRIP_DIRECTION == 1)
55 | update_strip_colors_forward();
56 | else
57 | update_strip_colors_reverse();
58 | update_strip();
59 | }
60 |
--------------------------------------------------------------------------------
/lib/vesc_standby_lights/cie1931.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | // CIE1931 correction table
20 | // Automatically generated
21 |
22 | const unsigned char cie[256] = {
23 | 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
24 | 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
25 | 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
26 | 3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
27 | 5, 5, 6, 6, 6, 6, 6, 7, 7, 7,
28 | 7, 8, 8, 8, 8, 9, 9, 9, 10, 10,
29 | 10, 10, 11, 11, 11, 12, 12, 12, 13, 13,
30 | 13, 14, 14, 15, 15, 15, 16, 16, 17, 17,
31 | 17, 18, 18, 19, 19, 20, 20, 21, 21, 22,
32 | 22, 23, 23, 24, 24, 25, 25, 26, 26, 27,
33 | 28, 28, 29, 29, 30, 31, 31, 32, 32, 33,
34 | 34, 34, 35, 36, 37, 37, 38, 39, 39, 40,
35 | 41, 42, 43, 43, 44, 45, 46, 47, 47, 48,
36 | 49, 50, 51, 52, 53, 54, 54, 55, 56, 57,
37 | 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
38 | 68, 70, 71, 72, 73, 74, 75, 76, 77, 79,
39 | 80, 81, 82, 83, 85, 86, 87, 88, 90, 91,
40 | 92, 94, 95, 96, 98, 99, 100, 102, 103, 105,
41 | 106, 108, 109, 110, 112, 113, 115, 116, 118, 120,
42 | 121, 123, 124, 126, 128, 129, 131, 132, 134, 136,
43 | 138, 139, 141, 143, 145, 146, 148, 150, 152, 154,
44 | 155, 157, 159, 161, 163, 165, 167, 169, 171, 173,
45 | 175, 177, 179, 181, 183, 185, 187, 189, 191, 193,
46 | 196, 198, 200, 202, 204, 207, 209, 211, 214, 216,
47 | 218, 220, 223, 225, 228, 230, 232, 235, 237, 240,
48 | 242, 245, 247, 250, 252, 255,
49 | };
50 |
--------------------------------------------------------------------------------
/lib/bldc_uart/buffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2012-2014 Benjamin Vedder benjamin@vedder.se
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * buffer.h
20 | *
21 | * Created on: 13 maj 2013
22 | * Author: benjamin
23 | */
24 |
25 | #ifndef BUFFER_H_
26 | #define BUFFER_H_
27 |
28 | #include
29 |
30 | void buffer_append_int16(uint8_t* buffer, int16_t number, int32_t *index);
31 | void buffer_append_uint16(uint8_t* buffer, uint16_t number, int32_t *index);
32 | void buffer_append_int32(uint8_t* buffer, int32_t number, int32_t *index);
33 | void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index);
34 | void buffer_append_int64(uint8_t* buffer, int64_t number, int32_t *index);
35 | void buffer_append_uint64(uint8_t* buffer, uint64_t number, int32_t *index);
36 | void buffer_append_float16(uint8_t* buffer, float number, float scale, int32_t *index);
37 | void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *index);
38 | void buffer_append_double64(uint8_t* buffer, double number, double scale, int32_t *index);
39 | void buffer_append_float32_auto(uint8_t* buffer, float number, int32_t *index);
40 | int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index);
41 | uint16_t buffer_get_uint16(const uint8_t *buffer, int32_t *index);
42 | int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index);
43 | uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index);
44 | int64_t buffer_get_int64(const uint8_t *buffer, int32_t *index);
45 | uint64_t buffer_get_uint64(const uint8_t *buffer, int32_t *index);
46 | float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index);
47 | float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index);
48 | double buffer_get_double64(const uint8_t *buffer, double scale, int32_t *index);
49 | float buffer_get_float32_auto(const uint8_t *buffer, int32_t *index);
50 |
51 | #endif /* BUFFER_H_ */
52 |
--------------------------------------------------------------------------------
/lib/bldc_uart/bldc_interface_uart.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2015 Benjamin Vedder benjamin@vedder.se
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * bldc_interface_uart.c
20 | *
21 | * Created on: 9 okt 2015
22 | * Author: benjamin
23 | */
24 |
25 | #include "bldc_interface_uart.h"
26 | // #include "ch.h"
27 | // #include "hal.h"
28 | #include "bldc_interface.h"
29 |
30 | // Settings
31 | #define PACKET_HANDLER 0
32 |
33 | // Private functions
34 | static void process_packet(unsigned char *data, unsigned int len);
35 | static void send_packet_bldc_interface(unsigned char *data, unsigned int len);
36 |
37 | /**
38 | * Initialize the UART BLDC interface and provide a function to be used for
39 | * sending packets.
40 | *
41 | * @param func
42 | * Function provided for sending packets.
43 | */
44 | void bldc_interface_uart_init(void(*func)(unsigned char *data, unsigned int len)) {
45 | // Initialize packet handler
46 | packet_init(func, process_packet, PACKET_HANDLER);
47 |
48 | // Initialize the bldc interface and provide a send function
49 | bldc_interface_init(send_packet_bldc_interface);
50 | }
51 |
52 | /**
53 | * Process one byte received on the UART. Once a full packet is received the
54 | * corresponding callback will be called by bldc_interface.
55 | *
56 | * @param b
57 | * The byte received on the UART to process.
58 | */
59 | void bldc_interface_uart_process_byte(unsigned char b) {
60 | packet_process_byte(b, PACKET_HANDLER);
61 | }
62 |
63 | /**
64 | * Call this function at around 1 khz to reset the state of the packet
65 | * interface after a timeout in case data is lost.
66 | */
67 | void bldc_interface_uart_run_timer(void) {
68 | packet_timerfunc();
69 | }
70 |
71 | /**
72 | * Callback for the packet handled for when a whole packet is received,
73 | * assembled and checked.
74 | *
75 | * @param data
76 | * Data array pointer
77 | * @param len
78 | * Data array length
79 | */
80 | static void process_packet(unsigned char *data, unsigned int len) {
81 | // Let bldc_interface process the packet.
82 | bldc_interface_process_packet(data, len);
83 | }
84 |
85 | /**
86 | * Callback that bldc_interface uses to send packets.
87 | *
88 | * @param data
89 | * Data array pointer
90 | * @param len
91 | * Data array length
92 | */
93 | static void send_packet_bldc_interface(unsigned char *data, unsigned int len) {
94 | // Pass the packet to the packet handler to add checksum, length, start and stop bytes.
95 | packet_send_packet(data, len, PACKET_HANDLER);
96 | }
97 |
--------------------------------------------------------------------------------
/lib/bldc_uart/crc.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Benjamin Vedder benjamin@vedder.se
3 |
4 | This file is part of the VESC firmware.
5 |
6 | The VESC firmware is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | The VESC firmware is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include "crc.h"
21 |
22 | // CRC Table
23 | const unsigned short crc16_tab[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084,
24 | 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad,
25 | 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7,
26 | 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
27 | 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a,
28 | 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672,
29 | 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719,
30 | 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7,
31 | 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948,
32 | 0x9969, 0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50,
33 | 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b,
34 | 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
35 | 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97,
36 | 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe,
37 | 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca,
38 | 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3,
39 | 0x5004, 0x4025, 0x7046, 0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d,
40 | 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214,
41 | 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c,
42 | 0xc50d, 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
43 | 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3,
44 | 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d,
45 | 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806,
46 | 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e,
47 | 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1,
48 | 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b,
49 | 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0,
50 | 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
51 | 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 };
52 |
53 | unsigned short crc16(unsigned char *buf, unsigned int len) {
54 | unsigned int i;
55 | unsigned short cksum = 0;
56 | for (i = 0; i < len; i++) {
57 | cksum = crc16_tab[(((cksum >> 8) ^ *buf++) & 0xFF)] ^ (cksum << 8);
58 | }
59 | return cksum;
60 | }
61 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Synchronized Lighting for VESC #
2 |
3 | * The idea is to lightup the underbody of a skateboard in accordance with the movement - for example, like you skating the rainbow ;)
4 |
5 | * You can choose from existing options and and also create your own lighting color palletes.
6 |
7 | * While standby - "breathing lights" mode enabed.
8 |
9 | **click to watch video**
10 | [](https://youtu.be/jkwX-VA8xU0)
11 |
12 |
13 | ## This project is easy to repeat - all you need is:
14 |
15 | * [Arduino Nano](https://aliexpress.com/item/Nano-V3-ATmega328-CH340G-Micro-USB-Pin-headers-NOT-soldered-Compatible-for-Arduino-Nano-V3-0/32664577152.html) - brains for project
16 |
17 | * [APA102](https://aliexpress.com/item/1m-5m-APA102-Smart-LED-Pixel-Strip-30-60-144-LEDs-Pixels-m-IP30-IP65-IP67/32780224340.html) - led strip
18 |
19 | * [LM2596HVS](https://aliexpress.com/item/DC-DC-Converter-Adjustable-Power-Supply-DC-DC-Step-Down-3A-LM2596HVS-LM2596HV-DC-Step-Down/32485142548.html) - DC-DC step down converter
20 |
21 | **don't forget to set stepdown converter to +5volts**
22 | 
23 |
24 |
25 | ## Tuning your VESC Synchronized Lighting
26 |
27 | 1. Set strip length(led num) and strip brightness(1-31 max) in **"lib/led_strip_apa102/led_strip_apa102.h"** file:
28 | ```
29 | #define LED_COUNT 42
30 | #define LED_STRIP_BRIGHTNESS 31
31 | ```
32 |
33 | 2. Tune "WHEEL_SIZE_RATIO" constant in **"main.cpp"** for velocity of color pallete change
34 | ```
35 | const uint8_t WHEEL_SIZE_RATIO = 20;
36 | ```
37 |
38 | 3. Also you can tune "STANDBY_TIMEOUT" constant in order to enable standby lighting mode
39 | ```
40 | const uint16_t STANDBY_TIMEOUT = 3000;
41 | ```
42 |
43 | 4. Select **"PPM and UART"** in **"APP setting"** of **"VESC tool"**.
44 |
45 | 5. You are ready to go!
46 |
47 | 6. You can create your own color palletes and set it up in **"lib/color_palletes/color_palletes.h"** file.
48 |
49 | **CURRENT_PALLETE_WIDTH** must be equal to pallete elements, for example, if you want to create new pallete with 3 elements:
50 | ```
51 | const hsv_color pallete_name[3] PROGMEM = { 0, 240, 0 };
52 | ```
53 | or
54 | ```
55 | const hsv_color another_pallete_name[6] PROGMEM = { 0, 240, 0, 240, 0, 240, };
56 | ```
57 | so **CURRENT_PALLETE_WIDTH** must be equal to number in **[ ]** brackets and count of values in **{ }** brackets.
58 |
59 | 7. If you want to change direction of color movement tune "STRIP_DIRECTION" constant in **"main.cpp"**.
60 |
61 |
62 | ## To compile this project in Arduino IDE:
63 |
64 | 1. Create new folder for this project. For example name it **synchronized_lighting**.
65 |
66 | 2. Copy main.cpp to this folder.
67 |
68 | 3. Rename **main.cpp** to **synchronized_lighting.ino**
69 |
70 | 4. Copy all files from **lib** folder and subfolders to your newly created directory **synchronized_lighting** folder.
71 |
72 | 5. Open Arduino IDE and install **FastGPIO** and **APA102** libraries.
73 |
74 | 6. Compile!
75 |
76 | 7. **Power down or even disconnect Arduino from your VESC when uploading new firmware.**
77 |
78 |
79 | ### If you use this project and like it - Please Donate
80 |
81 | [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=LHTUV26X68QS6)
82 |
83 |
--------------------------------------------------------------------------------
/lib/vesc_standby_lights/breathing_lights.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include
20 | #include "breathing_lights.h"
21 | #include "led_strip_apa102.h"
22 | #include "generate_random_hue.h"
23 | #include "hsv_to_rgb.h"
24 | #include "cie1931.h"
25 |
26 | extern "C" {
27 | #include "vesc_uart.h"
28 | };
29 |
30 | uint32_t previous_tacho_value = 0;
31 | uint32_t previous_millis = 0;
32 | uint8_t fadeout_flag = 0;
33 | uint8_t standby_flag = 0;
34 |
35 |
36 | static bool vesc_standby_tacho(void) {
37 | bldc_get_values();
38 | if (previous_tacho_value == bldc_get_tachometer_value()) return true;
39 | return false;
40 | }
41 |
42 | static bool vesc_standby_timeout(uint16_t standby_timeout) {
43 | if (millis() - previous_millis > standby_timeout) return true;
44 | return false;
45 | }
46 |
47 |
48 | static void fadeout(void) {
49 | fadeout_flag = 1;
50 | rgb_color rgb;
51 | for (uint8_t i = 255; i > 00; i--) {
52 | for (uint8_t z = 0; z < LED_COUNT; z++) {
53 | if (led_strip_colors[z].red > 1) led_strip_colors[z].red -= 1;
54 | if (led_strip_colors[z].green > 1) led_strip_colors[z].green -= 1;
55 | if (led_strip_colors[z].blue > 1) led_strip_colors[z].blue -= 1;
56 | }
57 | if (!vesc_standby_tacho()) return;
58 | update_strip();
59 | }
60 | }
61 |
62 |
63 | bool vesc_standby(void) {
64 | if (vesc_standby_tacho()) {
65 | if (vesc_standby_timeout(STANDBY_TIMEOUT)) { return true; }
66 | return false;
67 | }
68 | else {
69 | previous_tacho_value = bldc_get_tachometer_value();
70 | previous_millis = millis();
71 | fadeout_flag = 0;
72 | return false;
73 | }
74 | }
75 |
76 |
77 | void vesc_standby_lights(void) {
78 | if (fadeout_flag == 0)
79 | fadeout();
80 |
81 | if (!vesc_standby_tacho()) return;
82 |
83 | rgb_color current_rgb_color;
84 | uint16_t random_hue = generate_random_hue();
85 | current_rgb_color = hsvToRgb(random_hue, 255, 255);
86 |
87 | for (uint8_t i = 0; i < 255; i++) {
88 | current_rgb_color = hsvToRgb(random_hue, 255, cie[i]);
89 | for (uint8_t z = 0; z < LED_COUNT; z++) { led_strip_colors[z] = current_rgb_color; }
90 | update_strip();
91 | }
92 | if (!vesc_standby_tacho()) return;
93 |
94 | for (uint8_t i = 255; i > 40; i--) {
95 | current_rgb_color = hsvToRgb(random_hue, 255, cie[i]);
96 | for (uint8_t z = 0; z < LED_COUNT; z++) { led_strip_colors[z] = current_rgb_color; }
97 | update_strip();
98 | if (!vesc_standby_tacho()) return;
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/lib/bldc_uart/bldc_interface.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Benjamin Vedder benjamin@vedder.se
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef BLDC_INTERFACE_H_
19 | #define BLDC_INTERFACE_H_
20 |
21 | #include "datatypes.h"
22 |
23 | // interface functions
24 | void bldc_interface_init(void(*func)(unsigned char *data, unsigned int len));
25 | void bldc_interface_set_forward_can(int32_t vesc_id);
26 | void bldc_interface_set_forward_func(void(*func)(unsigned char *data, unsigned int len));
27 | void bldc_interface_send_packet(unsigned char *data, unsigned int len);
28 | void bldc_interface_process_packet(unsigned char *data, unsigned int len);
29 |
30 | // Function pointer setters
31 | void bldc_interface_set_rx_value_func(void(*func)(mc_values *values));
32 | // void bldc_interface_set_rx_printf_func(void(*func)(char *str));
33 | // void bldc_interface_set_rx_fw_func(void(*func)(int major, int minor));
34 | // void bldc_interface_set_rx_rotor_pos_func(void(*func)(float pos));
35 | // void bldc_interface_set_rx_mcconf_func(void(*func)(mc_configuration *conf));
36 | // void bldc_interface_set_rx_appconf_func(void(*func)(app_configuration *conf));
37 | // void bldc_interface_set_rx_detect_func(void(*func)(float cycle_int_limit, float coupling_k,
38 | // const signed char *hall_table, signed char hall_res));
39 | // void bldc_interface_set_rx_dec_ppm_func(void(*func)(float val, float ms));
40 | // void bldc_interface_set_rx_dec_adc_func(void(*func)(float val, float voltage));
41 | // void bldc_interface_set_rx_dec_chuk_func(void(*func)(float val));
42 | // void bldc_interface_set_rx_mcconf_received_func(void(*func)(void));
43 | // void bldc_interface_set_rx_appconf_received_func(void(*func)(void));
44 |
45 | // Setters
46 | // void bldc_interface_terminal_cmd(char* cmd);
47 | // void bldc_interface_set_duty_cycle(float dutyCycle);
48 | void bldc_interface_set_current(float current);
49 | // void bldc_interface_set_current_brake(float current);
50 | void bldc_interface_set_rpm(int rpm);
51 | // void bldc_interface_set_pos(float pos);
52 | // void bldc_interface_set_servo_pos(float pos);
53 | // void bldc_interface_set_mcconf(const mc_configuration *mcconf);
54 | // void bldc_interface_set_appconf(const app_configuration *appconf);
55 |
56 | // Getters
57 | // void bldc_interface_get_fw_version(void);
58 | void bldc_interface_get_values(void);
59 | // void bldc_interface_get_mcconf(void);
60 | // void bldc_interface_get_appconf(void);
61 | // void bldc_interface_get_decoded_ppm(void);
62 | // void bldc_interface_get_decoded_adc(void);
63 | // void bldc_interface_get_decoded_chuk(void);
64 |
65 | // Other functions
66 | // void bldc_interface_detect_motor_param(float current, float min_rpm, float low_duty);
67 | void bldc_interface_reboot(void);
68 | void bldc_interface_send_alive(void);
69 |
70 | // Helpers
71 | // const char* bldc_interface_fault_to_string(mc_fault_code fault);
72 |
73 | #endif /* BLDC_INTERFACE_H_ */
74 |
--------------------------------------------------------------------------------
/lib/bldc_uart/vesc_uart.c:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #include "vesc_uart.h"
20 | #include "arduino_uart_wrapper.h"
21 |
22 | // extern "C" {
23 | // Include libraries copied from VESC
24 | #include "datatypes.h"
25 | #include "buffer.h"
26 | #include "crc.h"
27 | #include "bldc_interface.h"
28 | /* bldc_interface.c and bldc_interface.h - These files can assemble the payload for all the commands
29 | that the VESC supports. They can also interpret packets from the VESC and extract
30 | the data from them. Notice that only the payload is handled in these files,
31 | not the start, stop, length and checksum bytes since these are different for the CAN interface.
32 | */
33 | #include "bldc_interface_uart.h"
34 | /* bldc_interface_uart.c and bldc_interface_uart.h - Connects packet and bldc_interface to provide a clean
35 | UART interface. This is where the user has to make the connection to the UART interface for the platform of choice.
36 | */
37 | // };
38 |
39 | // define pointer "mc_values_ptr" of type mc_values
40 | mc_values *mc_values_ptr;
41 |
42 | /* NOTE:
43 | bldc_interface.c - changed "send_buffer[]" size
44 | packet.h changed "PACKET_MAX_PL_LEN" size and "PACKET_HANDLERS" quantity
45 | bldc_interface_uart.c - commented #include "ch.h" and #include "hal.h"
46 | */
47 |
48 | /* - datatypes.h - The data structures used by the VESC.
49 | - crc.c and crc.h - For calculating the CRC checksum
50 | - buffer.c and buffer.h - Helper functions for for going between C types and byte arrays. These are used by the bldc_interface files
51 | - packet.c and packet.h - For assembling the packets for the VESC with start, stop, length and checksum bytes. These
52 | files also have a state machine where one byte received from the VESC can be added at a time
53 | to assemble a packet and check the checksum correctness.
54 | - comm_uart.c - The connection between these files and the UART port is done in the file comm_uart.c,
55 | which is the file that you have to implement if you want to port this to a different platform.
56 | */
57 |
58 |
59 | // The bldc_interface_uart files have three functions that have to be used:
60 | // --bldc_interface_uart_init
61 | // --bldc_interface_uart_process_byte - you could call bldc_interface_uart_process_byte directly from an interrupt handler every time you receive a byte.
62 | // --bldc_interface_uart_run_timer
63 |
64 |
65 |
66 | //user implementation of function that will send the bytes in *data with length len on the UART
67 | static void send_packet(unsigned char *data, unsigned int len) {
68 | serial_write(data, len);
69 | }
70 |
71 |
72 | // Callback function for the received data from vesc
73 | static void bldc_val_received(mc_values *val) {
74 | mc_values_ptr = val;
75 | }
76 |
77 |
78 | void comm_uart_init() {
79 | serial_begin(115200);
80 | // serial_print("Debug console started!");
81 | // Initialize the bldc interface and provide send function
82 | bldc_interface_uart_init(send_packet);
83 | // Give bldc_interface a callback function to call for handling the data when values are received.
84 | bldc_interface_set_rx_value_func(bldc_val_received);
85 | }
86 |
87 |
88 |
89 | /* Custom function to get actual values from VESC, based on Benjamin Vedder tutorial.
90 | Use values in bldc_val_received() callback function. */
91 | void bldc_get_values(void) {
92 | /* Every time you want to read the realtime data you call the corresponding getter.
93 | This will send the get command to the VESC and return. When the data is received
94 | the callback will be called from the UART interface. */
95 | bldc_interface_get_values();
96 |
97 | //delay to give some time vesc to answer
98 | do_delay(10);
99 |
100 | while (serial_available()) {
101 | /* Call bldc_interface_uart_process_byte() function every time a byte is received on the UART
102 | with the received byte. It will run the state machine in the packet assembler and the callbacks in
103 | bldc interface will be called when the packets are ready.
104 | You could call bldc_interface_uart_process_byte directly from an interrupt handler every time you receive a byte. */
105 | bldc_interface_uart_process_byte(serial_read());
106 | }
107 |
108 | bldc_interface_uart_run_timer();
109 | //Call this function every millisecond to reset the packet state machine after a timeout in case data is lost.
110 | }
111 |
112 |
113 | /* get current VESC tachometer value */
114 | long unsigned int bldc_get_tachometer_value(void) {
115 | return mc_values_ptr->tachometer;
116 | }
117 |
118 |
119 | /* Communicating is a uint8_t byte stream.
120 | First byte: "0x02" for packet length < 256 byte, second byte is for the packet length.
121 | Second byte: "0x03" for > 256 byte packet length, and next 2 bytes for the packet length.
122 | The follwing 2 bytes after the packet are the checksum. (see crc.h)
123 | The byte stream it terminated with a 0x03.
124 | */
125 |
--------------------------------------------------------------------------------
/lib/bldc_uart/packet.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Benjamin Vedder benjamin@vedder.se
3 |
4 | This file is part of the VESC firmware.
5 |
6 | The VESC firmware is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | The VESC firmware is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include
21 | #include "packet.h"
22 | #include "crc.h"
23 |
24 | typedef struct {
25 | volatile unsigned char rx_state;
26 | volatile unsigned short rx_timeout;
27 | void(*send_func)(unsigned char *data, unsigned int len);
28 | void(*process_func)(unsigned char *data, unsigned int len);
29 | unsigned int payload_length;
30 | unsigned char rx_buffer[PACKET_MAX_PL_LEN];
31 | unsigned char tx_buffer[PACKET_MAX_PL_LEN + 6];
32 | unsigned int rx_data_ptr;
33 | unsigned char crc_low;
34 | unsigned char crc_high;
35 | } PACKET_STATE_t;
36 |
37 | static PACKET_STATE_t handler_states[PACKET_HANDLERS];
38 |
39 | void packet_init(void (*s_func)(unsigned char *data, unsigned int len),
40 | void (*p_func)(unsigned char *data, unsigned int len), int handler_num) {
41 | handler_states[handler_num].send_func = s_func;
42 | handler_states[handler_num].process_func = p_func;
43 | }
44 |
45 | void packet_send_packet(unsigned char *data, unsigned int len, int handler_num) {
46 | if (len > PACKET_MAX_PL_LEN) {
47 | return;
48 | }
49 |
50 | int b_ind = 0;
51 |
52 | if (len <= 256) {
53 | handler_states[handler_num].tx_buffer[b_ind++] = 2;
54 | handler_states[handler_num].tx_buffer[b_ind++] = len;
55 | } else {
56 | handler_states[handler_num].tx_buffer[b_ind++] = 3;
57 | handler_states[handler_num].tx_buffer[b_ind++] = len >> 8;
58 | handler_states[handler_num].tx_buffer[b_ind++] = len & 0xFF;
59 | }
60 |
61 | memcpy(handler_states[handler_num].tx_buffer + b_ind, data, len);
62 | b_ind += len;
63 |
64 | unsigned short crc = crc16(data, len);
65 | handler_states[handler_num].tx_buffer[b_ind++] = (uint8_t)(crc >> 8);
66 | handler_states[handler_num].tx_buffer[b_ind++] = (uint8_t)(crc & 0xFF);
67 | handler_states[handler_num].tx_buffer[b_ind++] = 3;
68 |
69 | if (handler_states[handler_num].send_func) {
70 | handler_states[handler_num].send_func(handler_states[handler_num].tx_buffer, b_ind);
71 | }
72 | }
73 |
74 | /**
75 | * Call this function every millisecond.
76 | */
77 | void packet_timerfunc(void) {
78 | for (int i = 0;i < PACKET_HANDLERS;i++) {
79 | if (handler_states[i].rx_timeout) {
80 | handler_states[i].rx_timeout--;
81 | } else {
82 | handler_states[i].rx_state = 0;
83 | }
84 | }
85 | }
86 |
87 | void packet_process_byte(uint8_t rx_data, int handler_num) {
88 | switch (handler_states[handler_num].rx_state) {
89 | case 0:
90 | if (rx_data == 2) {
91 | // 1 byte PL len
92 | handler_states[handler_num].rx_state += 2;
93 | handler_states[handler_num].rx_timeout = PACKET_RX_TIMEOUT;
94 | handler_states[handler_num].rx_data_ptr = 0;
95 | handler_states[handler_num].payload_length = 0;
96 | } else if (rx_data == 3) {
97 | // 2 byte PL len
98 | handler_states[handler_num].rx_state++;
99 | handler_states[handler_num].rx_timeout = PACKET_RX_TIMEOUT;
100 | handler_states[handler_num].rx_data_ptr = 0;
101 | handler_states[handler_num].payload_length = 0;
102 | } else {
103 | handler_states[handler_num].rx_state = 0;
104 | }
105 | break;
106 |
107 | case 1:
108 | handler_states[handler_num].payload_length = (unsigned int)rx_data << 8;
109 | handler_states[handler_num].rx_state++;
110 | handler_states[handler_num].rx_timeout = PACKET_RX_TIMEOUT;
111 | break;
112 |
113 | case 2:
114 | handler_states[handler_num].payload_length |= (unsigned int)rx_data;
115 | if (handler_states[handler_num].payload_length > 0 &&
116 | handler_states[handler_num].payload_length <= PACKET_MAX_PL_LEN) {
117 | handler_states[handler_num].rx_state++;
118 | handler_states[handler_num].rx_timeout = PACKET_RX_TIMEOUT;
119 | } else {
120 | handler_states[handler_num].rx_state = 0;
121 | }
122 | break;
123 |
124 | case 3:
125 | handler_states[handler_num].rx_buffer[handler_states[handler_num].rx_data_ptr++] = rx_data;
126 | if (handler_states[handler_num].rx_data_ptr == handler_states[handler_num].payload_length) {
127 | handler_states[handler_num].rx_state++;
128 | }
129 | handler_states[handler_num].rx_timeout = PACKET_RX_TIMEOUT;
130 | break;
131 |
132 | case 4:
133 | handler_states[handler_num].crc_high = rx_data;
134 | handler_states[handler_num].rx_state++;
135 | handler_states[handler_num].rx_timeout = PACKET_RX_TIMEOUT;
136 | break;
137 |
138 | case 5:
139 | handler_states[handler_num].crc_low = rx_data;
140 | handler_states[handler_num].rx_state++;
141 | handler_states[handler_num].rx_timeout = PACKET_RX_TIMEOUT;
142 | break;
143 |
144 | case 6:
145 | if (rx_data == 3) {
146 | if (crc16(handler_states[handler_num].rx_buffer, handler_states[handler_num].payload_length)
147 | == ((unsigned short)handler_states[handler_num].crc_high << 8
148 | | (unsigned short)handler_states[handler_num].crc_low)) {
149 | // Packet received!
150 | if (handler_states[handler_num].process_func) {
151 | handler_states[handler_num].process_func(handler_states[handler_num].rx_buffer,
152 | handler_states[handler_num].payload_length);
153 | }
154 | }
155 | }
156 | handler_states[handler_num].rx_state = 0;
157 | break;
158 |
159 | default:
160 | handler_states[handler_num].rx_state = 0;
161 | break;
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/lib/bldc_uart/buffer.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2012-2014 Benjamin Vedder benjamin@vedder.se
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * buffer.c
20 | *
21 | * Created on: 13 maj 2013
22 | * Author: benjamin
23 | */
24 |
25 | #include "buffer.h"
26 | #include
27 | #include
28 |
29 | void buffer_append_int16(uint8_t* buffer, int16_t number, int32_t *index) {
30 | buffer[(*index)++] = number >> 8;
31 | buffer[(*index)++] = number;
32 | }
33 |
34 | void buffer_append_uint16(uint8_t* buffer, uint16_t number, int32_t *index) {
35 | buffer[(*index)++] = number >> 8;
36 | buffer[(*index)++] = number;
37 | }
38 |
39 | void buffer_append_int32(uint8_t* buffer, int32_t number, int32_t *index) {
40 | buffer[(*index)++] = number >> 24;
41 | buffer[(*index)++] = number >> 16;
42 | buffer[(*index)++] = number >> 8;
43 | buffer[(*index)++] = number;
44 | }
45 |
46 | void buffer_append_uint32(uint8_t* buffer, uint32_t number, int32_t *index) {
47 | buffer[(*index)++] = number >> 24;
48 | buffer[(*index)++] = number >> 16;
49 | buffer[(*index)++] = number >> 8;
50 | buffer[(*index)++] = number;
51 | }
52 |
53 | void buffer_append_int64(uint8_t* buffer, int64_t number, int32_t *index) {
54 | buffer[(*index)++] = number >> 56;
55 | buffer[(*index)++] = number >> 48;
56 | buffer[(*index)++] = number >> 40;
57 | buffer[(*index)++] = number >> 32;
58 | buffer[(*index)++] = number >> 24;
59 | buffer[(*index)++] = number >> 16;
60 | buffer[(*index)++] = number >> 8;
61 | buffer[(*index)++] = number;
62 | }
63 |
64 | void buffer_append_uint64(uint8_t* buffer, uint64_t number, int32_t *index) {
65 | buffer[(*index)++] = number >> 56;
66 | buffer[(*index)++] = number >> 48;
67 | buffer[(*index)++] = number >> 40;
68 | buffer[(*index)++] = number >> 32;
69 | buffer[(*index)++] = number >> 24;
70 | buffer[(*index)++] = number >> 16;
71 | buffer[(*index)++] = number >> 8;
72 | buffer[(*index)++] = number;
73 | }
74 |
75 | void buffer_append_float16(uint8_t* buffer, float number, float scale, int32_t *index) {
76 | buffer_append_int16(buffer, (int16_t)(number * scale), index);
77 | }
78 |
79 | void buffer_append_float32(uint8_t* buffer, float number, float scale, int32_t *index) {
80 | buffer_append_int32(buffer, (int32_t)(number * scale), index);
81 | }
82 |
83 | void buffer_append_double64(uint8_t* buffer, double number, double scale, int32_t *index) {
84 | buffer_append_int64(buffer, (int64_t)(number * scale), index);
85 | }
86 |
87 | void buffer_append_float32_auto(uint8_t* buffer, float number, int32_t *index) {
88 | int e = 0;
89 | float sig = frexpf(number, &e);
90 | float sig_abs = fabsf(sig);
91 | uint32_t sig_i = 0;
92 |
93 | if (sig_abs >= 0.5) {
94 | sig_i = (uint32_t)((sig_abs - 0.5f) * 2.0f * 8388608.0f);
95 | e += 126;
96 | }
97 |
98 | uint32_t res = ((e & 0xFF) << 23) | (sig_i & 0x7FFFFF);
99 | if (sig < 0) {
100 | res |= 1 << 31;
101 | }
102 |
103 | buffer_append_uint32(buffer, res, index);
104 | }
105 |
106 | int16_t buffer_get_int16(const uint8_t *buffer, int32_t *index) {
107 | int16_t res = ((uint16_t) buffer[*index]) << 8 |
108 | ((uint16_t) buffer[*index + 1]);
109 | *index += 2;
110 | return res;
111 | }
112 |
113 | uint16_t buffer_get_uint16(const uint8_t *buffer, int32_t *index) {
114 | uint16_t res = ((uint16_t) buffer[*index]) << 8 |
115 | ((uint16_t) buffer[*index + 1]);
116 | *index += 2;
117 | return res;
118 | }
119 |
120 | int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index) {
121 | int32_t res = ((uint32_t) buffer[*index]) << 24 |
122 | ((uint32_t) buffer[*index + 1]) << 16 |
123 | ((uint32_t) buffer[*index + 2]) << 8 |
124 | ((uint32_t) buffer[*index + 3]);
125 | *index += 4;
126 | return res;
127 | }
128 |
129 | uint32_t buffer_get_uint32(const uint8_t *buffer, int32_t *index) {
130 | uint32_t res = ((uint32_t) buffer[*index]) << 24 |
131 | ((uint32_t) buffer[*index + 1]) << 16 |
132 | ((uint32_t) buffer[*index + 2]) << 8 |
133 | ((uint32_t) buffer[*index + 3]);
134 | *index += 4;
135 | return res;
136 | }
137 |
138 | int64_t buffer_get_int64(const uint8_t *buffer, int32_t *index) {
139 | int64_t res = ((uint64_t) buffer[*index]) << 56 |
140 | ((uint64_t) buffer[*index + 1]) << 48 |
141 | ((uint64_t) buffer[*index + 2]) << 40 |
142 | ((uint64_t) buffer[*index + 3]) << 32 |
143 | ((uint64_t) buffer[*index + 4]) << 24 |
144 | ((uint64_t) buffer[*index + 5]) << 16 |
145 | ((uint64_t) buffer[*index + 6]) << 8 |
146 | ((uint64_t) buffer[*index + 7]);
147 | *index += 8;
148 | return res;
149 | }
150 |
151 | uint64_t buffer_get_uint64(const uint8_t *buffer, int32_t *index) {
152 | uint64_t res = ((uint64_t) buffer[*index]) << 56 |
153 | ((uint64_t) buffer[*index + 1]) << 48 |
154 | ((uint64_t) buffer[*index + 2]) << 40 |
155 | ((uint64_t) buffer[*index + 3]) << 32 |
156 | ((uint64_t) buffer[*index + 4]) << 24 |
157 | ((uint64_t) buffer[*index + 5]) << 16 |
158 | ((uint64_t) buffer[*index + 6]) << 8 |
159 | ((uint64_t) buffer[*index + 7]);
160 | *index += 8;
161 | return res;
162 | }
163 |
164 | float buffer_get_float16(const uint8_t *buffer, float scale, int32_t *index) {
165 | return (float)buffer_get_int16(buffer, index) / scale;
166 | }
167 |
168 | float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index) {
169 | return (float)buffer_get_int32(buffer, index) / scale;
170 | }
171 |
172 | double buffer_get_double64(const uint8_t *buffer, double scale, int32_t *index) {
173 | return (double)buffer_get_int64(buffer, index) / scale;
174 | }
175 |
176 | float buffer_get_float32_auto(const uint8_t *buffer, int32_t *index) {
177 | uint32_t res = buffer_get_uint32(buffer, index);
178 |
179 | int e = (res >> 23) & 0xFF;
180 | uint32_t sig_i = res & 0x7FFFFF;
181 | bool neg = res & (1 << 31);
182 |
183 | float sig = 0.0;
184 | if (e != 0 || sig_i != 0) {
185 | sig = (float)sig_i / (8388608.0 * 2.0) + 0.5;
186 | e -= 126;
187 | }
188 |
189 | if (neg) {
190 | sig = -sig;
191 | }
192 |
193 | return ldexpf(sig, e);
194 | }
195 |
--------------------------------------------------------------------------------
/lib/color_palletes/color_palletes.h:
--------------------------------------------------------------------------------
1 | /*
2 | This is Synchronized Lighting project for VESC controller
3 | Copyright (C) 2018 Maksim Reshetnikov ju_mpe_r@electric-skates.ru
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 | */
18 |
19 | #ifndef COLOR_PALLETES_H
20 | #define COLOR_PALLETES_H
21 |
22 | #include
23 | #include
24 | #include "led_strip_apa102.h"
25 |
26 |
27 | /* set your pallete name here */
28 | #define CURRENT_PALLETE test_pallete
29 | /* set width of your pallete here */
30 | static const uint16_t CURRENT_PALLETE_WIDTH = 30;
31 |
32 |
33 | void write_custom_color_pallete(void);
34 | rgb_color get_pallete_color(uint32_t position);
35 |
36 |
37 | /* hue 0-360, saturation 0-255, value 0-255 */
38 | typedef struct {
39 | uint16_t hue;
40 | // uint8_t saturation;
41 | // uint8_t value;
42 | } hsv_color;
43 |
44 |
45 |
46 |
47 | /* -== COLOR PALLETES PART ==- */
48 | /* Pallete values are Hue value of HSV color space (0 - 360) for each led */
49 |
50 | /* TEST PALLETE */
51 | const hsv_color test_pallete[30] PROGMEM = {
52 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 | 120, 120, 120, 120, 120, 120, 120, 120, 120, 120,
54 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
55 | };
56 |
57 | /* POLICE PALLETE */
58 | const hsv_color police_pallete[80] PROGMEM = {
59 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
64 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
65 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
66 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
67 | };
68 |
69 | /* akhlut POLICE PALLETE */
70 | const hsv_color akhlut_police_pallete[460] PROGMEM = {
71 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
76 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
77 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
78 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
79 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
84 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
85 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
86 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
87 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
89 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
90 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
91 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
92 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
94 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
95 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
96 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
97 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
99 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
100 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
101 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
102 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
107 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
112 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116 | 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
117 | };
118 |
119 |
120 | /* FULL HUE PALLETE */
121 | const hsv_color full_hue[360] PROGMEM = {
122 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
123 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
124 | 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
125 | 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
126 | 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
127 | 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
128 | 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
129 | 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
130 | 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
131 | 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
132 | 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
133 | 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
134 | 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
135 | 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
136 | 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
137 | 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
138 | 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
139 | 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
140 | 180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
141 | 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
142 | 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
143 | 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
144 | 220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
145 | 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
146 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
147 | 250, 251, 252, 253, 254, 255, 256, 257, 258, 259,
148 | 260, 261, 262, 263, 264, 265, 266, 267, 268, 269,
149 | 270, 271, 272, 273, 274, 275, 276, 277, 278, 279,
150 | 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
151 | 290, 291, 292, 293, 294, 295, 296, 297, 298, 299,
152 | 300, 301, 302, 303, 304, 305, 306, 307, 308, 309,
153 | 310, 311, 312, 313, 314, 315, 316, 317, 318, 319,
154 | 320, 321, 322, 323, 324, 325, 326, 327, 328, 329,
155 | 330, 331, 332, 333, 334, 335, 336, 337, 338, 339,
156 | 340, 341, 342, 343, 344, 345, 346, 347, 348, 349,
157 | 350, 351, 352, 353, 354, 355, 356, 357, 358, 359,
158 | };
159 |
160 | /* RASTA PALLETE */
161 | const hsv_color rasta_pallete[60] PROGMEM = {
162 | 120, 120, 120, 120, 120, 120, 120, 120, 120, 120,
163 | 120, 120, 120, 120, 120, 120, 120, 120, 120, 120,
164 | 60, 60, 60, 60, 60, 60, 60, 60, 60, 60,
165 | 60, 60, 60, 60, 60, 60, 60, 60, 60, 60,
166 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
167 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 | };
169 |
170 | /* ORION PALLETE */
171 | const hsv_color orion_pallete [80] PROGMEM = {
172 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
173 | 250, 251, 252, 253, 254, 255, 256, 257, 258, 259,
174 | 260, 261, 262, 263, 264, 265, 265, 267, 268, 269,
175 | 270, 271, 272, 273, 274, 275, 276, 277, 278, 279,
176 | 280, 279, 278, 277, 276, 275, 274, 273, 272, 271,
177 | 270, 269, 268, 267, 266, 265, 264, 263, 262, 261,
178 | 260, 259, 258, 257, 256, 255, 254, 253, 252, 251,
179 | 250, 249, 248, 247, 246, 245, 244, 243, 242, 241,
180 | };
181 |
182 | #endif /* COLOR_PALLETES_H */
183 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/lib/bldc_uart/datatypes.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016-2017 Benjamin Vedder benjamin@vedder.se
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | #ifndef DATATYPES_H_
19 | #define DATATYPES_H_
20 |
21 | #include
22 | #include
23 |
24 | // For double precision literals
25 | #define D(x) ((double)x##L)
26 | #define D_PI D(3.14159265358979323846)
27 |
28 | // Sizes
29 | #define LOG_NAME_MAX_LEN 20
30 |
31 | // Constants
32 | #define MS_PER_DAY (24 * 60 * 60 * 1000)
33 |
34 | // CAN ID mask for DecaWave module
35 | #define CAN_MASK_DW (5 << 8)
36 | #define CAN_DW_ID_ANY 255
37 |
38 | // Orientation data
39 | // typedef struct {
40 | // float q0;
41 | // float q1;
42 | // float q2;
43 | // float q3;
44 | // float integralFBx;
45 | // float integralFBy;
46 | // float integralFBz;
47 | // float accMagP;
48 | // int initialUpdateDone;
49 | // } ATTITUDE_INFO;
50 |
51 | // Position and orientation state
52 | // typedef struct {
53 | // float px; // Meters
54 | // float py; // Meters
55 | // float pz; // Meters
56 | // float vx; // Meters / second
57 | // float vy; // Meters / second
58 | // float speed; // Meters / second
59 | // float roll; // Degrees
60 | // float pitch; // Degrees
61 | // float yaw; // Degrees
62 | // float roll_rate; // Degrees / second
63 | // float pitch_rate; // Degrees / second
64 | // float yaw_rate; // Degrees / second
65 | // float q0;
66 | // float q1;
67 | // float q2;
68 | // float q3;
69 | //
70 | // // Current gps position and time stamp
71 | // float px_gps;
72 | // float py_gps;
73 | // float pz_gps;
74 | // int32_t gps_ms;
75 | // int gps_fix_type;
76 | // float gps_last_corr_diff;
77 | //
78 | // // Previous GPS position and time stamp
79 | // float px_gps_last;
80 | // float py_gps_last;
81 | // float pz_gps_last;
82 | // int32_t gps_ms_last;
83 | //
84 | // float gps_corr_cnt;
85 | // float gps_ang_corr_x_last_gps;
86 | // float gps_ang_corr_y_last_gps;
87 | // float gps_ang_corr_x_last_car;
88 | // float gps_ang_corr_y_last_car;
89 | // float gps_ground_level;
90 | //
91 | // uint32_t gps_corr_time;
92 | // uint32_t ultra_update_time;
93 | //
94 | // // Multirotor state
95 | // float tilt_roll_err;
96 | // float tilt_pitch_err;
97 | //
98 | // float error_vx_last;
99 | // float error_vy_last;
100 | //
101 | // float vel_corr_x_int;
102 | // float vel_corr_y_int;
103 | //
104 | // float tilt_corr_x_int;
105 | // float tilt_corr_y_int;
106 | // } POS_STATE;
107 |
108 | // Autopilot map point
109 | // typedef struct {
110 | // float px;
111 | // float py;
112 | // float pz;
113 | // float speed;
114 | // int32_t time;
115 | // } ROUTE_POINT;
116 |
117 | // Position history point
118 | // typedef struct {
119 | // float px;
120 | // float py;
121 | // float pz;
122 | // float yaw;
123 | // float speed;
124 | // int32_t time;
125 | // } POS_POINT;
126 |
127 | // typedef enum {
128 | // MOTE_PACKET_FILL_RX_BUFFER = 0,
129 | // MOTE_PACKET_FILL_RX_BUFFER_LONG,
130 | // MOTE_PACKET_PROCESS_RX_BUFFER,
131 | // MOTE_PACKET_PROCESS_SHORT_BUFFER,
132 | // } MOTE_PACKET;
133 |
134 | // CAN commands
135 | // typedef enum {
136 | // CAN_PACKET_FILL_RX_BUFFER = 5,
137 | // CAN_PACKET_FILL_RX_BUFFER_LONG,
138 | // CAN_PACKET_PROCESS_RX_BUFFER,
139 | // CAN_PACKET_PROCESS_SHORT_BUFFER,
140 | // CAN_PACKET_STATUS
141 | // } CAN_PACKET_ID;
142 |
143 | // Commands
144 | // typedef enum {
145 | // // General commands
146 | // CMD_PRINTF = 0,
147 | // CMD_TERMINAL_CMD,
148 | //
149 | // // Common vehicle commands
150 | // CMD_VESC_FWD = 50,
151 | // CMD_SET_POS,
152 | // CMD_SET_POS_ACK,
153 | // CMD_SET_ENU_REF,
154 | // CMD_GET_ENU_REF,
155 | // CMD_AP_ADD_POINTS,
156 | // CMD_AP_REMOVE_LAST_POINT,
157 | // CMD_AP_CLEAR_POINTS,
158 | // CMD_AP_GET_ROUTE_PART,
159 | // CMD_AP_SET_ACTIVE,
160 | // CMD_AP_REPLACE_ROUTE,
161 | // CMD_AP_SYNC_POINT,
162 | // CMD_SEND_RTCM_USB,
163 | // CMD_SEND_NMEA_RADIO,
164 | // CMD_SET_YAW_OFFSET,
165 | // CMD_SET_YAW_OFFSET_ACK,
166 | // CMD_LOG_LINE_USB,
167 | // CMD_PLOT_INIT,
168 | // CMD_PLOT_DATA,
169 | // CMD_SET_MS_TODAY,
170 | // CMD_SET_SYSTEM_TIME,
171 | // CMD_SET_SYSTEM_TIME_ACK,
172 | // CMD_REBOOT_SYSTEM,
173 | // CMD_REBOOT_SYSTEM_ACK,
174 | // CMD_RADAR_SETUP_SET,
175 | // CMD_RADAR_SETUP_GET,
176 | // CMD_RADAR_SAMPLES,
177 | // CMD_DW_SAMPLE,
178 | // CMD_EMERGENCY_STOP,
179 | // CMD_SET_MAIN_CONFIG,
180 | // CMD_GET_MAIN_CONFIG,
181 | // CMD_GET_MAIN_CONFIG_DEFAULT,
182 | //
183 | // // Car commands
184 | // CMD_GET_STATE = 120,
185 | // CMD_RC_CONTROL,
186 | // CMD_SET_SERVO_DIRECT,
187 | //
188 | // // Multirotor commands
189 | // CMD_MR_GET_STATE = 160,
190 | // CMD_MR_RC_CONTROL,
191 | // CMD_MR_OVERRIDE_POWER,
192 | //
193 | // // Mote commands
194 | // CMD_MOTE_UBX_START_BASE = 200,
195 | // CMD_MOTE_UBX_START_BASE_ACK,
196 | // CMD_MOTE_UBX_BASE_STATUS
197 | // } CMD_PACKET;
198 |
199 | // RC control modes
200 | // typedef enum {
201 | // RC_MODE_CURRENT = 0,
202 | // RC_MODE_DUTY,
203 | // RC_MODE_PID,
204 | // RC_MODE_CURRENT_BRAKE
205 | // } RC_MODE;
206 |
207 | // typedef struct {
208 | // bool yaw_use_odometry; // Use odometry data for yaw angle correction.
209 | // float yaw_imu_gain; // Gain for yaw angle from IMU (vs odometry)
210 | // bool disable_motor; // Disable motor drive commands to make sure that the motor does not move.
211 | // bool simulate_motor; // Simulate motor movement without motor controller feedback
212 | //
213 | // float gear_ratio;
214 | // float wheel_diam;
215 | // float motor_poles;
216 | // float steering_max_angle_rad; // = arctan(axist_distance / turn_radius_at_maximum_steering_angle)
217 | // float steering_center;
218 | // float steering_range;
219 | // float steering_ramp_time; // Ramp time constant for the steering servo in seconds
220 | // float axis_distance;
221 | // } MAIN_CONFIG_CAR;
222 |
223 | // typedef struct {
224 | // // Dead reckoning
225 | // float vel_decay_e;
226 | // float vel_decay_l;
227 | // float vel_max;
228 | // float map_min_x;
229 | // float map_max_x;
230 | // float map_min_y;
231 | // float map_max_y;
232 | //
233 | // // State correction for dead reckoning
234 | // float vel_gain_p;
235 | // float vel_gain_i;
236 | // float vel_gain_d;
237 | //
238 | // float tilt_gain_p;
239 | // float tilt_gain_i;
240 | // float tilt_gain_d;
241 | //
242 | // float max_corr_error;
243 | // float max_tilt_error;
244 | //
245 | // // Attitude controller
246 | // float ctrl_gain_roll_p;
247 | // float ctrl_gain_roll_i;
248 | // float ctrl_gain_roll_dp;
249 | // float ctrl_gain_roll_de;
250 | //
251 | // float ctrl_gain_pitch_p;
252 | // float ctrl_gain_pitch_i;
253 | // float ctrl_gain_pitch_dp;
254 | // float ctrl_gain_pitch_de;
255 | //
256 | // float ctrl_gain_yaw_p;
257 | // float ctrl_gain_yaw_i;
258 | // float ctrl_gain_yaw_dp;
259 | // float ctrl_gain_yaw_de;
260 | //
261 | // // Position controller
262 | // float ctrl_gain_pos_p;
263 | // float ctrl_gain_pos_i;
264 | // float ctrl_gain_pos_d;
265 | //
266 | // // Altitude controller
267 | // float ctrl_gain_alt_p;
268 | // float ctrl_gain_alt_i;
269 | // float ctrl_gain_alt_d;
270 | //
271 | // // Joystick gain
272 | // float js_gain_tilt;
273 | // float js_gain_yaw;
274 | // bool js_mode_rate;
275 | //
276 | // // Motor mapping and configuration
277 | // int8_t motor_fl_f; // x: Front Left +: Front
278 | // int8_t motor_bl_l; // x: Back Left +: Left
279 | // int8_t motor_fr_r; // x: Front Right +: Right
280 | // int8_t motor_br_b; // x: Back Right +: Back
281 | // bool motors_x; // Use x motor configuration (use + if false)
282 | // bool motors_cw; // Front left (or front in + mode) runs in the clockwise direction (ccw if false)
283 | // uint16_t motor_pwm_min_us; // Minimum servo pulse length for motor in microseconds
284 | // uint16_t motor_pwm_max_us; // Maximum servo pulse length for motor in microseconds
285 | // } MAIN_CONFIG_MULTIROTOR;
286 |
287 | // Car configuration
288 | // typedef struct {
289 | // // Common vehicle settings
290 | // bool mag_use; // Use the magnetometer
291 | // bool mag_comp; // Should be 0 when capturing samples for the calibration
292 | // float yaw_mag_gain; // Gain for yaw angle from magnetomer (vs gyro)
293 | //
294 | // // Magnetometer calibration
295 | // float mag_cal_cx;
296 | // float mag_cal_cy;
297 | // float mag_cal_cz;
298 | // float mag_cal_xx;
299 | // float mag_cal_xy;
300 | // float mag_cal_xz;
301 | // float mag_cal_yx;
302 | // float mag_cal_yy;
303 | // float mag_cal_yz;
304 | // float mag_cal_zx;
305 | // float mag_cal_zy;
306 | // float mag_cal_zz;
307 | //
308 | // // GPS parameters
309 | // float gps_ant_x; // Antenna offset from vehicle center in X
310 | // float gps_ant_y; // Antenna offset from vehicle center in Y
311 | // bool gps_comp; // Use GPS position correction
312 | // bool gps_req_rtk; // Require RTK solution
313 | // bool gps_use_rtcm_base_as_enu_ref; // Use RTCM base station position as ENU reference
314 | // float gps_corr_gain_stat; // Static GPS correction gain
315 | // float gps_corr_gain_dyn; // Dynamic GPS correction gain
316 | // float gps_corr_gain_yaw; // Gain for yaw correction
317 | // bool gps_send_nmea; // Send NMEA data for logging and debugging
318 | // bool gps_use_ubx_info; // Use info about the ublox solution
319 | // float gps_ubx_max_acc; // Maximum ublox accuracy to use solution (m, higher = worse)
320 | //
321 | // // Autopilot parameters
322 | // bool ap_repeat_routes; // Repeat the same route when the end is reached
323 | // float ap_base_rad; // Radius around car at 0 speed
324 | // int ap_mode_time; // Drive to route points based on time (1 = abs time, 2 = rel since start)
325 | // float ap_max_speed; // Maximum allowed speed for autopilot
326 | // int32_t ap_time_add_repeat_ms; // Time to add to each point for each repetition of the route
327 | //
328 | // // Logging
329 | // int log_rate_hz;
330 | // bool log_en;
331 | // char log_name[LOG_NAME_MAX_LEN + 1];
332 | // int log_en_uart;
333 | // int log_uart_baud;
334 | //
335 | // MAIN_CONFIG_CAR car;
336 | // MAIN_CONFIG_MULTIROTOR mr;
337 | // } MAIN_CONFIG;
338 |
339 | // typedef struct {
340 | // // GPS position
341 | // double lat;
342 | // double lon;
343 | // double height;
344 | // double x;
345 | // double y;
346 | // double z;
347 | // int fix_type; // 0=Invalid, 1=SPP, 4=RTK fix, 5=RTK float
348 | // int sats;
349 | // int32_t ms; // Milliseconds today
350 | // uint32_t update_time;
351 | // // Local position (ENU frame)
352 | // bool local_init_done;
353 | // float lx;
354 | // float ly;
355 | // float lz;
356 | // // Initial local position
357 | // double ix;
358 | // double iy;
359 | // double iz;
360 | // // Rotation matrix for local position
361 | // float r1c1, r1c2, r1c3;
362 | // float r2c1, r2c2, r2c3;
363 | // float r3c1, r3c2, r3c3;
364 | // } GPS_STATE;
365 |
366 | // DW Logging Info
367 | // typedef struct {
368 | // bool valid;
369 | // uint8_t dw_anchor;
370 | // int32_t time_today_ms;
371 | // float dw_dist;
372 | // float px;
373 | // float py;
374 | // float px_gps;
375 | // float py_gps;
376 | // float pz_gps;
377 | // } DW_LOG_INFO;
378 |
379 | // ============== UBLOX Datatypes ================== //
380 |
381 | // typedef struct {
382 | // uint16_t ref_station_id;
383 | // uint32_t i_tow; // GPS time of week of the navigation epoch
384 | // float pos_n; // Position north in meters
385 | // float pos_e; // Position east in meters
386 | // float pos_d; // Position down in meters
387 | // float acc_n; // Accuracy north in meters
388 | // float acc_e; // Accuracy east in meters
389 | // float acc_d; // Accuracy down in meters
390 | // bool fix_ok; // A valid fix
391 | // bool diff_soln; // Differential corrections are applied
392 | // bool rel_pos_valid; // Relative position components and accuracies valid
393 | // int carr_soln; // fix_type 0: no fix, 1: float, 2: fix
394 | // } ubx_nav_relposned;
395 |
396 | // typedef struct {
397 | // uint32_t i_tow; // GPS time of week of the navigation epoch
398 | // uint32_t dur; // Passed survey-in observation time (s)
399 | // double meanX; // Current survey-in mean position ECEF X coordinate
400 | // double meanY; // Current survey-in mean position ECEF Y coordinate
401 | // double meanZ; // Current survey-in mean position ECEF Z coordinate
402 | // float meanAcc; // Current survey-in mean position accuracy
403 | // uint32_t obs; // Number of position observations used during survey-in
404 | // bool valid; // Survey-in position validity flag, 1 = valid, otherwise 0
405 | // bool active; // Survey-in in progress flag, 1 = in-progress, otherwise 0
406 | // } ubx_nav_svin;
407 |
408 | // typedef struct {
409 | // double pr_mes;
410 | // double cp_mes;
411 | // float do_mes;
412 | // uint8_t gnss_id;
413 | // uint8_t sv_id;
414 | // uint8_t freq_id;
415 | // uint16_t locktime;
416 | // uint8_t cno;
417 | // uint8_t pr_stdev;
418 | // uint8_t cp_stdev;
419 | // uint8_t do_stdev;
420 | // bool pr_valid;
421 | // bool cp_valid;
422 | // bool half_cyc_valid;
423 | // bool half_cyc_sub;
424 | // } ubx_rxm_rawx_obs;
425 |
426 | // typedef struct {
427 | // double rcv_tow;
428 | // uint16_t week;
429 | // int8_t leaps;
430 | // uint8_t num_meas;
431 | // bool leap_sec;
432 | // bool clk_reset;
433 | // ubx_rxm_rawx_obs obs[32];
434 | // } ubx_rxm_rawx;
435 |
436 | // typedef struct {
437 | // uint32_t baudrate;
438 | // bool in_rtcm3;
439 | // bool in_rtcm2;
440 | // bool in_nmea;
441 | // bool in_ubx;
442 | // bool out_rtcm3;
443 | // bool out_nmea;
444 | // bool out_ubx;
445 | // } ubx_cfg_prt_uart;
446 |
447 | // typedef struct {
448 | // bool lla; // Use lla instead of ecef
449 | // int mode; // Mode. 0 = Disabled, 1 = Survey in, 2 = Fixed
450 | // double ecefx_lat;
451 | // double ecefy_lon;
452 | // double ecefz_alt;
453 | // float fixed_pos_acc; // Fixed position accuracy
454 | // uint32_t svin_min_dur; // SVIN minimum duration (s)
455 | // float svin_acc_limit; // SVIN accuracy limit
456 | // } ubx_cfg_tmode3;
457 |
458 | // typedef struct {
459 | // bool apply_dyn; // Apply dynamic model settings
460 | // bool apply_min_el; // Apply minimum elevation settings
461 | // bool apply_pos_fix_mode; // Apply fix mode settings
462 | // bool apply_pos_mask; // Apply position mask settings
463 | // bool apply_time_mask; // Apply time mask settings
464 | // bool apply_static_hold_mask; // Apply static hold settings
465 | // bool apply_dgps; // Apply DGPS settings.
466 | // bool apply_cno; // Apply CNO threshold settings (cnoThresh, cnoThreshNumSVs).
467 | // bool apply_utc; // Apply UTC settings
468 | //
469 | // /*
470 | // * Dynamic platform model:
471 | // * 0: portable
472 | // * 2: stationary
473 | // * 3: pedestrian
474 | // * 4: automotive
475 | // * 5: sea
476 | // * 6: airborne with <1g acceleration
477 | // * 7: airborne with <2g acceleration
478 | // * 8: airborne with <4g acceleration
479 | // * 9: wrist worn watch
480 | // */
481 | // uint8_t dyn_model;
482 | //
483 | // /*
484 | // * Position Fixing Mode:
485 | // * 1: 2D only
486 | // * 2: 3D only
487 | // * 3: auto 2D/3D
488 | // */
489 | // uint8_t fix_mode;
490 | //
491 | // double fixed_alt; // Fixed altitude (mean sea level) for 2D fix mode. (m)
492 | // double fixed_alt_var; // Fixed altitude variance for 2D mode. (m^2)
493 | // int8_t min_elev; // Minimum Elevation for a GNSS satellite to be used in NAV (deg)
494 | // float p_dop; // Position DOP Mask to use
495 | // float t_dop; // Time DOP Mask to use
496 | // uint16_t p_acc; // Position Accuracy Mask (m)
497 | // uint16_t t_acc; // Time Accuracy Mask (m)
498 | // uint8_t static_hold_thres; // Static hold threshold (cm/s)
499 | // uint8_t dgnss_timeout; // DGNSS (RTK) timeout (s)
500 | // uint8_t cno_tres_num_sat; // Number of satellites required to have C/N0 above cnoThresh for a fix to be attempted
501 | // uint8_t cno_tres; // C/N0 threshold for deciding whether to attempt a fix (dBHz)
502 | // uint16_t static_hold_max_dist; // Static hold distance threshold (before quitting static hold) (m)
503 | //
504 | // /*
505 | // * UTC standard to be used:
506 | // * 0: Automatic; receiver selects based on GNSS configuration (see GNSS time bases).
507 | // * 3: UTC as operated by the U.S. Naval Observatory (USNO); derived from GPS time
508 | // * 6: UTC as operated by the former Soviet Union; derived from GLONASS time
509 | // * 7: UTC as operated by the National Time Service Center, China; derived from BeiDou time
510 | // */
511 | // uint8_t utc_standard;
512 | // } ubx_cfg_nav5;
513 |
514 | // typedef struct {
515 | // uint8_t tp_idx; // Timepulse selection. 0=TP1, 1=TP2
516 | // int16_t ant_cable_delay; // Antenna cable delay in ns
517 | // int16_t rf_group_delay; // RF group delay in ns
518 | // uint32_t freq_period; // Frequency or time period, Hz or us
519 | // uint32_t freq_period_lock; // Frequency or time period when locked to GNSS time, Hz or us
520 | // uint32_t pulse_len_ratio; // Pulse length or duty cycle, us or 2^-32
521 | // uint32_t pulse_len_ratio_lock; // Pulse length or duty cycle when locked to GNSS time, us or 2^-32
522 | // int32_t user_config_delay; // User configurable time pulse delay, ns
523 | //
524 | // /*
525 | // * If set enable time pulse; if pin assigned to another function, other function takes
526 | // * precedence. Must be set for FTS variant.
527 | // */
528 | // bool active;
529 | //
530 | // /*
531 | // * If set synchronize time pulse to GNSS as soon as GNSS time is valid. If not
532 | // * set, or before GNSS time is valid use local clock. This flag is ignored by
533 | // * the FTS product variant; in this case the receiver always locks to the best
534 | // * available time/frequency reference (which is not necessarily GNSS).
535 | // */
536 | // bool lockGnssFreq;
537 | //
538 | // /*
539 | // * If set the receiver switches between the timepulse settings given by 'freqPeriodLocked' &
540 | // * 'pulseLenLocked' and those given by 'freqPeriod' & 'pulseLen'. The 'Locked' settings are
541 | // * used where the receiver has an accurate sense of time. For non-FTS products, this occurs
542 | // * when GNSS solution with a reliable time is available, but for FTS products the setting syncMode
543 | // * field governs behavior. In all cases, the receiver only uses 'freqPeriod' & 'pulseLen' when
544 | // * the flag is unset.
545 | // */
546 | // bool lockedOtherSet;
547 | //
548 | // /*
549 | // * If set 'freqPeriodLock' and 'freqPeriod' are interpreted as frequency,
550 | // * otherwise interpreted as period.
551 | // */
552 | // bool isFreq;
553 | //
554 | // /*
555 | // * If set 'pulseLenRatioLock' and 'pulseLenRatio' interpreted as pulse
556 | // * length, otherwise interpreted as duty cycle.
557 | // */
558 | // bool isLength;
559 | //
560 | // /*
561 | // * Align pulse to top of second (period time must be integer fraction of 1s).
562 | // * Also set 'lockGnssFreq' to use this feature.
563 | // * This flag is ignored by the FTS product variant; it is assumed to be always set
564 | // * (as is lockGnssFreq). Set maxSlewRate and maxPhaseCorrRate fields of CFG-SMGR to
565 | // * 0 to disable alignment.
566 | // */
567 | // bool alignToTow;
568 | //
569 | // /*
570 | // * Pulse polarity:
571 | // * 0: falling edge at top of second
572 | // * 1: rising edge at top of second
573 | // */
574 | // bool polarity;
575 | //
576 | // /*
577 | // * Timegrid to use:
578 | // * 0: UTC
579 | // * 1: GPS
580 | // * 2: GLONASS
581 | // * 3: BeiDou
582 | // * 4: Galileo (not supported in protocol versions less than 18)
583 | // * This flag is only relevant if 'lockGnssFreq' and 'alignToTow' are set.
584 | // * Note that configured GNSS time is estimated by the receiver if locked to
585 | // * any GNSS system. If the receiver has a valid GNSS fix it will attempt to
586 | // * steer the TP to the specified time grid even if the specified time is not
587 | // * based on information from the constellation's satellites. To ensure timing
588 | // * based purely on a given GNSS, restrict the supported constellations in CFG-GNSS.
589 | // */
590 | // uint8_t gridUtcGnss;
591 | //
592 | // /*
593 | // * Sync Manager lock mode to use:
594 | // *
595 | // * 0: switch to 'freqPeriodLock' and 'pulseLenRatioLock' as soon as Sync
596 | // * Manager has an accurate time, never switch back to 'freqPeriod' and 'pulseLenRatio'
597 | // *
598 | // * 1: switch to 'freqPeriodLock' and 'pulseLenRatioLock' as soon as Sync Manager has
599 | // * an accurate time, and switch back to 'freqPeriod' and 'pulseLenRatio' as soon as
600 | // * time gets inaccurate.
601 | // *
602 | // * This field is only relevant for the FTS product variant.
603 | // * This field is only relevant if the flag 'lockedOtherSet' is set.
604 | // */
605 | // uint8_t syncMode;
606 | // } ubx_cfg_tp5;
607 |
608 | // ============== RTCM Datatypes ================== //
609 |
610 | // typedef struct {
611 | // double t_tow; // Time of week (GPS)
612 | // double t_tod; // Time of day (GLONASS)
613 | // double t_wn; // Week number
614 | // int staid; // ref station id
615 | // bool sync; // True if more messages are coming
616 | // int type; // RTCM Type
617 | // } rtcm_obs_header_t;
618 |
619 | // typedef struct {
620 | // double P[2]; // Pseudorange observation
621 | // double L[2]; // Carrier phase observation
622 | // uint8_t cn0[2]; // Carrier-to-Noise density [dB Hz]
623 | // uint8_t lock[2]; // Lock. Set to 0 when the lock has changed, 127 otherwise. TODO: is this correct?
624 | // uint8_t prn; // Sattelite
625 | // uint8_t freq; // Frequency slot (GLONASS)
626 | // uint8_t code[2]; // Code indicator
627 | // } rtcm_obs_t;
628 |
629 | // typedef struct {
630 | // int staid;
631 | // double lat;
632 | // double lon;
633 | // double height;
634 | // double ant_height;
635 | // } rtcm_ref_sta_pos_t;
636 |
637 | // typedef struct {
638 | // double tgd; // Group delay differential between L1 and L2 [s]
639 | // double c_rs; // Amplitude of the sine harmonic correction term to the orbit radius [m]
640 | // double c_rc; // Amplitude of the cosine harmonic correction term to the orbit radius [m]
641 | // double c_uc; // Amplitude of the cosine harmonic correction term to the argument of latitude [rad]
642 | // double c_us; // Amplitude of the sine harmonic correction term to the argument of latitude [rad]
643 | // double c_ic; // Amplitude of the cosine harmonic correction term to the angle of inclination [rad]
644 | // double c_is; // Amplitude of the sine harmonic correction term to the angle of inclination [rad]
645 | // double dn; // Mean motion difference [rad/s]
646 | // double m0; // Mean anomaly at reference time [radians]
647 | // double ecc; // Eccentricity of satellite orbit
648 | // double sqrta; // Square root of the semi-major axis of orbit [m^(1/2)]
649 | // double omega0; // Longitude of ascending node of orbit plane at weekly epoch [rad]
650 | // double omegadot; // Rate of right ascension [rad/s]
651 | // double w; // Argument of perigee [rad]
652 | // double inc; // Inclination [rad]
653 | // double inc_dot; // Inclination first derivative [rad/s]
654 | // double af0; // Polynomial clock correction coefficient (clock bias) [s]
655 | // double af1; // Polynomial clock correction coefficient (clock drift) [s/s]
656 | // double af2; // Polynomial clock correction coefficient (rate of clock drift) [s/s^2]
657 | // double toe_tow; // Time of week [s]
658 | // uint16_t toe_wn; // Week number [week]
659 | // double toc_tow; // Clock reference time of week [s]
660 | // int sva; // SV accuracy (URA index)
661 | // int svh; // SV health (0:ok)
662 | // int code; // GPS/QZS: code on L2, GAL/CMP: data sources
663 | // int flag; // GPS/QZS: L2 P data flag, CMP: nav type
664 | // double fit; // fit interval (h)
665 | // uint8_t prn; // Sattelite
666 | // uint8_t iode; // Issue of ephemeris data
667 | // uint16_t iodc; // Issue of clock data
668 | // } rtcm_ephemeris_t;
669 |
670 | // typedef struct {
671 | // int buffer_ptr;
672 | // int len;
673 | // uint8_t buffer[1100];
674 | // rtcm_obs_header_t header;
675 | // rtcm_obs_t obs[32]; // 32 observations per sat system should be more than enough
676 | // rtcm_ref_sta_pos_t pos;
677 | // rtcm_ephemeris_t eph;
678 | // void(*rx_rtcm_obs)(rtcm_obs_header_t *header, rtcm_obs_t *obs, int obs_num);
679 | // void(*rx_rtcm_1005_1006)(rtcm_ref_sta_pos_t *pos);
680 | // void(*rx_rtcm_1019)(rtcm_ephemeris_t *eph);
681 | // void(*rx_rtcm)(uint8_t *data, int len, int type);
682 | // } rtcm3_state;
683 |
684 | // ============== Radar Datatypes ================== //
685 |
686 | // typedef struct {
687 | // bool log_en;
688 | // float f_center;
689 | // float f_span;
690 | // int points;
691 | // float t_sweep;
692 | // float cc_x;
693 | // float cc_y;
694 | // float cc_rad;
695 | // int log_rate_ms;
696 | // float map_plot_avg_factor;
697 | // float map_plot_max_div;
698 | // int plot_mode; // 0 = off, 1 = sample, 2 = fft
699 | // int map_plot_start;
700 | // int map_plot_end;
701 | // } radar_settings_t;
702 |
703 | // ============== VESC Datatypes ================== //
704 |
705 | // CAN status sent by VESC
706 | // typedef struct {
707 | // int id;
708 | // uint32_t rx_time;
709 | // float rpm;
710 | // float current;
711 | // float duty;
712 | // } can_status_msg;
713 |
714 | // typedef enum {
715 | // PWM_MODE_NONSYNCHRONOUS_HISW = 0, // This mode is not recommended
716 | // PWM_MODE_SYNCHRONOUS, // The recommended and most tested mode
717 | // PWM_MODE_BIPOLAR // Some glitches occasionally, can kill MOSFETs
718 | // } mc_pwm_mode;
719 |
720 | // typedef enum {
721 | // COMM_MODE_INTEGRATE = 0,
722 | // COMM_MODE_DELAY
723 | // } mc_comm_mode;
724 |
725 | // typedef enum {
726 | // SENSOR_MODE_SENSORLESS = 0,
727 | // SENSOR_MODE_SENSORED,
728 | // SENSOR_MODE_HYBRID
729 | // } mc_sensor_mode;
730 |
731 | // typedef enum {
732 | // FOC_SENSOR_MODE_SENSORLESS = 0,
733 | // FOC_SENSOR_MODE_ENCODER,
734 | // FOC_SENSOR_MODE_HALL
735 | // } mc_foc_sensor_mode;
736 |
737 | // typedef enum {
738 | // MOTOR_TYPE_BLDC = 0,
739 | // MOTOR_TYPE_DC,
740 | // MOTOR_TYPE_FOC
741 | // } mc_motor_type;
742 |
743 | typedef enum {
744 | FAULT_CODE_NONE = 0,
745 | FAULT_CODE_OVER_VOLTAGE,
746 | FAULT_CODE_UNDER_VOLTAGE,
747 | FAULT_CODE_DRV,
748 | FAULT_CODE_ABS_OVER_CURRENT,
749 | FAULT_CODE_OVER_TEMP_FET,
750 | FAULT_CODE_OVER_TEMP_MOTOR
751 | } mc_fault_code;
752 |
753 | typedef struct {
754 | float v_in;
755 | float temp_mos;
756 | float temp_motor;
757 | float current_motor;
758 | float current_in;
759 | float id;
760 | float iq;
761 | float rpm;
762 | float duty_now;
763 | float amp_hours;
764 | float amp_hours_charged;
765 | float watt_hours;
766 | float watt_hours_charged;
767 | int tachometer;
768 | int tachometer_abs;
769 | mc_fault_code fault_code;
770 | } mc_values;
771 |
772 | // typedef enum {
773 | // SENSOR_PORT_MODE_HALL = 0,
774 | // SENSOR_PORT_MODE_ABI,
775 | // SENSOR_PORT_MODE_AS5047_SPI
776 | // } sensor_port_mode;
777 |
778 | // typedef enum {
779 | // DRV8301_OC_LIMIT = 0,
780 | // DRV8301_OC_LATCH_SHUTDOWN,
781 | // DRV8301_OC_REPORT_ONLY,
782 | // DRV8301_OC_DISABLED
783 | // } drv8301_oc_mode;
784 |
785 | typedef enum {
786 | MOTOR_CONTROL_DUTY = 0,
787 | MOTOR_CONTROL_CURRENT,
788 | MOTOR_CONTROL_CURRENT_BRAKE,
789 | MOTOR_CONTROL_RPM,
790 | MOTOR_CONTROL_POS
791 | } motor_control_mode;
792 |
793 | // typedef struct {
794 | // // Switching and drive
795 | // mc_pwm_mode pwm_mode;
796 | // mc_comm_mode comm_mode;
797 | // mc_motor_type motor_type;
798 | // mc_sensor_mode sensor_mode;
799 | // // Limits
800 | // float l_current_max;
801 | // float l_current_min;
802 | // float l_in_current_max;
803 | // float l_in_current_min;
804 | // float l_abs_current_max;
805 | // float l_min_erpm;
806 | // float l_max_erpm;
807 | // float l_erpm_start;
808 | // float l_max_erpm_fbrake;
809 | // float l_max_erpm_fbrake_cc;
810 | // float l_min_vin;
811 | // float l_max_vin;
812 | // float l_battery_cut_start;
813 | // float l_battery_cut_end;
814 | // bool l_slow_abs_current;
815 | // float l_temp_fet_start;
816 | // float l_temp_fet_end;
817 | // float l_temp_motor_start;
818 | // float l_temp_motor_end;
819 | // float l_min_duty;
820 | // float l_max_duty;
821 | // float l_watt_max;
822 | // float l_watt_min;
823 | // // Overridden limits (Computed during runtime)
824 | // float lo_current_max;
825 | // float lo_current_min;
826 | // float lo_in_current_max;
827 | // float lo_in_current_min;
828 | // float lo_current_motor_max_now;
829 | // float lo_current_motor_min_now;
830 | // // Sensorless (bldc)
831 | // float sl_min_erpm;
832 | // float sl_min_erpm_cycle_int_limit;
833 | // float sl_max_fullbreak_current_dir_change;
834 | // float sl_cycle_int_limit;
835 | // float sl_phase_advance_at_br;
836 | // float sl_cycle_int_rpm_br;
837 | // float sl_bemf_coupling_k;
838 | // // Hall sensor
839 | // int8_t hall_table[8];
840 | // float hall_sl_erpm;
841 | // // FOC
842 | // float foc_current_kp;
843 | // float foc_current_ki;
844 | // float foc_f_sw;
845 | // float foc_dt_us;
846 | // float foc_encoder_offset;
847 | // bool foc_encoder_inverted;
848 | // float foc_encoder_ratio;
849 | // float foc_motor_l;
850 | // float foc_motor_r;
851 | // float foc_motor_flux_linkage;
852 | // float foc_observer_gain;
853 | // float foc_observer_gain_slow;
854 | // float foc_pll_kp;
855 | // float foc_pll_ki;
856 | // float foc_duty_dowmramp_kp;
857 | // float foc_duty_dowmramp_ki;
858 | // float foc_openloop_rpm;
859 | // float foc_sl_openloop_hyst;
860 | // float foc_sl_openloop_time;
861 | // float foc_sl_d_current_duty;
862 | // float foc_sl_d_current_factor;
863 | // mc_foc_sensor_mode foc_sensor_mode;
864 | // uint8_t foc_hall_table[8];
865 | // float foc_sl_erpm;
866 | // bool foc_sample_v0_v7;
867 | // float foc_sat_comp;
868 | // bool foc_temp_comp;
869 | // float foc_temp_comp_base_temp;
870 | // // Speed PID
871 | // float s_pid_kp;
872 | // float s_pid_ki;
873 | // float s_pid_kd;
874 | // float s_pid_min_erpm;
875 | // bool s_pid_allow_braking;
876 | // // Pos PID
877 | // float p_pid_kp;
878 | // float p_pid_ki;
879 | // float p_pid_kd;
880 | // float p_pid_ang_div;
881 | // // Current controller
882 | // float cc_startup_boost_duty;
883 | // float cc_min_current;
884 | // float cc_gain;
885 | // float cc_ramp_step_max;
886 | // // Misc
887 | // int32_t m_fault_stop_time_ms;
888 | // float m_duty_ramp_step;
889 | // float m_current_backoff_gain;
890 | // uint32_t m_encoder_counts;
891 | // sensor_port_mode m_sensor_port_mode;
892 | // bool m_invert_direction;
893 | // drv8301_oc_mode m_drv8301_oc_mode;
894 | // int m_drv8301_oc_adj;
895 | // float m_bldc_f_sw_min;
896 | // float m_bldc_f_sw_max;
897 | // float m_dc_f_sw;
898 | // } mc_configuration;
899 |
900 | // Applications to use
901 | // typedef enum {
902 | // APP_NONE = 0,
903 | // APP_PPM,
904 | // APP_ADC,
905 | // APP_UART,
906 | // APP_PPM_UART,
907 | // APP_ADC_UART,
908 | // APP_NUNCHUK,
909 | // APP_NRF,
910 | // APP_CUSTOM
911 | // } app_use;
912 |
913 | // Throttle curve mode
914 | // typedef enum {
915 | // THR_EXP_EXPO = 0,
916 | // THR_EXP_NATURAL,
917 | // THR_EXP_POLY
918 | // } thr_exp_mode;
919 |
920 | // PPM control types
921 | // typedef enum {
922 | // PPM_CTRL_TYPE_NONE = 0,
923 | // PPM_CTRL_TYPE_CURRENT,
924 | // PPM_CTRL_TYPE_CURRENT_NOREV,
925 | // PPM_CTRL_TYPE_CURRENT_NOREV_BRAKE,
926 | // PPM_CTRL_TYPE_DUTY,
927 | // PPM_CTRL_TYPE_DUTY_NOREV,
928 | // PPM_CTRL_TYPE_PID,
929 | // PPM_CTRL_TYPE_PID_NOREV
930 | // } ppm_control_type;
931 |
932 | // typedef struct {
933 | // ppm_control_type ctrl_type;
934 | // float pid_max_erpm;
935 | // float hyst;
936 | // float pulse_start;
937 | // float pulse_end;
938 | // float pulse_center;
939 | // bool median_filter;
940 | // bool safe_start;
941 | // float throttle_exp;
942 | // thr_exp_mode throttle_exp_mode;
943 | // float ramp_time_pos;
944 | // float ramp_time_neg;
945 | // bool multi_esc;
946 | // bool tc;
947 | // float tc_max_diff;
948 | // } ppm_config;
949 |
950 | // ADC control types
951 | // typedef enum {
952 | // ADC_CTRL_TYPE_NONE = 0,
953 | // ADC_CTRL_TYPE_CURRENT,
954 | // ADC_CTRL_TYPE_CURRENT_REV_CENTER,
955 | // ADC_CTRL_TYPE_CURRENT_REV_BUTTON,
956 | // ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_CENTER,
957 | // ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_BUTTON,
958 | // ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_ADC,
959 | // ADC_CTRL_TYPE_DUTY,
960 | // ADC_CTRL_TYPE_DUTY_REV_CENTER,
961 | // ADC_CTRL_TYPE_DUTY_REV_BUTTON
962 | // } adc_control_type;
963 |
964 | // typedef struct {
965 | // adc_control_type ctrl_type;
966 | // float hyst;
967 | // float voltage_start;
968 | // float voltage_end;
969 | // float voltage_center;
970 | // float voltage2_start;
971 | // float voltage2_end;
972 | // bool use_filter;
973 | // bool safe_start;
974 | // bool cc_button_inverted;
975 | // bool rev_button_inverted;
976 | // bool voltage_inverted;
977 | // bool voltage2_inverted;
978 | // float throttle_exp;
979 | // thr_exp_mode throttle_exp_mode;
980 | // bool multi_esc;
981 | // bool tc;
982 | // float tc_max_diff;
983 | // uint32_t update_rate_hz;
984 | // } adc_config;
985 |
986 | // Nunchuk control types
987 | // typedef enum {
988 | // CHUK_CTRL_TYPE_NONE = 0,
989 | // CHUK_CTRL_TYPE_CURRENT,
990 | // CHUK_CTRL_TYPE_CURRENT_NOREV
991 | // } chuk_control_type;
992 |
993 | // typedef struct {
994 | // chuk_control_type ctrl_type;
995 | // float hyst;
996 | // float ramp_time_pos;
997 | // float ramp_time_neg;
998 | // float stick_erpm_per_s_in_cc;
999 | // float throttle_exp;
1000 | // thr_exp_mode throttle_exp_mode;
1001 | // bool multi_esc;
1002 | // bool tc;
1003 | // float tc_max_diff;
1004 | // } chuk_config;
1005 |
1006 | // NRF Datatypes
1007 | // typedef enum {
1008 | // NRF_SPEED_250K = 0,
1009 | // NRF_SPEED_1M,
1010 | // NRF_SPEED_2M
1011 | // } NRF_SPEED;
1012 |
1013 | // typedef enum {
1014 | // NRF_POWER_M18DBM = 0,
1015 | // NRF_POWER_M12DBM,
1016 | // NRF_POWER_M6DBM,
1017 | // NRF_POWER_0DBM
1018 | // } NRF_POWER;
1019 |
1020 | // typedef enum {
1021 | // NRF_AW_3 = 0,
1022 | // NRF_AW_4,
1023 | // NRF_AW_5
1024 | // } NRF_AW;
1025 |
1026 | // typedef enum {
1027 | // NRF_CRC_DISABLED = 0,
1028 | // NRF_CRC_1B,
1029 | // NRF_CRC_2B
1030 | // } NRF_CRC;
1031 |
1032 | // typedef enum {
1033 | // NRF_RETR_DELAY_250US = 0,
1034 | // NRF_RETR_DELAY_500US,
1035 | // NRF_RETR_DELAY_750US,
1036 | // NRF_RETR_DELAY_1000US,
1037 | // NRF_RETR_DELAY_1250US,
1038 | // NRF_RETR_DELAY_1500US,
1039 | // NRF_RETR_DELAY_1750US,
1040 | // NRF_RETR_DELAY_2000US,
1041 | // NRF_RETR_DELAY_2250US,
1042 | // NRF_RETR_DELAY_2500US,
1043 | // NRF_RETR_DELAY_2750US,
1044 | // NRF_RETR_DELAY_3000US,
1045 | // NRF_RETR_DELAY_3250US,
1046 | // NRF_RETR_DELAY_3500US,
1047 | // NRF_RETR_DELAY_3750US,
1048 | // NRF_RETR_DELAY_4000US
1049 | // } NRF_RETR_DELAY;
1050 |
1051 | // typedef struct {
1052 | // NRF_SPEED speed;
1053 | // NRF_POWER power;
1054 | // NRF_CRC crc_type;
1055 | // NRF_RETR_DELAY retry_delay;
1056 | // unsigned char retries;
1057 | // unsigned char channel;
1058 | // unsigned char address[3];
1059 | // bool send_crc_ack;
1060 | // } nrf_config;
1061 |
1062 | // typedef struct {
1063 | // // Settings
1064 | // uint8_t controller_id;
1065 | // uint32_t timeout_msec;
1066 | // float timeout_brake_current;
1067 | // bool send_can_status;
1068 | // uint32_t send_can_status_rate_hz;
1069 | //
1070 | // // Application to use
1071 | // app_use app_to_use;
1072 | //
1073 | // // PPM application settings
1074 | // ppm_config app_ppm_conf;
1075 | //
1076 | // // ADC application settings
1077 | // adc_config app_adc_conf;
1078 | //
1079 | // // UART application settings
1080 | // uint32_t app_uart_baudrate;
1081 | //
1082 | // // Nunchuk application settings
1083 | // chuk_config app_chuk_conf;
1084 | //
1085 | // // NRF application settings
1086 | // nrf_config app_nrf_conf;
1087 | // } app_configuration;
1088 |
1089 | // Communication commands
1090 | typedef enum {
1091 | COMM_FW_VERSION = 0,
1092 | COMM_JUMP_TO_BOOTLOADER,
1093 | COMM_ERASE_NEW_APP,
1094 | COMM_WRITE_NEW_APP_DATA,
1095 | COMM_GET_VALUES,
1096 | COMM_SET_DUTY,
1097 | COMM_SET_CURRENT,
1098 | COMM_SET_CURRENT_BRAKE,
1099 | COMM_SET_RPM,
1100 | COMM_SET_POS,
1101 | COMM_SET_HANDBRAKE,
1102 | COMM_SET_DETECT,
1103 | COMM_SET_SERVO_POS,
1104 | COMM_SET_MCCONF,
1105 | COMM_GET_MCCONF,
1106 | COMM_GET_MCCONF_DEFAULT,
1107 | COMM_SET_APPCONF,
1108 | COMM_GET_APPCONF,
1109 | COMM_GET_APPCONF_DEFAULT,
1110 | COMM_SAMPLE_PRINT,
1111 | COMM_TERMINAL_CMD,
1112 | COMM_PRINT,
1113 | COMM_ROTOR_POSITION,
1114 | COMM_EXPERIMENT_SAMPLE,
1115 | COMM_DETECT_MOTOR_PARAM,
1116 | COMM_DETECT_MOTOR_R_L,
1117 | COMM_DETECT_MOTOR_FLUX_LINKAGE,
1118 | COMM_DETECT_ENCODER,
1119 | COMM_DETECT_HALL_FOC,
1120 | COMM_REBOOT,
1121 | COMM_ALIVE,
1122 | COMM_GET_DECODED_PPM,
1123 | COMM_GET_DECODED_ADC,
1124 | COMM_GET_DECODED_CHUK,
1125 | COMM_FORWARD_CAN,
1126 | COMM_SET_CHUCK_DATA,
1127 | COMM_CUSTOM_APP_DATA,
1128 | COMM_NRF_START_PAIRING
1129 | } COMM_PACKET_ID;
1130 |
1131 | // ============== Decawave Datatypes ================== //
1132 |
1133 | // typedef enum {
1134 | // CMD_DW_RANGE = 0
1135 | // } CMD_DW;
1136 |
1137 | #endif /* DATATYPES_H_ */
1138 |
--------------------------------------------------------------------------------
/lib/bldc_uart/bldc_interface.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016-2017 Benjamin Vedder benjamin@vedder.se
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 | */
17 |
18 | /*
19 | * bldc_interface.c
20 | *
21 | * Compatible Firmware Versions
22 | * 3.20
23 | * 3.21
24 | * 3.22
25 | *
26 | */
27 |
28 | #include "bldc_interface.h"
29 | #include "buffer.h"
30 | #include
31 |
32 | // Private variables
33 | // static unsigned char send_buffer[1024];
34 | static unsigned char send_buffer[128];
35 |
36 | // Private variables for received data
37 | static mc_values values;
38 | // static mc_values values;
39 | // static int fw_major;
40 | // static int fw_minor;
41 | // static float rotor_pos;
42 | // static mc_configuration mcconf;
43 | // static app_configuration appconf;
44 | // static float detect_cycle_int_limit;
45 | // static float detect_coupling_k;
46 | // static signed char detect_hall_table[8];
47 | // static signed char detect_hall_res;
48 | // static float dec_ppm;
49 | // static float dec_ppm_len;
50 | // static float dec_adc;
51 | // static float dec_adc_voltage;
52 | // static float dec_chuk;
53 |
54 | // Private functions
55 | void send_packet_no_fwd(unsigned char *data, unsigned int len);
56 |
57 | // Function pointers
58 | static void(*send_func)(unsigned char *data, unsigned int len) = 0;
59 | static void(*forward_func)(unsigned char *data, unsigned int len) = 0;
60 |
61 | // Function pointers for received data
62 | static void(*rx_value_func)(mc_values *values) = 0;
63 | // static void(*rx_printf_func)(char *str) = 0;
64 | // static void(*rx_fw_func)(int major, int minor) = 0;
65 | // static void(*rx_rotor_pos_func)(float pos) = 0;
66 | // static void(*rx_mcconf_func)(mc_configuration *conf) = 0;
67 | // static void(*rx_appconf_func)(app_configuration *conf) = 0;
68 | // static void(*rx_detect_func)(float cycle_int_limit, float coupling_k,
69 | // const signed char *hall_table, signed char hall_res) = 0;
70 | // static void(*rx_dec_ppm_func)(float val, float ms) = 0;
71 | // static void(*rx_dec_adc_func)(float val, float voltage) = 0;
72 | // static void(*rx_dec_chuk_func)(float val) = 0;
73 | // static void(*rx_mcconf_received_func)(void) = 0;
74 | // static void(*rx_appconf_received_func)(void) = 0;
75 | static void(*motor_control_set_func)(motor_control_mode mode, float value) = 0;
76 | static void(*values_requested_func)(void) = 0;
77 |
78 | void bldc_interface_init(void(*func)(unsigned char *data, unsigned int len)) {
79 | send_func = func;
80 | }
81 |
82 | void bldc_interface_set_forward_func(void(*func)(unsigned char *data, unsigned int len)) {
83 | forward_func = func;
84 | }
85 |
86 | /**
87 | * Send a packet using the set send function.
88 | *
89 | * @param data
90 | * The packet data.
91 | *
92 | * @param len
93 | * The data length.
94 | */
95 | void bldc_interface_send_packet(unsigned char *data, unsigned int len) {
96 | if (send_func) {
97 | send_func(data, len);
98 | }
99 | }
100 |
101 | /**
102 | * Process a received buffer with commands and data.
103 | *
104 | * @param data
105 | * The buffer to process.
106 | *
107 | * @param len
108 | * The length of the buffer.
109 | */
110 | void bldc_interface_process_packet(unsigned char *data, unsigned int len) {
111 | if (!len) {
112 | return;
113 | }
114 |
115 | if (forward_func) {
116 | forward_func(data, len);
117 | return;
118 | }
119 |
120 | int32_t ind = 0;
121 | int i = 0;
122 | unsigned char id = data[0];
123 | data++;
124 | len--;
125 |
126 | switch (id) {
127 | // case COMM_FW_VERSION:
128 | // if (len == 2) {
129 | // ind = 0;
130 | // fw_major = data[ind++];
131 | // fw_minor = data[ind++];
132 | // } else {
133 | // fw_major = -1;
134 | // fw_minor = -1;
135 | // }
136 | // break;
137 |
138 | // case COMM_ERASE_NEW_APP:
139 | // case COMM_WRITE_NEW_APP_DATA:
140 | // // TODO
141 | // break;
142 |
143 | case COMM_GET_VALUES:
144 | ind = 0;
145 | values.temp_mos = buffer_get_float16(data, 1e1, &ind);
146 | values.temp_motor = buffer_get_float16(data, 1e1, &ind);
147 | values.current_motor = buffer_get_float32(data, 1e2, &ind);
148 | values.current_in = buffer_get_float32(data, 1e2, &ind);
149 | values.id = buffer_get_float32(data, 1e2, &ind);
150 | values.iq = buffer_get_float32(data, 1e2, &ind);
151 | values.duty_now = buffer_get_float16(data, 1e3, &ind);
152 | values.rpm = buffer_get_float32(data, 1e0, &ind);
153 | values.v_in = buffer_get_float16(data, 1e1, &ind);
154 | values.amp_hours = buffer_get_float32(data, 1e4, &ind);
155 | values.amp_hours_charged = buffer_get_float32(data, 1e4, &ind);
156 | values.watt_hours = buffer_get_float32(data, 1e4, &ind);
157 | values.watt_hours_charged = buffer_get_float32(data, 1e4, &ind);
158 | values.tachometer = buffer_get_int32(data, &ind);
159 | values.tachometer_abs = buffer_get_int32(data, &ind);
160 | values.fault_code = (mc_fault_code)data[ind++];
161 |
162 | if (rx_value_func) {
163 | rx_value_func(&values);
164 | }
165 | break;
166 |
167 | // case COMM_PRINT:
168 | // if (rx_printf_func) {
169 | // data[len] = '\0';
170 | // rx_printf_func((char*)data);
171 | // }
172 | // break;
173 | //
174 | // case COMM_SAMPLE_PRINT:
175 | // // TODO
176 | // break;
177 | //
178 | // case COMM_ROTOR_POSITION:
179 | // ind = 0;
180 | // rotor_pos = buffer_get_float32(data, 100000.0, &ind);
181 | //
182 | // if (rx_rotor_pos_func) {
183 | // rx_rotor_pos_func(rotor_pos);
184 | // }
185 | // break;
186 | //
187 | // case COMM_EXPERIMENT_SAMPLE:
188 | // // TODO
189 | // break;
190 | //
191 | // case COMM_GET_MCCONF:
192 | // case COMM_GET_MCCONF_DEFAULT:
193 | // ind = 0;
194 | // mcconf.pwm_mode = data[ind++];
195 | // mcconf.comm_mode = data[ind++];
196 | // mcconf.motor_type = data[ind++];
197 | // mcconf.sensor_mode = data[ind++];
198 | //
199 | // mcconf.l_current_max = buffer_get_float32_auto(data, &ind);
200 | // mcconf.l_current_min = buffer_get_float32_auto(data, &ind);
201 | // mcconf.l_in_current_max = buffer_get_float32_auto(data, &ind);
202 | // mcconf.l_in_current_min = buffer_get_float32_auto(data, &ind);
203 | // mcconf.l_abs_current_max = buffer_get_float32_auto(data, &ind);
204 | // mcconf.l_min_erpm = buffer_get_float32_auto(data, &ind);
205 | // mcconf.l_max_erpm = buffer_get_float32_auto(data, &ind);
206 | // mcconf.l_erpm_start = buffer_get_float32_auto(data, &ind);
207 | // mcconf.l_max_erpm_fbrake = buffer_get_float32_auto(data, &ind);
208 | // mcconf.l_max_erpm_fbrake_cc = buffer_get_float32_auto(data, &ind);
209 | // mcconf.l_min_vin = buffer_get_float32_auto(data, &ind);
210 | // mcconf.l_max_vin = buffer_get_float32_auto(data, &ind);
211 | // mcconf.l_battery_cut_start = buffer_get_float32_auto(data, &ind);
212 | // mcconf.l_battery_cut_end = buffer_get_float32_auto(data, &ind);
213 | // mcconf.l_slow_abs_current = data[ind++];
214 | // mcconf.l_temp_fet_start = buffer_get_float32_auto(data, &ind);
215 | // mcconf.l_temp_fet_end = buffer_get_float32_auto(data, &ind);
216 | // mcconf.l_temp_motor_start = buffer_get_float32_auto(data, &ind);
217 | // mcconf.l_temp_motor_end = buffer_get_float32_auto(data, &ind);
218 | // mcconf.l_min_duty = buffer_get_float32_auto(data, &ind);
219 | // mcconf.l_max_duty = buffer_get_float32_auto(data, &ind);
220 | // mcconf.l_watt_max = buffer_get_float32_auto(data, &ind);
221 | // mcconf.l_watt_min = buffer_get_float32_auto(data, &ind);
222 | //
223 | // mcconf.lo_current_max = mcconf.l_current_max;
224 | // mcconf.lo_current_min = mcconf.l_current_min;
225 | // mcconf.lo_in_current_max = mcconf.l_in_current_max;
226 | // mcconf.lo_in_current_min = mcconf.l_in_current_min;
227 | // mcconf.lo_current_motor_max_now = mcconf.l_current_max;
228 | // mcconf.lo_current_motor_min_now = mcconf.l_current_min;
229 | //
230 | // mcconf.sl_min_erpm = buffer_get_float32_auto(data, &ind);
231 | // mcconf.sl_min_erpm_cycle_int_limit = buffer_get_float32_auto(data, &ind);
232 | // mcconf.sl_max_fullbreak_current_dir_change = buffer_get_float32_auto(data, &ind);
233 | // mcconf.sl_cycle_int_limit = buffer_get_float32_auto(data, &ind);
234 | // mcconf.sl_phase_advance_at_br = buffer_get_float32_auto(data, &ind);
235 | // mcconf.sl_cycle_int_rpm_br = buffer_get_float32_auto(data, &ind);
236 | // mcconf.sl_bemf_coupling_k = buffer_get_float32_auto(data, &ind);
237 | //
238 | // memcpy(mcconf.hall_table, data + ind, 8);
239 | // ind += 8;
240 | // mcconf.hall_sl_erpm = buffer_get_float32_auto(data, &ind);
241 | //
242 | // mcconf.foc_current_kp = buffer_get_float32_auto(data, &ind);
243 | // mcconf.foc_current_ki = buffer_get_float32_auto(data, &ind);
244 | // mcconf.foc_f_sw = buffer_get_float32_auto(data, &ind);
245 | // mcconf.foc_dt_us = buffer_get_float32_auto(data, &ind);
246 | // mcconf.foc_encoder_inverted = data[ind++];
247 | // mcconf.foc_encoder_offset = buffer_get_float32_auto(data, &ind);
248 | // mcconf.foc_encoder_ratio = buffer_get_float32_auto(data, &ind);
249 | // mcconf.foc_sensor_mode = data[ind++];
250 | // mcconf.foc_pll_kp = buffer_get_float32_auto(data, &ind);
251 | // mcconf.foc_pll_ki = buffer_get_float32_auto(data, &ind);
252 | // mcconf.foc_motor_l = buffer_get_float32_auto(data, &ind);
253 | // mcconf.foc_motor_r = buffer_get_float32_auto(data, &ind);
254 | // mcconf.foc_motor_flux_linkage = buffer_get_float32_auto(data, &ind);
255 | // mcconf.foc_observer_gain = buffer_get_float32_auto(data, &ind);
256 | // mcconf.foc_observer_gain_slow = buffer_get_float32_auto(data, &ind);
257 | // mcconf.foc_duty_dowmramp_kp = buffer_get_float32_auto(data, &ind);
258 | // mcconf.foc_duty_dowmramp_ki = buffer_get_float32_auto(data, &ind);
259 | // mcconf.foc_openloop_rpm = buffer_get_float32_auto(data, &ind);
260 | // mcconf.foc_sl_openloop_hyst = buffer_get_float32_auto(data, &ind);
261 | // mcconf.foc_sl_openloop_time = buffer_get_float32_auto(data, &ind);
262 | // mcconf.foc_sl_d_current_duty = buffer_get_float32_auto(data, &ind);
263 | // mcconf.foc_sl_d_current_factor = buffer_get_float32_auto(data, &ind);
264 | // memcpy(mcconf.foc_hall_table, data + ind, 8);
265 | // ind += 8;
266 | // mcconf.foc_sl_erpm = buffer_get_float32_auto(data, &ind);
267 | // mcconf.foc_sample_v0_v7 = data[ind++];
268 | // mcconf.foc_sat_comp = buffer_get_float32_auto(data, &ind);
269 | // mcconf.foc_temp_comp = data[ind++];
270 | // mcconf.foc_temp_comp_base_temp = buffer_get_float32_auto(data, &ind);
271 | //
272 | // mcconf.s_pid_kp = buffer_get_float32_auto(data, &ind);
273 | // mcconf.s_pid_ki = buffer_get_float32_auto(data, &ind);
274 | // mcconf.s_pid_kd = buffer_get_float32_auto(data, &ind);
275 | // mcconf.s_pid_min_erpm = buffer_get_float32_auto(data, &ind);
276 | // mcconf.s_pid_allow_braking = data[ind++];
277 | //
278 | // mcconf.p_pid_kp = buffer_get_float32_auto(data, &ind);
279 | // mcconf.p_pid_ki = buffer_get_float32_auto(data, &ind);
280 | // mcconf.p_pid_kd = buffer_get_float32_auto(data, &ind);
281 | // mcconf.p_pid_ang_div = buffer_get_float32_auto(data, &ind);
282 | //
283 | // mcconf.cc_startup_boost_duty = buffer_get_float32_auto(data, &ind);
284 | // mcconf.cc_min_current = buffer_get_float32_auto(data, &ind);
285 | // mcconf.cc_gain = buffer_get_float32_auto(data, &ind);
286 | // mcconf.cc_ramp_step_max = buffer_get_float32_auto(data, &ind);
287 | //
288 | // mcconf.m_fault_stop_time_ms = buffer_get_int32(data, &ind);
289 | // mcconf.m_duty_ramp_step = buffer_get_float32_auto(data, &ind);
290 | // mcconf.m_current_backoff_gain = buffer_get_float32_auto(data, &ind);
291 | // mcconf.m_encoder_counts = buffer_get_uint32(data, &ind);
292 | // mcconf.m_sensor_port_mode = data[ind++];
293 | // mcconf.m_invert_direction = data[ind++];
294 | // mcconf.m_drv8301_oc_mode = data[ind++];
295 | // mcconf.m_drv8301_oc_adj = data[ind++];
296 | // mcconf.m_bldc_f_sw_min = buffer_get_float32_auto(data, &ind);
297 | // mcconf.m_bldc_f_sw_max = buffer_get_float32_auto(data, &ind);
298 | // mcconf.m_dc_f_sw = buffer_get_float32_auto(data, &ind);
299 | //
300 | // if (rx_mcconf_func) {
301 | // rx_mcconf_func(&mcconf);
302 | // }
303 | // break;
304 | //
305 | // case COMM_GET_APPCONF:
306 | // case COMM_GET_APPCONF_DEFAULT:
307 | // ind = 0;
308 | // appconf.controller_id = data[ind++];
309 | // appconf.timeout_msec = buffer_get_uint32(data, &ind);
310 | // appconf.timeout_brake_current = buffer_get_float32_auto(data, &ind);
311 | // appconf.send_can_status = data[ind++];
312 | // appconf.send_can_status_rate_hz = buffer_get_uint16(data, &ind);
313 | //
314 | // appconf.app_to_use = data[ind++];
315 | //
316 | // appconf.app_ppm_conf.ctrl_type = data[ind++];
317 | // appconf.app_ppm_conf.pid_max_erpm = buffer_get_float32_auto(data, &ind);
318 | // appconf.app_ppm_conf.hyst = buffer_get_float32_auto(data, &ind);
319 | // appconf.app_ppm_conf.pulse_start = buffer_get_float32_auto(data, &ind);
320 | // appconf.app_ppm_conf.pulse_end = buffer_get_float32_auto(data, &ind);
321 | // appconf.app_ppm_conf.pulse_center = buffer_get_float32_auto(data, &ind);
322 | // appconf.app_ppm_conf.median_filter = data[ind++];
323 | // appconf.app_ppm_conf.safe_start = data[ind++];
324 | // appconf.app_ppm_conf.throttle_exp = buffer_get_float32_auto(data, &ind);
325 | // appconf.app_ppm_conf.throttle_exp_mode = data[ind++];
326 | // appconf.app_ppm_conf.ramp_time_pos = buffer_get_float32_auto(data, &ind);
327 | // appconf.app_ppm_conf.ramp_time_neg = buffer_get_float32_auto(data, &ind);
328 | // appconf.app_ppm_conf.multi_esc = data[ind++];
329 | // appconf.app_ppm_conf.tc = data[ind++];
330 | // appconf.app_ppm_conf.tc_max_diff = buffer_get_float32_auto(data, &ind);
331 | //
332 | // appconf.app_adc_conf.ctrl_type = data[ind++];
333 | // appconf.app_adc_conf.hyst = buffer_get_float32_auto(data, &ind);
334 | // appconf.app_adc_conf.voltage_start = buffer_get_float32_auto(data, &ind);
335 | // appconf.app_adc_conf.voltage_end = buffer_get_float32_auto(data, &ind);
336 | // appconf.app_adc_conf.voltage_center = buffer_get_float32_auto(data, &ind);
337 | // appconf.app_adc_conf.voltage2_start = buffer_get_float32_auto(data, &ind);
338 | // appconf.app_adc_conf.voltage2_end = buffer_get_float32_auto(data, &ind);
339 | // appconf.app_adc_conf.use_filter = data[ind++];
340 | // appconf.app_adc_conf.safe_start = data[ind++];
341 | // appconf.app_adc_conf.cc_button_inverted = data[ind++];
342 | // appconf.app_adc_conf.rev_button_inverted = data[ind++];
343 | // appconf.app_adc_conf.voltage_inverted = data[ind++];
344 | // appconf.app_adc_conf.voltage2_inverted = data[ind++];
345 | // appconf.app_adc_conf.throttle_exp = buffer_get_float32_auto(data, &ind);
346 | // appconf.app_adc_conf.throttle_exp_mode = data[ind++];
347 | // appconf.app_adc_conf.multi_esc = data[ind++];
348 | // appconf.app_adc_conf.tc = data[ind++];
349 | // appconf.app_adc_conf.tc_max_diff = buffer_get_float32_auto(data, &ind);
350 | // appconf.app_adc_conf.update_rate_hz = buffer_get_uint16(data, &ind);
351 | //
352 | // appconf.app_uart_baudrate = buffer_get_uint32(data, &ind);
353 | //
354 | // appconf.app_chuk_conf.ctrl_type = data[ind++];
355 | // appconf.app_chuk_conf.hyst = buffer_get_float32_auto(data, &ind);
356 | // appconf.app_chuk_conf.ramp_time_pos = buffer_get_float32_auto(data, &ind);
357 | // appconf.app_chuk_conf.ramp_time_neg = buffer_get_float32_auto(data, &ind);
358 | // appconf.app_chuk_conf.stick_erpm_per_s_in_cc = buffer_get_float32_auto(data, &ind);
359 | // appconf.app_chuk_conf.throttle_exp = buffer_get_float32_auto(data, &ind);
360 | // appconf.app_chuk_conf.throttle_exp_mode = data[ind++];
361 | // appconf.app_chuk_conf.multi_esc = data[ind++];
362 | // appconf.app_chuk_conf.tc = data[ind++];
363 | // appconf.app_chuk_conf.tc_max_diff = buffer_get_float32_auto(data, &ind);
364 | //
365 | // appconf.app_nrf_conf.speed = data[ind++];
366 | // appconf.app_nrf_conf.power = data[ind++];
367 | // appconf.app_nrf_conf.crc_type = data[ind++];
368 | // appconf.app_nrf_conf.retry_delay = data[ind++];
369 | // appconf.app_nrf_conf.retries = data[ind++];
370 | // appconf.app_nrf_conf.channel = data[ind++];
371 | // memcpy(appconf.app_nrf_conf.address, data + ind, 3);
372 | // ind += 3;
373 | // appconf.app_nrf_conf.send_crc_ack = data[ind++];
374 | //
375 | // if (rx_appconf_func) {
376 | // rx_appconf_func(&appconf);
377 | // }
378 | // break;
379 | //
380 | // case COMM_DETECT_MOTOR_PARAM:
381 | // ind = 0;
382 | // detect_cycle_int_limit = buffer_get_float32(data, 1000.0, &ind);
383 | // detect_coupling_k = buffer_get_float32(data, 1000.0, &ind);
384 | // for (i = 0;i < 8;i++) {
385 | // detect_hall_table[i] = (const signed char)(data[ind++]);
386 | // }
387 | // detect_hall_res = (const signed char)(data[ind++]);
388 | //
389 | // if (rx_detect_func) {
390 | // rx_detect_func(detect_cycle_int_limit, detect_coupling_k,
391 | // detect_hall_table, detect_hall_res);
392 | // }
393 | // break;
394 | //
395 | // case COMM_DETECT_MOTOR_R_L: {
396 | // // TODO!
397 | // } break;
398 | //
399 | // case COMM_DETECT_MOTOR_FLUX_LINKAGE: {
400 | // // TODO!
401 | // } break;
402 | //
403 | // case COMM_DETECT_ENCODER: {
404 | // // TODO!
405 | // } break;
406 | //
407 | // case COMM_DETECT_HALL_FOC: {
408 | // // TODO!
409 | // } break;
410 | //
411 | // case COMM_GET_DECODED_PPM:
412 | // ind = 0;
413 | // dec_ppm = buffer_get_float32(data, 1000000.0, &ind);
414 | // dec_ppm_len = buffer_get_float32(data, 1000000.0, &ind);
415 | //
416 | // if (rx_dec_ppm_func) {
417 | // rx_dec_ppm_func(dec_ppm, dec_ppm_len);
418 | // }
419 | // break;
420 | //
421 | // case COMM_GET_DECODED_ADC:
422 | // ind = 0;
423 | // dec_adc = buffer_get_float32(data, 1000000.0, &ind);
424 | // dec_adc_voltage = buffer_get_float32(data, 1000000.0, &ind);
425 | // // TODO for adc2
426 | //
427 | // if (rx_dec_adc_func) {
428 | // rx_dec_adc_func(dec_adc, dec_adc_voltage);
429 | // }
430 | // break;
431 | //
432 | // case COMM_GET_DECODED_CHUK:
433 | // ind = 0;
434 | // dec_chuk = buffer_get_float32(data, 1000000.0, &ind);
435 | //
436 | // if (rx_dec_chuk_func) {
437 | // rx_dec_chuk_func(dec_chuk);
438 | // }
439 | // break;
440 | //
441 | // case COMM_SET_MCCONF:
442 | // // This is a confirmation that the new mcconf is received.
443 | // if (rx_mcconf_received_func) {
444 | // rx_mcconf_received_func();
445 | // }
446 | // break;
447 | //
448 | // case COMM_SET_APPCONF:
449 | // // This is a confirmation that the new appconf is received.
450 | // if (rx_appconf_received_func) {
451 | // rx_appconf_received_func();
452 | // }
453 | // break;
454 |
455 | default:
456 | break;
457 | }
458 | }
459 |
460 | /**
461 | * Function pointer setters. When data that is requested with the get functions
462 | * is received, the corresponding function pointer will be called with the
463 | * received data.
464 | *
465 | * @param func
466 | * A function to be called when the corresponding data is received.
467 | */
468 |
469 | void bldc_interface_set_rx_value_func(void(*func)(mc_values *values)) {
470 | rx_value_func = func;
471 | }
472 |
473 | // void bldc_interface_set_rx_printf_func(void(*func)(char *str)) {
474 | // rx_printf_func = func;
475 | // }
476 |
477 | // void bldc_interface_set_rx_fw_func(void(*func)(int major, int minor)) {
478 | // rx_fw_func = func;
479 | // }
480 |
481 | // void bldc_interface_set_rx_rotor_pos_func(void(*func)(float pos)) {
482 | // rx_rotor_pos_func = func;
483 | // }
484 |
485 | // void bldc_interface_set_rx_mcconf_func(void(*func)(mc_configuration *conf)) {
486 | // rx_mcconf_func = func;
487 | // }
488 |
489 | // void bldc_interface_set_rx_appconf_func(void(*func)(app_configuration *conf)) {
490 | // rx_appconf_func = func;
491 | // }
492 |
493 | // void bldc_interface_set_rx_detect_func(void(*func)(float cycle_int_limit, float coupling_k,
494 | // const signed char *hall_table, signed char hall_res)) {
495 | // rx_detect_func = func;
496 | // }
497 |
498 | // void bldc_interface_set_rx_dec_ppm_func(void(*func)(float val, float ms)) {
499 | // rx_dec_ppm_func = func;
500 | // }
501 |
502 | // void bldc_interface_set_rx_dec_adc_func(void(*func)(float val, float voltage)) {
503 | // rx_dec_adc_func = func;
504 | // }
505 |
506 | // void bldc_interface_set_rx_dec_chuk_func(void(*func)(float val)) {
507 | // rx_dec_chuk_func = func;
508 | // }
509 |
510 | // void bldc_interface_set_rx_mcconf_received_func(void(*func)(void)) {
511 | // rx_mcconf_received_func = func;
512 | // }
513 |
514 | // void bldc_interface_set_rx_appconf_received_func(void(*func)(void)) {
515 | // rx_appconf_received_func = func;
516 | // }
517 |
518 | // void bldc_interface_set_sim_control_function(void(*func)(motor_control_mode mode, float value)) {
519 | // motor_control_set_func = func;
520 | // }
521 |
522 | // void bldc_interface_set_sim_values_func(void(*func)(void)) {
523 | // values_requested_func = func;
524 | // }
525 |
526 | // Setters
527 | // void bldc_interface_terminal_cmd(char* cmd) {
528 | // int len = strlen(cmd);
529 | // send_buffer[0] = COMM_TERMINAL_CMD;
530 | // memcpy(send_buffer + 1, cmd, len);
531 | // send_packet_no_fwd(send_buffer, len + 1);
532 | // }
533 |
534 | // void bldc_interface_set_duty_cycle(float dutyCycle) {
535 | // if (motor_control_set_func) {
536 | // motor_control_set_func(MOTOR_CONTROL_DUTY, dutyCycle);
537 | // return;
538 | // }
539 | // int32_t send_index = 0;
540 | // send_buffer[send_index++] = COMM_SET_DUTY;
541 | // buffer_append_float32(send_buffer, dutyCycle, 100000.0, &send_index);
542 | // send_packet_no_fwd(send_buffer, send_index);
543 | // }
544 |
545 | void bldc_interface_set_current(float current) {
546 | if (motor_control_set_func) {
547 | motor_control_set_func(MOTOR_CONTROL_CURRENT, current);
548 | return;
549 | }
550 | int32_t send_index = 0;
551 | send_buffer[send_index++] = COMM_SET_CURRENT;
552 | buffer_append_float32(send_buffer, current, 1000.0, &send_index);
553 | send_packet_no_fwd(send_buffer, send_index);
554 | }
555 |
556 | // void bldc_interface_set_current_brake(float current) {
557 | // if (motor_control_set_func) {
558 | // motor_control_set_func(MOTOR_CONTROL_CURRENT_BRAKE, current);
559 | // return;
560 | // }
561 | // int32_t send_index = 0;
562 | // send_buffer[send_index++] = COMM_SET_CURRENT_BRAKE;
563 | // buffer_append_float32(send_buffer, current, 1000.0, &send_index);
564 | // send_packet_no_fwd(send_buffer, send_index);
565 | // }
566 |
567 | void bldc_interface_set_rpm(int rpm) {
568 | if (motor_control_set_func) {
569 | motor_control_set_func(MOTOR_CONTROL_RPM, rpm);
570 | return;
571 | }
572 | int32_t send_index = 0;
573 | send_buffer[send_index++] = COMM_SET_RPM;
574 | buffer_append_int32(send_buffer, rpm, &send_index);
575 | send_packet_no_fwd(send_buffer, send_index);
576 | }
577 |
578 | // void bldc_interface_set_pos(float pos) {
579 | // if (motor_control_set_func) {
580 | // motor_control_set_func(MOTOR_CONTROL_POS, pos);
581 | // return;
582 | // }
583 | // int32_t send_index = 0;
584 | // send_buffer[send_index++] = COMM_SET_POS;
585 | // buffer_append_float32(send_buffer, pos, 1000000.0, &send_index);
586 | // send_packet_no_fwd(send_buffer, send_index);
587 | // }
588 |
589 | // void bldc_interface_set_handbrake(float current) {
590 | // int32_t send_index = 0;
591 | // send_buffer[send_index++] = COMM_SET_HANDBRAKE;
592 | // buffer_append_float32(send_buffer, current, 1e3, &send_index);
593 | // send_packet_no_fwd(send_buffer, send_index);
594 | // }
595 |
596 | // void bldc_interface_set_servo_pos(float pos) {
597 | // int32_t send_index = 0;
598 | // send_buffer[send_index++] = COMM_SET_SERVO_POS;
599 | // buffer_append_float16(send_buffer, pos, 1000.0, &send_index);
600 | // send_packet_no_fwd(send_buffer, send_index);
601 | // }
602 |
603 | // void bldc_interface_set_mcconf(const mc_configuration *mcconf) {
604 | // int32_t ind = 0;
605 | // send_buffer[ind++] = COMM_SET_MCCONF;
606 | //
607 | // send_buffer[ind++] = mcconf->pwm_mode;
608 | // send_buffer[ind++] = mcconf->comm_mode;
609 | // send_buffer[ind++] = mcconf->motor_type;
610 | // send_buffer[ind++] = mcconf->sensor_mode;
611 | //
612 | // buffer_append_float32_auto(send_buffer, mcconf->l_current_max, &ind);
613 | // buffer_append_float32_auto(send_buffer, mcconf->l_current_min, &ind);
614 | // buffer_append_float32_auto(send_buffer, mcconf->l_in_current_max, &ind);
615 | // buffer_append_float32_auto(send_buffer, mcconf->l_in_current_min, &ind);
616 | // buffer_append_float32_auto(send_buffer, mcconf->l_abs_current_max, &ind);
617 | // buffer_append_float32_auto(send_buffer, mcconf->l_min_erpm, &ind);
618 | // buffer_append_float32_auto(send_buffer, mcconf->l_max_erpm, &ind);
619 | // buffer_append_float32_auto(send_buffer, mcconf->l_erpm_start, &ind);
620 | // buffer_append_float32_auto(send_buffer, mcconf->l_max_erpm_fbrake, &ind);
621 | // buffer_append_float32_auto(send_buffer, mcconf->l_max_erpm_fbrake_cc, &ind);
622 | // buffer_append_float32_auto(send_buffer, mcconf->l_min_vin, &ind);
623 | // buffer_append_float32_auto(send_buffer, mcconf->l_max_vin, &ind);
624 | // buffer_append_float32_auto(send_buffer, mcconf->l_battery_cut_start, &ind);
625 | // buffer_append_float32_auto(send_buffer, mcconf->l_battery_cut_end, &ind);
626 | // send_buffer[ind++] = mcconf->l_slow_abs_current;
627 | // buffer_append_float32_auto(send_buffer, mcconf->l_temp_fet_start, &ind);
628 | // buffer_append_float32_auto(send_buffer, mcconf->l_temp_fet_end, &ind);
629 | // buffer_append_float32_auto(send_buffer, mcconf->l_temp_motor_start, &ind);
630 | // buffer_append_float32_auto(send_buffer, mcconf->l_temp_motor_end, &ind);
631 | // buffer_append_float32_auto(send_buffer, mcconf->l_min_duty, &ind);
632 | // buffer_append_float32_auto(send_buffer, mcconf->l_max_duty, &ind);
633 | // buffer_append_float32_auto(send_buffer, mcconf->l_watt_max, &ind);
634 | // buffer_append_float32_auto(send_buffer, mcconf->l_watt_min, &ind);
635 | //
636 | // buffer_append_float32_auto(send_buffer, mcconf->sl_min_erpm, &ind);
637 | // buffer_append_float32_auto(send_buffer, mcconf->sl_min_erpm_cycle_int_limit, &ind);
638 | // buffer_append_float32_auto(send_buffer, mcconf->sl_max_fullbreak_current_dir_change, &ind);
639 | // buffer_append_float32_auto(send_buffer, mcconf->sl_cycle_int_limit, &ind);
640 | // buffer_append_float32_auto(send_buffer, mcconf->sl_phase_advance_at_br, &ind);
641 | // buffer_append_float32_auto(send_buffer, mcconf->sl_cycle_int_rpm_br, &ind);
642 | // buffer_append_float32_auto(send_buffer, mcconf->sl_bemf_coupling_k, &ind);
643 | //
644 | // memcpy(send_buffer + ind, mcconf->hall_table, 8);
645 | // ind += 8;
646 | // buffer_append_float32_auto(send_buffer, mcconf->hall_sl_erpm, &ind);
647 | //
648 | // buffer_append_float32_auto(send_buffer, mcconf->foc_current_kp, &ind);
649 | // buffer_append_float32_auto(send_buffer, mcconf->foc_current_ki, &ind);
650 | // buffer_append_float32_auto(send_buffer, mcconf->foc_f_sw, &ind);
651 | // buffer_append_float32_auto(send_buffer, mcconf->foc_dt_us, &ind);
652 | // send_buffer[ind++] = mcconf->foc_encoder_inverted;
653 | // buffer_append_float32_auto(send_buffer, mcconf->foc_encoder_offset, &ind);
654 | // buffer_append_float32_auto(send_buffer, mcconf->foc_encoder_ratio, &ind);
655 | // send_buffer[ind++] = mcconf->foc_sensor_mode;
656 | // buffer_append_float32_auto(send_buffer, mcconf->foc_pll_kp, &ind);
657 | // buffer_append_float32_auto(send_buffer, mcconf->foc_pll_ki, &ind);
658 | // buffer_append_float32_auto(send_buffer, mcconf->foc_motor_l, &ind);
659 | // buffer_append_float32_auto(send_buffer, mcconf->foc_motor_r, &ind);
660 | // buffer_append_float32_auto(send_buffer, mcconf->foc_motor_flux_linkage, &ind);
661 | // buffer_append_float32_auto(send_buffer, mcconf->foc_observer_gain, &ind);
662 | // buffer_append_float32_auto(send_buffer, mcconf->foc_observer_gain_slow, &ind);
663 | // buffer_append_float32_auto(send_buffer, mcconf->foc_duty_dowmramp_kp, &ind);
664 | // buffer_append_float32_auto(send_buffer, mcconf->foc_duty_dowmramp_ki, &ind);
665 | // buffer_append_float32_auto(send_buffer, mcconf->foc_openloop_rpm, &ind);
666 | // buffer_append_float32_auto(send_buffer, mcconf->foc_sl_openloop_hyst, &ind);
667 | // buffer_append_float32_auto(send_buffer, mcconf->foc_sl_openloop_time, &ind);
668 | // buffer_append_float32_auto(send_buffer, mcconf->foc_sl_d_current_duty, &ind);
669 | // buffer_append_float32_auto(send_buffer, mcconf->foc_sl_d_current_factor, &ind);
670 | // memcpy(send_buffer + ind, mcconf->foc_hall_table, 8);
671 | // ind += 8;
672 | // buffer_append_float32_auto(send_buffer, mcconf->foc_sl_erpm, &ind);
673 | // send_buffer[ind++] = mcconf->foc_sample_v0_v7;
674 | // buffer_append_float32_auto(send_buffer, mcconf->foc_sat_comp, &ind);
675 | // send_buffer[ind++] = mcconf->foc_temp_comp;
676 | // buffer_append_float32_auto(send_buffer, mcconf->foc_temp_comp_base_temp, &ind);
677 | //
678 | // buffer_append_float32_auto(send_buffer, mcconf->s_pid_kp, &ind);
679 | // buffer_append_float32_auto(send_buffer, mcconf->s_pid_ki, &ind);
680 | // buffer_append_float32_auto(send_buffer, mcconf->s_pid_kd, &ind);
681 | // buffer_append_float32_auto(send_buffer, mcconf->s_pid_min_erpm, &ind);
682 | // send_buffer[ind++] = mcconf->s_pid_allow_braking;
683 | //
684 | // buffer_append_float32_auto(send_buffer, mcconf->p_pid_kp, &ind);
685 | // buffer_append_float32_auto(send_buffer, mcconf->p_pid_ki, &ind);
686 | // buffer_append_float32_auto(send_buffer, mcconf->p_pid_kd, &ind);
687 | // buffer_append_float32_auto(send_buffer, mcconf->p_pid_ang_div, &ind);
688 | //
689 | // buffer_append_float32_auto(send_buffer, mcconf->cc_startup_boost_duty, &ind);
690 | // buffer_append_float32_auto(send_buffer, mcconf->cc_min_current, &ind);
691 | // buffer_append_float32_auto(send_buffer, mcconf->cc_gain, &ind);
692 | // buffer_append_float32_auto(send_buffer, mcconf->cc_ramp_step_max, &ind);
693 | //
694 | // buffer_append_int32(send_buffer, mcconf->m_fault_stop_time_ms, &ind);
695 | // buffer_append_float32_auto(send_buffer, mcconf->m_duty_ramp_step, &ind);
696 | // buffer_append_float32_auto(send_buffer, mcconf->m_current_backoff_gain, &ind);
697 | // buffer_append_uint32(send_buffer, mcconf->m_encoder_counts, &ind);
698 | // send_buffer[ind++] = mcconf->m_sensor_port_mode;
699 | // send_buffer[ind++] = mcconf->m_invert_direction;
700 | // send_buffer[ind++] = mcconf->m_drv8301_oc_mode;
701 | // send_buffer[ind++] = mcconf->m_drv8301_oc_adj;
702 | // buffer_append_float32_auto(send_buffer, mcconf->m_bldc_f_sw_min, &ind);
703 | // buffer_append_float32_auto(send_buffer, mcconf->m_bldc_f_sw_max, &ind);
704 | // buffer_append_float32_auto(send_buffer, mcconf->m_dc_f_sw, &ind);
705 | //
706 | // send_packet_no_fwd(send_buffer, ind);
707 | // }
708 |
709 | // void bldc_interface_set_appconf(const app_configuration *appconf) {
710 | // int32_t ind = 0;
711 | // // send_buffer[ind++] = COMM_SET_APPCONF;
712 | // send_buffer[ind++] = appconf->controller_id;
713 | // buffer_append_uint32(send_buffer, appconf->timeout_msec, &ind);
714 | // buffer_append_float32_auto(send_buffer, appconf->timeout_brake_current, &ind);
715 | // send_buffer[ind++] = appconf->send_can_status;
716 | // buffer_append_uint16(send_buffer, appconf->send_can_status_rate_hz, &ind);
717 | //
718 | // send_buffer[ind++] = appconf->app_to_use;
719 | //
720 | // send_buffer[ind++] = appconf->app_ppm_conf.ctrl_type;
721 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.pid_max_erpm, &ind);
722 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.hyst, &ind);
723 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.pulse_start, &ind);
724 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.pulse_end, &ind);
725 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.pulse_center, &ind);
726 | // send_buffer[ind++] = appconf->app_ppm_conf.median_filter;
727 | // send_buffer[ind++] = appconf->app_ppm_conf.safe_start;
728 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.throttle_exp, &ind);
729 | // send_buffer[ind++] = appconf->app_ppm_conf.throttle_exp_mode;
730 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.ramp_time_pos, &ind);
731 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.ramp_time_neg, &ind);
732 | // send_buffer[ind++] = appconf->app_ppm_conf.multi_esc;
733 | // send_buffer[ind++] = appconf->app_ppm_conf.tc;
734 | // buffer_append_float32_auto(send_buffer, appconf->app_ppm_conf.tc_max_diff, &ind);
735 | //
736 | // send_buffer[ind++] = appconf->app_adc_conf.ctrl_type;
737 | // buffer_append_float32_auto(send_buffer, appconf->app_adc_conf.hyst, &ind);
738 | // buffer_append_float32_auto(send_buffer, appconf->app_adc_conf.voltage_start, &ind);
739 | // buffer_append_float32_auto(send_buffer, appconf->app_adc_conf.voltage_end, &ind);
740 | // buffer_append_float32_auto(send_buffer, appconf->app_adc_conf.voltage_center, &ind);
741 | // buffer_append_float32_auto(send_buffer, appconf->app_adc_conf.voltage2_start, &ind);
742 | // buffer_append_float32_auto(send_buffer, appconf->app_adc_conf.voltage2_end, &ind);
743 | // send_buffer[ind++] = appconf->app_adc_conf.use_filter;
744 | // send_buffer[ind++] = appconf->app_adc_conf.safe_start;
745 | // send_buffer[ind++] = appconf->app_adc_conf.cc_button_inverted;
746 | // send_buffer[ind++] = appconf->app_adc_conf.rev_button_inverted;
747 | // send_buffer[ind++] = appconf->app_adc_conf.voltage_inverted;
748 | // send_buffer[ind++] = appconf->app_adc_conf.voltage2_inverted;
749 | // buffer_append_float32_auto(send_buffer, appconf->app_adc_conf.throttle_exp, &ind);
750 | // send_buffer[ind++] = appconf->app_adc_conf.throttle_exp_mode;
751 | // send_buffer[ind++] = appconf->app_adc_conf.multi_esc;
752 | // send_buffer[ind++] = appconf->app_adc_conf.tc;
753 | // buffer_append_float32_auto(send_buffer, appconf->app_adc_conf.tc_max_diff, &ind);
754 | // buffer_append_uint16(send_buffer, appconf->app_adc_conf.update_rate_hz, &ind);
755 | //
756 | // buffer_append_uint32(send_buffer, appconf->app_uart_baudrate, &ind);
757 | //
758 | // send_buffer[ind++] = appconf->app_chuk_conf.ctrl_type;
759 | // buffer_append_float32_auto(send_buffer, appconf->app_chuk_conf.hyst, &ind);
760 | // buffer_append_float32_auto(send_buffer, appconf->app_chuk_conf.ramp_time_pos, &ind);
761 | // buffer_append_float32_auto(send_buffer, appconf->app_chuk_conf.ramp_time_neg, &ind);
762 | // buffer_append_float32_auto(send_buffer, appconf->app_chuk_conf.stick_erpm_per_s_in_cc, &ind);
763 | // buffer_append_float32_auto(send_buffer, appconf->app_chuk_conf.throttle_exp, &ind);
764 | // send_buffer[ind++] = appconf->app_chuk_conf.throttle_exp_mode;
765 | // send_buffer[ind++] = appconf->app_chuk_conf.multi_esc;
766 | // send_buffer[ind++] = appconf->app_chuk_conf.tc;
767 | // buffer_append_float32_auto(send_buffer, appconf->app_chuk_conf.tc_max_diff, &ind);
768 | //
769 | // send_buffer[ind++] = appconf->app_nrf_conf.speed;
770 | // send_buffer[ind++] = appconf->app_nrf_conf.power;
771 | // send_buffer[ind++] = appconf->app_nrf_conf.crc_type;
772 | // send_buffer[ind++] = appconf->app_nrf_conf.retry_delay;
773 | // send_buffer[ind++] = appconf->app_nrf_conf.retries;
774 | // send_buffer[ind++] = appconf->app_nrf_conf.channel;
775 | // memcpy(send_buffer + ind, appconf->app_nrf_conf.address, 3);
776 | // ind += 3;
777 | // send_buffer[ind++] = appconf->app_nrf_conf.send_crc_ack;
778 | //
779 | // send_packet_no_fwd(send_buffer, ind);
780 | // }
781 |
782 | // Getters
783 | // void bldc_interface_get_fw_version(void) {
784 | // int32_t send_index = 0;
785 | // send_buffer[send_index++] = COMM_FW_VERSION;
786 | // send_packet_no_fwd(send_buffer, send_index);
787 | // }
788 |
789 | void bldc_interface_get_values(void) {
790 | if (values_requested_func) {
791 | values_requested_func();
792 | return;
793 | }
794 | int32_t send_index = 0;
795 | send_buffer[send_index++] = COMM_GET_VALUES;
796 | send_packet_no_fwd(send_buffer, send_index);
797 | }
798 |
799 | // void bldc_interface_get_mcconf(void) {
800 | // int32_t send_index = 0;
801 | // send_buffer[send_index++] = COMM_GET_MCCONF;
802 | // send_packet_no_fwd(send_buffer, send_index);
803 | // }
804 |
805 | // void bldc_interface_get_appconf(void) {
806 | // int32_t send_index = 0;
807 | // send_buffer[send_index++] = COMM_GET_APPCONF;
808 | // send_packet_no_fwd(send_buffer, send_index);
809 | // }
810 |
811 | // void bldc_interface_get_decoded_ppm(void) {
812 | // int32_t send_index = 0;
813 | // send_buffer[send_index++] = COMM_GET_DECODED_PPM;
814 | // send_packet_no_fwd(send_buffer, send_index);
815 | // }
816 |
817 | // void bldc_interface_get_decoded_adc(void) {
818 | // int32_t send_index = 0;
819 | // send_buffer[send_index++] = COMM_GET_DECODED_ADC;
820 | // send_packet_no_fwd(send_buffer, send_index);
821 | // }
822 |
823 | // void bldc_interface_get_decoded_chuk(void) {
824 | // int32_t send_index = 0;
825 | // send_buffer[send_index++] = COMM_GET_DECODED_CHUK;
826 | // send_packet_no_fwd(send_buffer, send_index);
827 | // }
828 |
829 | // Other functions
830 | // void bldc_interface_detect_motor_param(float current, float min_rpm, float low_duty) {
831 | // int32_t send_index = 0;
832 | // send_buffer[send_index++] = COMM_DETECT_MOTOR_PARAM;
833 | // buffer_append_float32(send_buffer, current, 1000.0, &send_index);
834 | // buffer_append_float32(send_buffer, min_rpm, 1000.0, &send_index);
835 | // buffer_append_float32(send_buffer, low_duty, 1000.0, &send_index);
836 | // send_packet_no_fwd(send_buffer, send_index);
837 | // }
838 |
839 | void bldc_interface_reboot(void) {
840 | int32_t send_index = 0;
841 | send_buffer[send_index++] = COMM_REBOOT;
842 | send_packet_no_fwd(send_buffer, send_index);
843 | }
844 |
845 | void bldc_interface_send_alive(void) {
846 | int32_t send_index = 0;
847 | send_buffer[send_index++] = COMM_ALIVE;
848 | send_packet_no_fwd(send_buffer, send_index);
849 | }
850 |
851 | // void send_values_to_receiver(mc_values *values) {
852 | // if (rx_value_func) {
853 | // rx_value_func(values);
854 | // }
855 | // }
856 |
857 | // Helpers
858 | // const char* bldc_interface_fault_to_string(mc_fault_code fault) {
859 | // switch (fault) {
860 | // case FAULT_CODE_NONE: return "FAULT_CODE_NONE";
861 | // case FAULT_CODE_OVER_VOLTAGE: return "FAULT_CODE_OVER_VOLTAGE";
862 | // case FAULT_CODE_UNDER_VOLTAGE: return "FAULT_CODE_UNDER_VOLTAGE";
863 | // case FAULT_CODE_DRV: return "FAULT_CODE_DRV";
864 | // case FAULT_CODE_ABS_OVER_CURRENT: return "FAULT_CODE_ABS_OVER_CURRENT";
865 | // case FAULT_CODE_OVER_TEMP_FET: return "FAULT_CODE_OVER_TEMP_FET";
866 | // case FAULT_CODE_OVER_TEMP_MOTOR: return "FAULT_CODE_OVER_TEMP_MOTOR";
867 | // default: return "Unknown fault";
868 | // }
869 | // }
870 |
871 | // Private functions
872 | void send_packet_no_fwd(unsigned char *data, unsigned int len) {
873 | if (!forward_func) {
874 | bldc_interface_send_packet(data, len);
875 | }
876 | }
877 |
--------------------------------------------------------------------------------