Skip to content

Commit

Permalink
Added examples and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidGeek committed Aug 19, 2018
1 parent 1504819 commit 7468295
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 39 deletions.
33 changes: 33 additions & 0 deletions examples/setCurrent/setCurrent.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Name: setCurrent.ino
Created: 19-08-2018
Author: SolidGeek
Description: This is a very simple example of how to set the current for the motor
*/

#include <VescUart.h>

/** Initiate VescUart class */
VescUart UART;

float current = 1.0; /** The current in amps */

void setup() {
Serial.begin(9600);
/** Setup UART port (Serial1 on Atmega32u4) */
Serial1.begin(19200);

while (!Serial1) {;}

/** Define which ports to use as UART */
UART.setSerialPort(&Serial1);
}

void loop() {

/** Call the function setCurrent() to set the motor current */
UART.setCurrent(current);

// UART.setBrakeCurrent(current);

}
71 changes: 71 additions & 0 deletions examples/setNunchuckValues/setNunchuckValues.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Name: setNunchuckValues.ino
Created: 19-08-2018
Author: SolidGeek
Description: Use the serial monitor to set the speed/brake and enable cruise control with the nunchuck values.
This example is made using a Arduino Micro (Atmega32u4) that has a HardwareSerial port (Serial1) seperated from the Serial port.
A Arduino Nano or Uno that only has one Serial port will not be able to get the commands over the Serial monitor.
*/

#include <VescUart.h>

/** Initiate VescUart class */
VescUart UART;

void setup() {

/** Setup Serial port to enter commands */
Serial.begin(9600);

/** Setup UART port (Serial1 on Atmega32u4) */
Serial1.begin(19200);

while (!Serial1) {;}

/** Define which ports to use as UART */
UART.setSerialPort(&Serial1);
}

String command;
int throttle = 127;

void loop() {

if (Serial.available()) {

command = Serial.readStringUntil('\n');

if (command.equals("cruise")) {

/** The lowerButton is used to set cruise control */
UART.nunchuck.lowerButton = true;

}
else if (command.equals("nocruise")) {

/** The lowerButton is used to set cruise control */
UART.nunchuck.lowerButton = false;

}else {

throttle = command.toInt();

if ( throttle >= 0 && throttle <= 255) {

/** The valueY is used to control the speed, where 127 is the middle = no current */
UART.nunchuck.valueY = throttle;
Serial.println(throttle);

} else {

throttle = 127;
Serial.println("Unvalid throttle value");

}
}
}

/** Call the function setNunchuckValues to send the current nunchuck values to the VESC */
UART.setNunchuckValues();

}
16 changes: 10 additions & 6 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ VescUart KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
setSerialPort
setDebugPort
getVescValues KEYWORD2
printVescValues KEYWORD2
setNunchuckValues
printVescValues
setSerialPort KEYWORD2
setDebugPort KEYWORD2
getVescValues KEYWORD2
printVescValues KEYWORD2
setNunchuckValues KEYWORD2
printVescValues KEYWORD2
setCurrent KEYWORD2
setBrakeCurrent KEYWORD2
setRPM KEYWORD2
setDuty KEYWORD2
92 changes: 66 additions & 26 deletions src/VescUart.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
#include "VescUart.h"
#include <HardwareSerial.h>


VescUart::VescUart(void){
nunchuck.valXJoy = 127;
nunchuck.valYJoy = 127;
nunchuck.valLowerButton = false;
nunchuck.valLowerButton = false;
nunchuck.valueX = 127;
nunchuck.valueY = 127;
nunchuck.lowerButton = false;
nunchuck.upperButton = false;
}

void VescUart::setSerialPort(HardwareSerial* _serialPort)
void VescUart::setSerialPort(HardwareSerial* port)
{
serialPort = _serialPort;
serialPort = port;
}

void VescUart::setDebugPort(Stream* _debugPort)
void VescUart::setDebugPort(Stream* port)
{
debugPort = _debugPort;
debugPort = port;
}

int VescUart::receiveUartMessage(uint8_t * payloadReceived) {

// Messages <= 255 starts with "2", 2nd byte is length
// Messages > 255 starts with "3" 2nd and 3rd byte is length combined with 1st >>8 and then &0xFF

unsigned int counter = 0;
unsigned int endMessage = 256;
uint16_t counter = 0;
uint16_t endMessage = 256;
bool messageRead = false;
uint8_t messageReceived[256];
unsigned int lenPayload = 0;
uint16_t lenPayload = 0;

unsigned long timeout = 0;
timeout = millis() + 100;
uint32_t timeout = millis() + 100; // Defining the timestamp for timeout (100ms before timeout)

while ( millis() < timeout && messageRead == false) {

Expand All @@ -50,11 +48,14 @@ int VescUart::receiveUartMessage(uint8_t * payloadReceived) {

case 3:
// ToDo: Add Message Handling > 255 (starting with 3)
if( debugPort != NULL ){
debugPort->println("Message is larger than 256 bytes - not supported");
}
break;

default:
if( debugPort != NULL ){
debugPort->println("No start bit");
debugPort->println("Unvalid start bit");
}
break;
}
Expand All @@ -70,16 +71,16 @@ int VescUart::receiveUartMessage(uint8_t * payloadReceived) {
debugPort->println("End of message reached!");
}
messageRead = true;
break; //Exit if end of message is reached, even if there is still more data in buffer.
break; // Exit if end of message is reached, even if there is still more data in the buffer.
}
}
}

if(messageRead == false && debugPort != NULL ) {
debugPort->println("Timeout");
}

bool unpacked = false;

