Post · 6 min read· pp. 102–108

VESC – Writing Custom Applications

Updated: 2016-01-22

The VESC has several extra ports and much extra computational power, so it can be used to run custom user code in addition to controlling a motor. This is convenient when there are space constraints and it is also the best way to implement real-time control applications where timing is critical. The code of the VESC is organized in such a way that it is easy to write and maintain custom applications while keeping the code up to date without having many conflicts when pulling updates using git. In this tutorial I will demonstrate how to make a custom application that will run a motor using speed control while a button is held with a speed proportional to the voltage on the ADC_EXT pin.

Preparation

Follow the tutorial in the VESC post and make sure that you can build and upload the firmware. When you can do that, you should be able to write and upload custom applications.

Connecting everything

This is the example that I’m going to use in this post. When the pushbutton is held down, the VESC will run the connected motor in a speed control loop with a speed proportional to the position of the potentiometer between 0 and 10k ERPM.

VESC_ADC_Button

Create the custom application

Download the latest firmware from using git

text
mkdir BLDC
cd BLDC
git clone https://github.com/vedderb/bldc.git bldc-firmware
cd bldc-firmware

In the applications directory, create a file called app_example.c and put in the following content. This is your custom application that you can edit

```c
#include "ch.h" // ChibiOS
#include "hal.h" // ChibiOS HAL
#include "mc_interface.h" // Motor control functions
#include "hw.h" // Pin mapping on this hardware
#include "timeout.h" // To reset the timeout
// Example thread
static THD_FUNCTION(example_thread, arg);
static THD_WORKING_AREA(example_thread_wa, 2048); // 2kb stack for this thread
void app_example_init(void) {
code
// Set the UART TX pin as an input with pulldown
palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_INPUT_PULLDOWN);

  // Start the example thread chThdCreateStatic(example_thread_wa, sizeof(example_thread_wa), NORMALPRIO, example_thread, NULL); }   static THD_FUNCTION(example_thread, arg) { (void)arg;   chRegSetThreadName("APP_EXAMPLE");   for(;;) { // Read the pot value and scale it to a number between 0 and 1 (see hw_46.h) float pot = (float)ADC_Value[ADC_IND_EXT]; pot /= 4095.0;   if (palReadPad(HW_UART_TX_PORT, HW_UART_TX_PIN)) { // If the button is pressed, run the motor with speed control // proportional to the POT position with a speed between 0 ERPM // and 10000 ERPM mc_interface_set_pid_speed(pot * 10000.0); } else { // If the button is not pressed, release the motor mc_interface_release_motor(); }   // Run this loop at 500Hz chThdSleepMilliseconds(2);   // Reset the timeout timeout_reset(); } }

|

#include "ch.h" // ChibiOS
#include "hal.h" // ChibiOS HAL
#include "mc\_interface.h" // Motor control functions
#include "hw.h" // Pin mapping on this hardware
#include "timeout.h" // To reset the timeout
// Example thread
static THD\_FUNCTION(example\_thread, arg);
static THD\_WORKING\_AREA(example\_thread\_wa, 2048); // 2kb stack for this thread
void app\_example\_init(void) {
// Set the UART TX pin as an input with pulldown
palSetPadMode(HW\_UART\_TX\_PORT, HW\_UART\_TX\_PIN, PAL\_MODE\_INPUT\_PULLDOWN);
// Start the example thread
chThdCreateStatic(example\_thread\_wa, sizeof(example\_thread\_wa),
NORMALPRIO, example\_thread, NULL);
}
static THD\_FUNCTION(example\_thread, arg) {
(void)arg;
chRegSetThreadName("APP\_EXAMPLE");
for(;;) {
// Read the pot value and scale it to a number between 0 and 1 (see hw\_46.h)
float pot = (float)ADC\_Value[ADC\_IND\_EXT];
pot /= 4095.0;
if (palReadPad(HW\_UART\_TX\_PORT, HW\_UART\_TX\_PIN)) {
// If the button is pressed, run the motor with speed control
// proportional to the POT position with a speed between 0 ERPM
// and 10000 ERPM
mc\_interface\_set\_pid\_speed(pot \* 10000.0);
} else {
// If the button is not pressed, release the motor
mc\_interface\_release\_motor();
}
// Run this loop at 500Hz
chThdSleepMilliseconds(2);
// Reset the timeout
timeout\_reset();
}
}

Open applications.mk and add your example application to the end of APPSRC

```text
APPSRC =	applications/app.c \
		applications/app_ppm.c \
		applications/app_adc.c \
		applications/app_sten.c \
		applications/app_uartcomm.c \
		applications/app_nunchuk.c \
		applications/app_example.c

