Arduino integration with VESC issue

I am trying to integrate the vesc with arduino and I am running a simple code to print the voltage and other data into the serial monitor. Below is the code for reference. When I chekc the output, it doesnt seem to be parsing the data correctly. I see the output as @⸮ @⸮ @⸮ @⸮ I am using the code from this github page: VESC-UART-Arduino/VESC_UART_Nano.ino at master · R0b0shack/VESC-UART-Arduino (github.com)

` /* Arduino UART communication with VESC. This software reads VESC telemetr data and displays it on an OLED display. This code is under development for an improved ppm remote control.

It is written by Sascha Ederer (roboshack.wordpress.com), based on the code of jenkie (pedelecforum.de), Andreas Chaitidis ([email protected]) and Benjamin Vedder (www.vedder.se). Copyright (C) 2016

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include “config.h” //#include “printf.h” #include “datatypes.h” #include “vesc_uart.h” #include <SPI.h>

//Library for the OLED Display #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>

mc_values VescMeasuredValues;

float current = 0.0; //measured battery current float motor_current = 0.0; //measured motor current float voltage = 0.0; //measured battery voltage float c_speed = 0.0; //measured rpm * Pi * wheel diameter [km] * 60 [minutes] float c_dist = 0.00; //measured odometry tachometer [turns] * Pi * wheel diameter [km] double power = 0.0; //calculated power

#define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET);

//Setup--------------------------------------------------------------------------------------------------------------------- void setup() {

    SERIALIO.begin(115200);
    
 /*   // initialize with the I2C addr 0x3C
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

    display.clearDisplay();

// set text color
display.setTextColor(WHITE);
// set text size
display.setTextSize(1);
// set text cursor position
display.setCursor(1,0);
// show text
display.println("System startup");
display.display();
    delay(1000);
display.clearDisplay();*/

}

void loop() { if (vesc_get_values(VescMeasuredValues)) {

    // calculation of several values to be displayed later on
    voltage = VescMeasuredValues.v_in;
    current = VescMeasuredValues.current_in;
    motor_current = VescMeasuredValues.current_motor;
    power = current*voltage;
    c_speed = (VescMeasuredValues.rpm/38)*3.14159265359*0.000083*60;
    c_dist = (VescMeasuredValues.tachometer/38)*3.14159265359*0.000083;

Serial.println(voltage); //Serial.println(current); /Serial.println(motor_current);Serial.println(power); Serial.println(c_speed); Serial.println(c_dist);/

}
else
{
    Serial.println(" ");

} }` Does anyone know what can be causing this issue?

It could be becuase your Baud rate is wrong.

You have not opened the Serial port in Arduino inside the setup() { Serial.begin(115200); }

Although it seems open (as you get @?), you don’t know what baud rate it is set to (hence your data is junk)

1 Like

Hi Louis,

I have tried changing the baud rate in the vesc tool using the default dropdown section. I tried setting it to 100k and 125k, but that doesn’t help me. I am still seeing @? data as output. Is there a different baud rate that I should try. also I am using the can baud rate to set the baud rate on the vesc, is that the correct one or am I missing something:

I mean in the Arduino tool.

You have modified RoboShacks code to print the values onto the serial port (Arduino USB), instead of the screen.

But you have not initialized the port. Add:

Serial.begin(115200);

inside the curly brackets after ‘void setup()’

Then in Arduino Serial monitor, choose 115200 baud.

1 Like

Like this: void setup() {

   Serial.begin(115200);

}

void loop() {

 if (vesc_get_values(VescMeasuredValues)) { 
      voltage = VescMeasuredValues.v_in;
      current = VescMeasuredValues.current_in;
      motor_current = VescMeasuredValues.current_motor;
      power = current*voltage;
      c_speed = (VescMeasuredValues.rpm/38)*3.14159265359*0.000083*60;
      c_dist = (VescMeasuredValues.tachometer/38)*3.14159265359*0.000083;
      
      Serial.print("Voltage: ");
      Serial.println(voltage);
      Serial.print("current: ");
      Serial.println(current);
      Serial.print("c_speed: ");
      Serial.println(c_speed);
      Serial.print("c_dist: ");
      Serial.println(c_dist);
  }

}

1 Like

Another question: when I am powering the arduino, I am using the USB from the laptop. But the vesc is powered using a power supply.