if (messageRead) {
unpacked = unpackPayload(messageReceived, endMessage, payloadReceived);
}
Expand Down Expand Up @@ -181,9 +182,9 @@ bool VescUart::processReadPacket(uint8_t * message) {
message++; // Removes the packetId from the actual message (payload)

switch (packetId){
case COMM_GET_VALUES:
case COMM_GET_VALUES: // Structure defined here: https://github.com/vedderb/bldc/blob/43c3bbaf91f5052a35b75c2ff17b5fe99fad94d1/commands.c#L164

ind = 4; // Skip the first 4 bytes
ind = 4; // Skip the first 4 bytes
data.avgMotorCurrent = buffer_get_float32(message, 100.0, &ind);
data.avgInputCurrent = buffer_get_float32(message, 100.0, &ind);
ind += 8; // Skip the next 8 bytes
Expand Down Expand Up @@ -230,10 +231,10 @@ void VescUart::setNunchuckValues() {
uint8_t payload[11];

payload[ind++] = COMM_SET_CHUCK_DATA;
payload[ind++] = nunchuck.valXJoy;
payload[ind++] = nunchuck.valYJoy;
buffer_append_bool(payload, nunchuck.valLowerButton, &ind);
buffer_append_bool(payload, nunchuck.valUpperButton, &ind);
payload[ind++] = nunchuck.valueX;
payload[ind++] = nunchuck.valueY;
buffer_append_bool(payload, nunchuck.lowerButton, &ind);
buffer_append_bool(payload, nunchuck.upperButton, &ind);

// Acceleration Data. Not used, Int16 (2 byte)
payload[ind++] = 0;
Expand All @@ -245,13 +246,53 @@ void VescUart::setNunchuckValues() {

if(debugPort != NULL){
debugPort->println("Data reached at setNunchuckValues:");
debugPort->print("valXJoy = "); debugPort->print(nunchuck.valXJoy); debugPort->print(" valYJoy = "); debugPort->println(nunchuck.valYJoy);
debugPort->print("LowerButton = "); debugPort->print(nunchuck.valLowerButton); debugPort->print(" UpperButton = "); debugPort->println(nunchuck.valUpperButton);
debugPort->print("valueX = "); debugPort->print(nunchuck.valueX); debugPort->print(" valueY = "); debugPort->println(nunchuck.valueY);
debugPort->print("LowerButton = "); debugPort->print(nunchuck.lowerButton); debugPort->print(" UpperButton = "); debugPort->println(nunchuck.upperButton);
}

packSendPayload(payload, 11);
}

void VescUart::setCurrent(float current) {
int32_t index = 0;
uint8_t payload[5];

payload[index++] = COMM_SET_CURRENT;
buffer_append_int32(payload, (int32_t)(current * 1000), &index);

packSendPayload(payload, 5);
}

void VescUart::setBrakeCurrent(float brakeCurrent) {
int32_t index = 0;
uint8_t payload[5];

payload[index++] = COMM_SET_CURRENT_BRAKE;
buffer_append_int32(payload, (int32_t)(brakeCurrent * 1000), &index);

packSendPayload(payload, 5);
}

void VescUart::setRPM(float rpm) {
int32_t index = 0;
uint8_t payload[5];

payload[index++] = COMM_SET_RPM ;
buffer_append_int32(payload, (int32_t)(rpm), &index);

packSendPayload(payload, 5);
}

void VescUart::setDuty(float duty) {
int32_t index = 0;
uint8_t payload[5];

payload[index++] = COMM_SET_DUTY;
buffer_append_int32(payload, (int32_t)(duty * 100000), &index);

packSendPayload(payload, 5);
}

void VescUart::serialPrint(uint8_t * data, int len) {
if(debugPort != NULL){
for (int i = 0; i <= len; i++)
Expand All @@ -264,7 +305,6 @@ void VescUart::serialPrint(uint8_t * data, int len) {
}
}


void VescUart::printVescValues() {
if(debugPort != NULL){
debugPort->print("avgMotorCurrent: "); debugPort->println(data.avgMotorCurrent);
Expand Down
39 changes: 32 additions & 7 deletions src/VescUart.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class VescUart

/** Struct to hold the nunchuck values to send over UART */
struct nunchuckPackage {
int valXJoy;
int valYJoy;
bool valUpperButton;
bool valLowerButton;
int valueX;
int valueY;
bool upperButton; // valUpperButton
bool lowerButton; // valLowerButton
};


Expand All @@ -44,14 +44,15 @@ class VescUart

/**
* @brief Set the serial port for uart communication
* @param port - Reference to Serial port (pointer)
*/
void setSerialPort(HardwareSerial* _serialPort);
void setSerialPort(HardwareSerial* port);

/**
* @brief Set the serial port for debugging
* @param Serial object
* @param port - Reference to Serial port (pointer)
*/
void setDebugPort(Stream* _debugSerialPort);
void setDebugPort(Stream* port);

/**
* @brief Sends a command to VESC and stores the returned data
Expand All @@ -65,6 +66,30 @@ class VescUart
*/
void setNunchuckValues(void);

/**
* @brief Set the current to drive the motor
* @param current - The current to apply
*/
void setCurrent(float current);

/**
* @brief Set the current to brake the motor
* @param brakeCurrent - The current to apply
*/
void setBrakeCurrent(float brakeCurrent);

/**
* @brief Set the rpm of the motor
* @param rpm - The desired RPM (actually eRPM = RPM * poles)
*/
void setRPM(float rpm);

/**
* @brief Set the duty of the motor
* @param duty - The desired duty (0.0-1.0)
*/
void setDuty(float duty);

/**
* @brief Help Function to print struct dataPackage over Serial for Debug
*/
Expand Down

0 comments on commit 7468295

Please sign in to comment.