Skip to content

Dev #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
May 18, 2021
Merged

Dev #79

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
ce52535
Issue #46: motor.command() and CRLF
maxlem-neuralium May 10, 2021
2abb7ed
Issue #46 : missing use of isSentinel
maxlem-neuralium May 10, 2021
6b2e449
Merge branch 'master' into dev
maxlem-neuralium May 10, 2021
a71f12c
Samd support add support for serial insterface other than Serial
maxlem-neuralium May 10, 2021
5a9f092
prevent buffer overrun when commands sent are too long
runger1101001 May 10, 2021
ef7cf73
unfortunately, the string "EXTERN" is defined in rp2040 headers
runger1101001 May 10, 2021
1fa5dd7
the constants SDA and SCL are not defined in rp2040.
runger1101001 May 10, 2021
8da3507
unfortunately this code does not work with rp2040
runger1101001 May 10, 2021
3fc05d2
rp2040 (aka Raspberry Pi Pico) support
runger1101001 May 10, 2021
d8011fc
Merge branch 'dev' of https://github.com/simplefoc/Arduino-FOC.git in…
runger1101001 May 10, 2021
5fa5893
Merge branch 'dev' into rp2040
runger1101001 May 10, 2021
8246fe5
Merge pull request #75 from maxlem/master
askuric May 13, 2021
a8a72b2
Merge branch 'dev' into dev
askuric May 13, 2021
5e6dc72
Merge pull request #77 from runger1101001/dev
askuric May 13, 2021
d37f34e
initial low side added
askuric May 13, 2021
9220ead
division by zero issue fix #76 + defines for default pwm freq
askuric May 13, 2021
e3c91e1
reduced \r error string + target setting separated + unknowm cmd error
askuric May 13, 2021
ffdc4b1
dead zone as5600 register fix - set to 0x0C
askuric May 13, 2021
39d2cef
removed old fix in drivers for issue #76
askuric May 13, 2021
b893f11
removed old fix in drivers for issue #76 2
askuric May 13, 2021
b5dccfb
readme prepared for v2.1.1
askuric May 13, 2021
bf45b6a
FIX phase resitence not changing voltage_limit
askuric May 13, 2021
3ec3038
examples for 6mOhm resistor
askuric May 13, 2021
d1b41d0
v2.1.1 update readme
askuric May 13, 2021
a3741bf
readme html problem resolution
askuric May 13, 2021
5fcfbfe
FEAT pwm sensor can be blocking or nonblocking
askuric May 13, 2021
b1d26d2
examples for nonblocking
askuric May 13, 2021
e43425c
Merge pull request #78 from runger1101001/rp2040
askuric May 13, 2021
3fde38d
support for RPI Pico to readme + Pullup naming update
askuric May 13, 2021
a4f9999
Annoying pullup struct rename from EXTERN/INTERN to USE_EXTERN/USE_IN…
askuric May 13, 2021
dd3dbeb
Merge branch 'FEAT_low_side_cs' into dev
askuric May 14, 2021
c21dc2e
LowSideCurrentSense: first (truly) successful DMA+ADC readings, 1.65-…
maxlem May 15, 2021
e6d77b0
wrapped samd21 adc/dma implementation in an ugly class
maxlem May 15, 2021
0b7839f
FEAT added weak definitions of generic hardware implementations + ard…
askuric May 15, 2021
9af38fa
SAMDCurrentSenseADCDMA class
maxlem May 15, 2021
7698c0e
FEAT ADC-DMA fix deadlock
maxlem May 18, 2021
a0d17a0
Samd support add support for serial insterface other than Serial
maxlem-neuralium May 10, 2021
db7d865
LowSideCurrentSense: first (truly) successful DMA+ADC readings, 1.65-…
maxlem May 15, 2021
7d3c011
wrapped samd21 adc/dma implementation in an ugly class
maxlem May 15, 2021
a5c7528
SAMDCurrentSenseADCDMA class
maxlem May 15, 2021
7e802f6
FEAT ADC-DMA fix deadlock
maxlem May 18, 2021
0d54abe
Merge branch 'dev' of github.com:maxlem/Arduino-FOC into dev
maxlem May 18, 2021
bb7d29c
FEAT add _startADC3PinConversionLowSide() API
maxlem May 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ Therefore this is an attempt to:
- ***NEW*** 📢: *Medium-power* BLDC driver (<30Amps): [Arduino <span class="simple">Simple<b>FOC</b>PowerShield</span> ](https://github.com/simplefoc/Arduino-SimpleFOC-PowerShield).
- See also [@byDagor](https://github.com/byDagor)'s *fully-integrated* ESP32 based board: [Dagor Brushless Controller](https://github.com/byDagor/Dagor-Brushless-Controller)

##### <b> Next release will be:</b> <i>Simple<b>FOC</b>library v2.1.1
##### <b> Release notes be:</b> <i>Simple<b>FOC</b>library</i> v2.1.1
> - bugfixes commander
> - bugfix `voltage_limit` when provided `phase_resistance`
> - bugfix `hall_sensor` examples
> - added examples fot the PowerShield
> - added initial support for `MagneticSensorPWM`
> - SAMD51 support
> - **initial support for Raspberry pi Pico**
> - added examples to find the raw max and min of the analog and pwm sensor
> - extension of the `Commander` interface
> - added pwm centering option `WC`
> - added pwm modulation cmd `WT`
> - improved esp32 implementation to avoid the need for mcpwm.h changes by @tschundler
> - issue #62: motor.shaft_angle setting in the motor.initFOC()
> - issue #76: esp32 division by zero
> - issue #46: commander end of line character - by @maxlem
> - [community fix](https://community.simplefoc.com/t/as5600-dead-spot-around-0/208) AS5600 register value
> - renamed `Pullup::EXTERN` and `Pullup::INTERN` to `Pullup::USE_EXTERN` and `Pullup::USE_INTERN`

## Arduino *SimpleFOClibrary* v2.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ MagneticSensorSPI sensor = MagneticSensorSPI(PA4, 14, 0x3FFF);
// make sure to use the pull-ups!!
// SDA PB7
// SCL PB6
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);

// Motor instance
BLDCMotor motor = BLDCMotor(11);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ void doB2(){encoder2.handleB();}


// inline current sensor instance
// check if your board has R010 (0.01 ohm resistor) or R006 (0.006 mOhm resistor)
InlineCurrentSense current_sense1 = InlineCurrentSense(0.01, 50.0, A0, A2);

// inline current sensor instance
// check if your board has R010 (0.01 ohm resistor) or R006 (0.006 mOhm resistor)
InlineCurrentSense current_sense2 = InlineCurrentSense(0.01, 50.0, A1, A3);

// commander communication instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void doA(){encoder.handleA();}
void doB(){encoder.handleB();}

// inline current sensor instance
// check if your board has R010 (0.01 ohm resistor) or R006 (0.006 mOhm resistor)
InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0, A0, A2);

// commander communication instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BLDCMotor motor = BLDCMotor(11);
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11, 8);
//StepperMotor motor = StepperMotor(50);
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
// magnetic sensor instance - SPI
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
// magnetic sensor instance - I2C
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);
// magnetic sensor instance - analog output
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// magnetic sensor instance - SPI
//MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
// magnetic sensor instance - I2C
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);
// magnetic sensor instance - analog output
MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void setup() {
encoder.quadrature = Quadrature::ON;

// check if you need internal pullups
encoder.pullup = Pullup::EXTERN;
encoder.pullup = Pullup::USE_EXTERN;

// initialise encoder hardware
encoder.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void setup() {
encoder.quadrature = Quadrature::ON;

// check if you need internal pullups
encoder.pullup = Pullup::EXTERN;
encoder.pullup = Pullup::USE_EXTERN;

// initialise encoder hardware
encoder.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void setup() {
Serial.begin(115200);

// check if you need internal pullups
sensor.pullup = Pullup::EXTERN;
sensor.pullup = Pullup::USE_EXTERN;

// initialise encoder hardware
sensor.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void setup() {
Serial.begin(115200);

// check if you need internal pullups
sensor.pullup = Pullup::EXTERN;
sensor.pullup = Pullup::USE_EXTERN;

// initialise encoder hardware
sensor.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void setup() {

// initialise magnetic sensor hardware
sensor.init();
// comment out to use sensor in blocking (non-interrupt) way
sensor.enableInterrupt(doPWM);

Serial.println("Sensor ready");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void setup() {

// initialise magnetic sensor hardware
sensor.init();
// comment out to use sensor in blocking (non-interrupt) way
sensor.enableInterrupt(doPWM);

Serial.println("Sensor ready");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void setup() {

// initialise magnetic sensor hardware
sensor.init();
// comment out to use sensor in blocking (non-interrupt) way
PciManager.registerListener(&listenerPWM);

Serial.println("Sensor ready");
Expand Down
4 changes: 2 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ pinB KEYWORD2
pinC KEYWORD2
index_pin KEYWORD2

INTERN KEYWORD2
EXTERN KEYWORD2
USE_INTERN KEYWORD2
USE_EXTERN KEYWORD2
DISABLE KEYWORD2
ENABLE KEYWORD2
SpaceVectorPWM KEYWORD2
Expand Down
1 change: 1 addition & 0 deletions src/SimpleFOC.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void loop() {
#include "drivers/StepperDriver4PWM.h"
#include "drivers/StepperDriver2PWM.h"
#include "current_sense/InlineCurrentSense.h"
#include "current_sense/LowsideCurrentSense.h"
#include "communication/Commander.h"
#include "communication/StepDirListener.h"

Expand Down
6 changes: 3 additions & 3 deletions src/common/base_classes/Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ enum Direction{
* Pullup configuration structure
*/
enum Pullup{
INTERN, //!< Use internal pullups
EXTERN //!< Use external pullups
USE_INTERN, //!< Use internal pullups
USE_EXTERN //!< Use external pullups
};

/**
Expand All @@ -43,4 +43,4 @@ class Sensor{
long velocity_calc_timestamp=0; //!< last velocity calculation timestamp
};

#endif
#endif
1 change: 1 addition & 0 deletions src/common/foc_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define _constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define _sqrt(a) (_sqrtApprox(a))
#define _isset(a) ( (a) != (NOT_SET) )
#define _UNUSED(v) (void) (v)

// utility defines
#define _2_SQRT3 1.15470053838
Expand Down
93 changes: 55 additions & 38 deletions src/communication/Commander.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "Commander.h"


Commander::Commander(Stream& serial){
Commander::Commander(Stream& serial, char eol, bool echo){
com_port = &serial;
this->eol = eol;
this->echo = echo;
}
Commander::Commander(){
// do nothing
Commander::Commander(char eol, bool echo){
this->eol = eol;
this->echo = echo;
}


Expand All @@ -19,43 +22,39 @@ void Commander::add(char id, CommandCallback onCommand, char* label ){

void Commander::run(){
if(!com_port) return;
// a string to hold incoming data
while (com_port->available()) {
// get the new byte:
received_chars[rec_cnt] = (char)com_port->read();
// end of user input
if (received_chars[rec_cnt++] == '\n') {
// execute the user command
run(received_chars);

// reset the command buffer
received_chars[0] = 0;
rec_cnt=0;
}
}
run(*com_port, eol);
}

void Commander::run(Stream& serial){
void Commander::run(Stream& serial, char eol){
Stream* tmp = com_port; // save the serial instance
// use the new serial instance to output if not available the one linked in constructor
if(!tmp) com_port = &serial;
char eol_tmp = this->eol;
this->eol = eol;
com_port = &serial;

// a string to hold incoming data
while (serial.available()) {
// get the new byte:
received_chars[rec_cnt] = (char)serial.read();
int ch = serial.read();
received_chars[rec_cnt++] = (char)ch;
// end of user input
if (received_chars[rec_cnt++] == '\n') {
if(echo)
print((char)ch);
if (isSentinel(ch)) {
// execute the user command
run(received_chars);

// reset the command buffer
received_chars[0] = 0;
rec_cnt=0;
}
if (rec_cnt>=MAX_COMMAND_LENGTH) { // prevent buffer overrun if message is too long
received_chars[0] = 0;
rec_cnt=0;
}
}

com_port = tmp; // reset the instance to the internal value
this->eol = eol_tmp;
}

void Commander::run(char* user_input){
Expand All @@ -71,7 +70,7 @@ void Commander::run(char* user_input){
}
break;
case CMD_VERBOSE:
if(user_input[1] != '\n') verbose = (VerboseMode)atoi(&user_input[1]);
if(!isSentinel(user_input[1])) verbose = (VerboseMode)atoi(&user_input[1]);
printVerbose(F("Verb:"));
switch (verbose){
case VerboseMode::nothing:
Expand All @@ -84,7 +83,7 @@ void Commander::run(char* user_input){
}
break;
case CMD_DECIMAL:
if(user_input[1] != '\n') decimal_places = atoi(&user_input[1]);
if(!isSentinel(user_input[1])) decimal_places = atoi(&user_input[1]);
printVerbose(F("Decimal:"));
println(decimal_places);
break;
Expand All @@ -100,14 +99,25 @@ void Commander::run(char* user_input){
}

void Commander::motor(FOCMotor* motor, char* user_command) {

// if target setting
if(isDigit(user_command[0]) || user_command[0] == '-' || user_command[0] == '+'){
printVerbose(F("Target: "));
motor->target = atof(user_command);
println(motor->target);
return;
}

// parse command letter
char cmd = user_command[0];
char sub_cmd = user_command[1];
// check if there is a subcommand or not
int value_index = (sub_cmd >= 'A' && sub_cmd <= 'Z') ? 2 : 1;
// check if get command
bool GET = user_command[value_index] == '\n';
bool GET = isSentinel(user_command[value_index]);
// parse command values
float value = atof(&user_command[value_index]);
float value = atof(&user_command[value_index]);


// a bit of optimisation of variable memory for Arduino UNO (atmega328)
switch(cmd){
Expand Down Expand Up @@ -149,10 +159,8 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
printVerbose(F("curr: "));
if(!GET){
motor->current_limit = value;
// if phase resistance is set, change the voltage limit as well.
if(_isset(motor->phase_resistance)) motor->voltage_limit = value*motor->phase_resistance;
// if phase resistance specified or the current control is on set the current limit to the velocity PID
if(_isset(motor->phase_resistance) || motor->torque_controller != TorqueControlType::voltage ) motor->PID_velocity.limit = value;
if(_isset(motor->phase_resistance) || motor->torque_controller != TorqueControlType::voltage ) motor->PID_velocity.limit = value;
}
println(motor->current_limit);
break;
Expand All @@ -179,7 +187,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
break;
default:
// change control type
if(!GET && value >= 0 && (int)value < 5)// if set command
if(!GET && value >= 0 && (int)value < 5) // if set command
motor->controller = (MotionControlType)value;
switch(motor->controller){
case MotionControlType::torque:
Expand Down Expand Up @@ -354,7 +362,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
case SCMD_SET:
if(!GET) motor->monitor_variables = (uint8_t) 0;
for(int i = 0; i < 7; i++){
if(user_command[value_index+i] == '\n') break;
if(isSentinel(user_command[value_index+i])) break;
if(!GET) motor->monitor_variables |= (user_command[value_index+i] - '0') << (6-i);
print( (user_command[value_index+i] - '0') );
}
Expand All @@ -365,16 +373,15 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
break;
}
break;
default: // target change
printVerbose(F("Target: "));
motor->target = atof(user_command);
println(motor->target);
default: // unknown cmd
printVerbose(F("unknown cmd "));
printError();
}
}

void Commander::pid(PIDController* pid, char* user_cmd){
char cmd = user_cmd[0];
bool GET = user_cmd[1] == '\n';
bool GET = isSentinel(user_cmd[1]);
float value = atof(&user_cmd[1]);

switch (cmd){
Expand Down Expand Up @@ -411,7 +418,7 @@ void Commander::pid(PIDController* pid, char* user_cmd){

void Commander::lpf(LowPassFilter* lpf, char* user_cmd){
char cmd = user_cmd[0];
bool GET = user_cmd[1] == '\n';
bool GET = isSentinel(user_cmd[1]);
float value = atof(&user_cmd[1]);

switch (cmd){
Expand All @@ -427,11 +434,21 @@ void Commander::lpf(LowPassFilter* lpf, char* user_cmd){
}

void Commander::scalar(float* value, char* user_cmd){
bool GET = user_cmd[0] == '\n';
bool GET = isSentinel(user_cmd[0]);
if(!GET) *value = atof(user_cmd);
println(*value);
}

bool Commander::isSentinel(char ch)
{
if(ch == eol)
return true;
else if (ch == '\r')
{
printVerbose(F("Warn: \\r detected! \n"));
}
return false;
}

void Commander::print(const int number){
if( !com_port || verbose == VerboseMode::nothing ) return;
Expand Down
Loading