try to fix submodule
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Simple example of custom commands that have nothing to do with the simple foc library
|
||||
*/
|
||||
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// instantiate the commander
|
||||
Commander command = Commander(Serial);
|
||||
|
||||
// led control function
|
||||
void doLed(char* cmd){
|
||||
if(atoi(cmd)) digitalWrite(LED_BUILTIN, HIGH);
|
||||
else digitalWrite(LED_BUILTIN, LOW);
|
||||
};
|
||||
// get analog input
|
||||
void doAnalog(char* cmd){
|
||||
if (cmd[0] == '0') Serial.println(analogRead(A0));
|
||||
else if (cmd[0] == '1') Serial.println(analogRead(A1));
|
||||
else if (cmd[0] == '2') Serial.println(analogRead(A2));
|
||||
else if (cmd[0] == '3') Serial.println(analogRead(A3));
|
||||
else if (cmd[0] == '4') Serial.println(analogRead(A4));
|
||||
};
|
||||
|
||||
void setup() {
|
||||
// define pins
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(A0, INPUT);
|
||||
pinMode(A1, INPUT);
|
||||
pinMode(A2, INPUT);
|
||||
pinMode(A3, INPUT);
|
||||
pinMode(A4, INPUT);
|
||||
|
||||
// Serial port to be used
|
||||
Serial.begin(115200);
|
||||
|
||||
// add new commands
|
||||
command.add('L', doLed, "led on/off");
|
||||
command.add('A', doAnalog, "analog read A0-A4");
|
||||
|
||||
Serial.println(F("Commander listening"));
|
||||
Serial.println(F(" - Send ? to see the node list..."));
|
||||
Serial.println(F(" - Send L0 to turn the led off and L1 to turn it off"));
|
||||
Serial.println(F(" - Send A0-A4 to read the analog pins"));
|
||||
_delay(1000);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
// user communication
|
||||
command.run();
|
||||
_delay(10);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Simple example of how to use the commander without serial - using just strings
|
||||
*/
|
||||
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// instantiate the commander
|
||||
Commander command = Commander();
|
||||
|
||||
// led control function
|
||||
void doLed(char* cmd){
|
||||
if(atoi(cmd)) digitalWrite(LED_BUILTIN, HIGH);
|
||||
else digitalWrite(LED_BUILTIN, LOW);
|
||||
};
|
||||
// get analog input
|
||||
void doAnalog(char* cmd){
|
||||
if (cmd[0] == '0') Serial.println(analogRead(A0));
|
||||
else if (cmd[0] == '1') Serial.println(analogRead(A1));
|
||||
};
|
||||
|
||||
void setup() {
|
||||
// define pins
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(A0, INPUT);
|
||||
pinMode(A1, INPUT);
|
||||
|
||||
// Serial port to be used
|
||||
Serial.begin(115200);
|
||||
|
||||
// add new commands
|
||||
command.add('L', doLed, "led control");
|
||||
command.add('A', doAnalog, "analog read A0-A1");
|
||||
|
||||
Serial.println(F("Commander running"));
|
||||
_delay(1000);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
// user communication
|
||||
command.run("?");
|
||||
_delay(2000);
|
||||
command.run("L0");
|
||||
_delay(1000);
|
||||
command.run("A0");
|
||||
_delay(1000);
|
||||
command.run("A1");
|
||||
_delay(1000);
|
||||
command.run("L1");
|
||||
_delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* A simple example to show how to use the commander with the control loops outside of the scope of the SimpleFOC library
|
||||
*/
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// BLDC motor & driver instance
|
||||
BLDCMotor motor = BLDCMotor(11);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(5, 10, 6, 8);
|
||||
|
||||
// encoder instance
|
||||
Encoder encoder = Encoder(2, 3, 500);
|
||||
// channel A and B callbacks
|
||||
void doA() { encoder.handleA(); }
|
||||
void doB() { encoder.handleB(); }
|
||||
|
||||
// target voltage to be set to the motor
|
||||
float target_velocity = 0;
|
||||
|
||||
// PID controllers and low pass filters
|
||||
PIDController PIDv{0.05, 1, 0, 100000000, 12};
|
||||
LowPassFilter LPFv{0.01};
|
||||
|
||||
//add communication
|
||||
Commander command = Commander(Serial);
|
||||
void doController(char* cmd) { command.pid(&PIDv, cmd); }
|
||||
void doFilter(char* cmd) { command.lpf(&LPFv, cmd); }
|
||||
void doTarget(char* cmd) { command.scalar(&target_velocity, cmd); }
|
||||
|
||||
|
||||
void setup() {
|
||||
|
||||
// initialize encoder sensor hardware
|
||||
encoder.init();
|
||||
encoder.enableInterrupts(doA, doB);
|
||||
// link the motor to the sensor
|
||||
motor.linkSensor(&encoder);
|
||||
|
||||
// driver config
|
||||
// power supply voltage [V]
|
||||
driver.init();
|
||||
// link driver
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
// set motion control loop to be used ( doing nothing )
|
||||
motor.torque_controller = TorqueControlType::voltage;
|
||||
motor.controller = MotionControlType::torque;
|
||||
|
||||
// use monitoring with serial
|
||||
Serial.begin(115200);
|
||||
motor.useMonitoring(Serial);
|
||||
// initialize motor
|
||||
motor.init();
|
||||
// align sensor and start FOC
|
||||
motor.initFOC();
|
||||
|
||||
// subscribe the new commands
|
||||
command.add('C', doController, "tune velocity pid");
|
||||
command.add('F', doFilter, "tune velocity LPF");
|
||||
command.add('T', doTarget, "motor target");
|
||||
|
||||
_delay(1000);
|
||||
Serial.println(F("Commander listening"));
|
||||
Serial.println(F(" - Send ? to see the node list..."));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop() {
|
||||
// looping foc
|
||||
motor.loopFOC();
|
||||
|
||||
// calculate voltage
|
||||
float target_voltage = PIDv(target_velocity - LPFv(motor.shaft_velocity));
|
||||
// set the voltage
|
||||
motor.move(target_voltage);
|
||||
|
||||
// user communication
|
||||
command.run();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* A simple example of reading step/dir communication
|
||||
* - this example uses hadware interrupts
|
||||
*/
|
||||
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// angle
|
||||
float received_angle = 0;
|
||||
|
||||
// StepDirListener( step_pin, dir_pin, counter_to_value)
|
||||
StepDirListener step_dir = StepDirListener(2, 3, 360.0/200.0); // receive the angle in degrees
|
||||
void onStep() { step_dir.handle(); }
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
// init step and dir pins
|
||||
step_dir.init();
|
||||
// enable interrupts
|
||||
step_dir.enableInterrupt(onStep);
|
||||
// attach the variable to be updated on each step (optional)
|
||||
// the same can be done asynchronously by caling step_dir.getValue();
|
||||
step_dir.attach(&received_angle);
|
||||
|
||||
Serial.println(F("Step/Dir listenning."));
|
||||
_delay(1000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.print(received_angle);
|
||||
Serial.print("\t");
|
||||
Serial.println(step_dir.getValue());
|
||||
_delay(500);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* A simple example of reading step/dir communication
|
||||
* - this example uses software interrupts - this code is intended primarily
|
||||
* for Arduino UNO/Mega and similar boards with very limited number of interrupt pins
|
||||
*/
|
||||
|
||||
#include <SimpleFOC.h>
|
||||
// software interrupt library
|
||||
#include <PciManager.h>
|
||||
#include <PciListenerImp.h>
|
||||
|
||||
|
||||
// angle
|
||||
float received_angle = 0;
|
||||
|
||||
// StepDirListener( step_pin, dir_pin, counter_to_value)
|
||||
StepDirListener step_dir = StepDirListener(4, 5, 2.0f*_PI/200.0); // receive the angle in radians
|
||||
void onStep() { step_dir.handle(); }
|
||||
|
||||
// If no available hadware interrupt pins use the software interrupt
|
||||
PciListenerImp listenStep(step_dir.pin_step, onStep);
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
// init step and dir pins
|
||||
step_dir.init();
|
||||
// enable software interrupts
|
||||
PciManager.registerListener(&listenStep);
|
||||
// attach the variable to be updated on each step (optional)
|
||||
// the same can be done asynchronously by caling step_dir.getValue();
|
||||
step_dir.attach(&received_angle);
|
||||
|
||||
Serial.println(F("Step/Dir listenning."));
|
||||
_delay(1000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.print(received_angle);
|
||||
Serial.print("\t");
|
||||
Serial.println(step_dir.getValue());
|
||||
_delay(500);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* A position control example using step/dir interface to update the motor position
|
||||
*/
|
||||
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// BLDC motor & driver instance
|
||||
BLDCMotor motor = BLDCMotor(11);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(10, 5, 6, 8);
|
||||
|
||||
// Stepper motor & driver instance
|
||||
//StepperMotor motor = StepperMotor(50);
|
||||
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
|
||||
|
||||
// encoder instance
|
||||
Encoder encoder = Encoder(2, 3, 500);
|
||||
// channel A and B callbacks
|
||||
void doA() { encoder.handleA(); }
|
||||
void doB() { encoder.handleB(); }
|
||||
|
||||
// StepDirListener( step_pin, dir_pin, counter_to_value)
|
||||
StepDirListener step_dir = StepDirListener(A4, A5, 2.0f*_PI/200.0);
|
||||
void onStep() { step_dir.handle(); }
|
||||
|
||||
void setup() {
|
||||
|
||||
// initialize encoder sensor hardware
|
||||
encoder.init();
|
||||
encoder.enableInterrupts(doA, doB);
|
||||
// link the motor to the sensor
|
||||
motor.linkSensor(&encoder);
|
||||
|
||||
// driver config
|
||||
// power supply voltage [V]
|
||||
driver.voltage_power_supply = 12;
|
||||
driver.init();
|
||||
// link the motor and the driver
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
// aligning voltage [V]
|
||||
motor.voltage_sensor_align = 3;
|
||||
// index search velocity [rad/s]
|
||||
motor.velocity_index_search = 3;
|
||||
|
||||
// set motion control loop to be used
|
||||
motor.controller = MotionControlType::angle;
|
||||
|
||||
// contoller configuration
|
||||
// default parameters in defaults.h
|
||||
// velocity PI controller parameters
|
||||
motor.PID_velocity.P = 0.2f;
|
||||
motor.PID_velocity.I = 20;
|
||||
motor.PID_velocity.D = 0;
|
||||
// default voltage_power_supply
|
||||
motor.voltage_limit = 12;
|
||||
// jerk control using voltage voltage ramp
|
||||
// default value is 300 volts per sec ~ 0.3V per millisecond
|
||||
motor.PID_velocity.output_ramp = 1000;
|
||||
|
||||
// velocity low pass filtering time constant
|
||||
motor.LPF_velocity.Tf = 0.01f;
|
||||
|
||||
// angle P controller
|
||||
motor.P_angle.P = 10;
|
||||
// maximal velocity of the position control
|
||||
motor.velocity_limit = 100;
|
||||
|
||||
// use monitoring with serial
|
||||
Serial.begin(115200);
|
||||
// comment out if not needed
|
||||
motor.useMonitoring(Serial);
|
||||
|
||||
// initialize motor
|
||||
motor.init();
|
||||
// align encoder and start FOC
|
||||
motor.initFOC();
|
||||
|
||||
// init step and dir pins
|
||||
step_dir.init();
|
||||
// enable interrupts
|
||||
step_dir.enableInterrupt(onStep);
|
||||
// attach the variable to be updated on each step (optional)
|
||||
// the same can be done asynchronously by caling motor.move(step_dir.getValue());
|
||||
step_dir.attach(&motor.target);
|
||||
|
||||
Serial.println(F("Motor ready."));
|
||||
Serial.println(F("Listening to step/dir commands!"));
|
||||
_delay(1000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// main FOC algorithm function
|
||||
motor.loopFOC();
|
||||
|
||||
// Motion control function
|
||||
motor.move();
|
||||
}
|
||||
Reference in New Issue
Block a user