I have made the following connections. Gnd VESC-> GND arduino RX VESC-> TX Arduino TX VESC-> RX arduino. Does that seem to be correct?

The Arduino will be powered from the PC (USB).

Unless you also connect to 5V on the VESC to 5V on Arduino.

Your other connections sound okay.

1 Like

Even after changing the code I seem to have the same issue. Below is my code:

#include “config.h” //#include “printf.h” #include “datatypes.h” #include “vesc_uart.h” #include <SPI.h>

//Library for the OLED Display #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>

mc_values VescMeasuredValues;

float current = 0.0; //measured battery current float motor_current = 0.0; //measured motor current float voltage = 0.0; //measured battery voltage float c_speed = 0.0; //measured rpm * Pi * wheel diameter [km] * 60 [minutes] float c_dist = 0.00; //measured odometry tachometer [turns] * Pi * wheel diameter [km] double power = 0.0; //calculated power

#define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET);

//Setup--------------------------------------------------------------------------------------------------------------------- void setup() {

  //  SERIALIO.begin(115200);
    { Serial.begin(115200); }

}

void loop() { if (vesc_get_values(VescMeasuredValues)) { voltage = VescMeasuredValues.v_in; current = VescMeasuredValues.current_in; motor_current = VescMeasuredValues.current_motor; power = currentvoltage; c_speed = (VescMeasuredValues.rpm/38)3.141592653590.00008360; c_dist = (VescMeasuredValues.tachometer/38)3.141592653590.000083;

  Serial.print("Voltage: ");
  Serial.println(voltage);
  Serial.print("current: ");
  Serial.println(current);
  Serial.print("c_speed: ");
  Serial.println(c_speed);
  Serial.print("c_dist: ");
  Serial.println(c_dist);

} }

Below is the image of the serial monitor:

and the snapshot of the vesc settings:

Also when I probe the tx and rx pin on an oscilloscope and logic analyzer… initially i saw data for a second or 2 and later there were no pulses at all. does that sound ok? I tried 2 vesc and they both behaved the same way

The code you sent does not compile for me. Are you sure you have Uploaded the update before you open the serial monitor?

Should be more like this:

Try this simple code instead and see if this works for you.

void setup() { Serial.begin(115200); Serial.print(“Hi”); }

void loop() { Serial.print("."); delay(100); }

VESC_UART_Nano.zip (11.2 KB) it complied successfully for me:

Also I have uploaded the file

You still have too many curly brackets. This

image

Should be like this:

image

But this is not your problem. You have misunderstood how this works.

I am doing some testing. The issue is that the serial port you are trying to talk to the computer on is the same as the port it is trying to talk to the VESC with.

You are seeing the messages meant for the VESC. Hence you cannot get your debugging.

What type of Arduino are you using? and what are you trying to obtain? I can help you better if you start with that info

1 Like

ohh… maybe now I understand. I am using arduino MKR1400. I am trying to get the data out of the vesc for now and later will print that over the display(next phase). As of now I am just running some bench test. But even if I have no vesc connected to my laptop(i disconnected it after I changed the settings and only have the arduino hooked to the laptop), it still does the same thing

Okay, so eventually you will have a screen, so the lack of port may not be a problem.

I suggest commissioning the screen first, and when you know you can display a numbers on it, then trying to re-do your VESC sketch.

Now you know you cannot use the USB serial, this will probably be fine. Your device is not really like an Arduino, so you may run into quirks, but worth the effort if you can get GSM.


You could test the communication is working by using an LED and a digital output.

Adding this to the loop for example (then powering the wheels)

if (current > 0) { digitalWrite(LEDpin,HIGH); }

else { digitalWrite(LEDpin,LOW); }

1 Like

OK let me do a quick setup if the display and test this code out

I did a quick test using a adafruit display. It seems to be that when it goes into the void loop() it does not print anything. So as of now my display only shows: Test display. Attached is the code for reference. VESC_UART_Nano_1.zip (12.2 KB)

Also I added the following code: if (current > 0) { tft.println(“current>0”); digitalWrite(LedPin,HIGH);} else { tft.println(“current<0”); digitalWrite(LedPin,LOW);}

but the LED is always off and the display shows Test display VESC_UART_Nano_2.zip (12.3 KB)

If the data from the VESC fails, your code will not print anything to the TFT in the loop.

Try this:

image

The dots are your existing function