next open app.h and add a definition for your example application under Custom apps

```c
// Custom apps
void app_gurgalof_init(void);
void app_sten_init(void);
void app_example_init(void); // Your application
|

// Custom apps
void app\_gurgalof\_init(void);
void app\_sten\_init(void);
void app\_example\_init(void); // Your application

now open app.c and add your app under the APP\_CUSTOM case in the init function:

|  |
| --- |
| ```c
void app_init(app_configuration *conf) {
	app_set_configuration(conf);
 
	switch (appconf.app_to_use) {
	case APP_PPM:
		app_ppm_start();
		break;
 
	case APP_ADC:
		app_adc_start();
		break;
 
	case APP_UART:
		hw_stop_i2c();
		app_uartcomm_start();
		break;
 
	case APP_PPM_UART:
		hw_stop_i2c();
		app_ppm_start();
		app_uartcomm_start();
		break;
 
	case APP_ADC_UART:
		hw_stop_i2c();
		app_adc_start();
		app_uartcomm_start();
		break;
 
	case APP_NUNCHUK:
		app_nunchuk_start();
		break;
 
	case APP_NRF:
		nrf_driver_init();
		break;
 
	case APP_CUSTOM:
#ifdef USE_APP_STEN
		hw_stop_i2c();
		app_sten_init();
#endif
		app_example_init(); // Your example application
		break;
 
	default:
		break;
	}
}
``` |

void app\_init(app\_configuration \*conf) {
app\_set\_configuration(conf);
switch (appconf.app\_to\_use) {
case APP\_PPM:
app\_ppm\_start();
break;
case APP\_ADC:
app\_adc\_start();
break;
case APP\_UART:
hw\_stop\_i2c();
app\_uartcomm\_start();
break;
case APP\_PPM\_UART:
hw\_stop\_i2c();
app\_ppm\_start();
app\_uartcomm\_start();
break;
case APP\_ADC\_UART:
hw\_stop\_i2c();
app\_adc\_start();
app\_uartcomm\_start();
break;
case APP\_NUNCHUK:
app\_nunchuk\_start();
break;
case APP\_NRF:
nrf\_driver\_init();
break;
case APP\_CUSTOM:
#ifdef USE\_APP\_STEN
hw\_stop\_i2c();
app\_sten\_init();
#endif
app\_example\_init(); // Your example application
break;
default:
break;
}
}

After this you are done editing the source code, so connect a programmer to the VESC. Go to the terminal in the bldc-firmware directory and type make upload to build and upload the firmware.

```text
make upload

Alternatively, you can just type make and upload the the firmware using BLDC Tool and the bootloader. The .bin file for the firmware will be created in the build directory under bldc-firmware.

Using your custom application

Configure the VESC as described in my other post as usual. In the app configuration, select custom user application

app_conf

and write the configuration. The next time the VESC boots, your custom example application will be loaded. Not too difficult, right?

Some tips

  • Have a look at the other applications for some inspiration.
  • mcpwm.h has all motor control functions and measurements that you can use.
  • The pin macros for the UART TX pin in this example are found in hwconf/hw_46.h. The ADC macros are also found there.
  • To print text for debugging in the BLDC Tool terminal, include commands.h and use the commands_printf function like the normal printf in C.
  • Include can.h and use the functions there to communicate with other VESCs over CAN bus. It is fairly easy and straight forward.
  • Have a look at the ChibiOS documentation which is very good. The testhal directory for this MCU has some good example applications that you can have a look at. Just be careful to not use drivers that are used by the VESC already, such as ADC. To read ADC values, use the macros and indexes found in hw_46.h that will use the existing implementation.
  • Eclipse is good for editing C source code. Just create a makefile project in eclipse and base it on the bldc-firmware directory. I will update this post or write a new one with how to make the indexing work in eclipse for C makefile projects, which is very useful.

Updates

  • 2016-01-22: Changed mcpwm to mc_interface for the FOC update.