/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
#include <SimpleFOC.h>
int ledPin = LED_BUILTIN; // LED connected to digital pin 9
int val = 0; // variable to store the read value
int random_variable;
int static_variable = 500;
// Encoder(pin_A, pin_B, PPR)
Encoder sensor = Encoder(2, 3, 2048);
// channel A and B callbacks
void doA(){sensor.handleA(); Serial.println("A Channel!"); }
void doB(){sensor.handleB(); Serial.println("B Channel!");}
// BLDCDriver3PWM( pin_pwmA, pin_pwmB, pin_pwmC, enable (optional))
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
// InlineCurrentSense(shunt_resistance, gain, adc_a, adc_b)
InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50, A0, A2);
// BLDCMotor( pole_pairs , ( phase_resistance, KV_rating optional) )
BLDCMotor motor = BLDCMotor(11, 9.75);
// commander interface
Commander command = Commander(Serial);
void onMotor(char* cmd){ command.motor(&motor, cmd); }
void setup() {
//pinMode(ledPin, OUTPUT); // sets the pin as output
//Serial.begin(9600);
// initialize encoder hardware
sensor.init();
// hardware interrupt enable
sensor.enableInterrupts(doA, doB);
// init sensor
// pwm frequency to be used [Hz]
driver.pwm_frequency = 1000;
// power supply voltage [V]
driver.voltage_power_supply = 12;
// Max DC voltage allowed - default voltage_power_supply
driver.voltage_limit = 12;
// driver init
driver.init();
// link the driver with the current sense
current_sense.linkDriver(&driver);
// init current sense
current_sense.init();
// link the motor to the sensor
motor.linkSensor(&sensor);
// init driver
// link the motor to the driver
motor.linkDriver(&driver);
// link driver and the current sense
// link the motor to current sense
motor.linkCurrentSense(¤t_sense);
// set control loop type to be used
motor.controller = MotionControlType::velocity;
// initialize motor
motor.init();
// use monitoring with the BLDCMotor
Serial.begin(9600);
// monitoring port
motor.useMonitoring(Serial);
// init current sense
Serial.println("Hello world3!");
// define the motor id
command.add('A', onMotor, "motor");
// Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 2V."));
}
unsigned long timeStart;
unsigned long timeDiff;
void loop() {
/*val = 800; // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
Serial.println("Hello world2!");
delay(1000);
random_variable = random(0, 1000);
Serial.print("Variable_1:");
Serial.print(random_variable);
Serial.print(",");
Serial.print("Variable_2:");
Serial.println(static_variable);
*/
// FOC algorithm function
timeStart = micros();
motor.loopFOC();
timeDiff = micros()-timeStart;
//Serial.print("Timediff:");
// Serial.println(timeDiff);
// velocity control loop function
// setting the target velocity to 2rad/s
motor.move(2);
// monitoring function outputting motor variables to the serial terminal
//motor.monitor();
// user communication
command.run();
}