zaguoba

gramophonEngine_noCurrent

Feb 25th, 2026 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.82 KB | Source Code | 0 0
  1. /**
  2.  * Comprehensive BLDC motor control example using magnetic sensor
  3.  *
  4.  * Using serial terminal user can send motor commands and configure the motor and FOC in real-time:
  5.  * - configure PID controller constants
  6.  * - change motion control loops
  7.  * - monitor motor variabels
  8.  * - set target values
  9.  * - check all the configuration values
  10.  *
  11.  * See more info in docs.simplefoc.com/commander_interface
  12.  */
  13. #include <SimpleFOC.h>
  14. #include "SimpleFOCDrivers.h"
  15. #include "encoders/as5048a/MagneticSensorAS5048A.h"
  16.  
  17. // LOOP TIME
  18. unsigned long time1, time2, loopt, period = 0;
  19.  
  20. float target_velocity = 0.0;
  21.  
  22. // magnetic sensor instance - SPI
  23. MagneticSensorAS5048A sensor(5);
  24. //MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, 5);
  25. // magnetic sensor instance - MagneticSensorI2C
  26. //MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
  27. // magnetic sensor instance - analog output
  28. // MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
  29.  
  30.  
  31. // BLDC motor & driver instance
  32. BLDCMotor motor = BLDCMotor(11);
  33. BLDCDriver3PWM driver = BLDCDriver3PWM(16, 17, 21, 22);
  34. // Stepper motor & driver instance
  35. //StepperMotor motor = StepperMotor(50);
  36. //StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6,  8);
  37.  
  38. // inline current sensor instance
  39. // ACS712-05B has the resolution of 0.185mV per Amp
  40. //InlineCurrentSense current_sense = InlineCurrentSense(185.0f, A4, A5);
  41.  
  42. // commander interface
  43. Commander command = Commander(Serial);
  44. void onMotor(char* cmd){ command.motor(&motor, cmd); }
  45.  
  46. void setup() {
  47.  
  48.   //POT SETUP
  49.   pinMode(potVdd, OUTPUT); // voltage supply for potentiometer
  50.  
  51.   // use monitoring with serial
  52.   Serial.begin(115200);
  53.   // enable more verbose output for debugging
  54.   // comment out if not needed
  55.   SimpleFOCDebug::enable(&Serial);
  56.  
  57.   sensor.min_elapsed_time=0.0001; // the default is 0.0001s;
  58.  
  59.  
  60.   // initialise magnetic sensor hardware
  61.   sensor.init();
  62.   // link the motor to the sensor
  63.   motor.linkSensor(&sensor);
  64.  
  65.   // driver config
  66.   // power supply voltage [V]
  67.   driver.voltage_power_supply = 24;
  68.   driver.init();
  69.   // link driver
  70.   motor.linkDriver(&driver);
  71.   // link current sense and the driver
  72.   //current_sense.linkDriver(&driver);
  73.  
  74.   // choose FOC modulation
  75.   //motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
  76.  
  77.   // set control loop type to be used
  78.   motor.controller = MotionControlType::angle;
  79.   //motor.torque_controller = TorqueControlType::foc_current; //foc_current;
  80.  
  81.   // contoller configuration based on the control type
  82.   motor.PID_velocity.P = 0.8f; //0.8f;//0.05f;
  83.   motor.PID_velocity.I = 5; //5 //1;
  84.   motor.PID_velocity.D = 0; //0.001; //0;
  85.   // default voltage_power_supply
  86.   motor.voltage_limit = 12;
  87.  
  88.   // velocity low pass filtering time constant
  89.   motor.LPF_velocity.Tf = 0.03f; //0.03f;
  90.  
  91.   // angle loop controller
  92.   motor.P_angle.P = 20;
  93.   // angle loop velocity limit
  94.   motor.velocity_limit = 50;
  95.  
  96.   // comment out if not needed
  97.   motor.useMonitoring(Serial);
  98.  
  99.   // current sense init and linking
  100.   //current_sense.init();
  101.   //motor.linkCurrentSense(&current_sense);
  102.  
  103.   // initialise motor
  104.   motor.init();
  105.   // align encoder and start FOC
  106.   motor.initFOC();
  107.  
  108.   // set the inital target value
  109.   motor.target = -10000; //-6.98;
  110.   motor.velocity_limit = 3.49f;
  111.   //motor.feed_forward_velocity = 3.5f;
  112.  
  113.   // define the motor id
  114.   command.add('A', onMotor, "motor");
  115.  
  116.   // Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
  117.   Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 2V."));
  118.  
  119.   _delay(1000);
  120. }
  121.  
  122.  
  123. void loop() {
  124.   // loop time measure
  125. //  time1 = micros();
  126.  
  127.   // read the value from the pot:
  128.   //digitalWrite(potVdd, HIGH); // 3.3V to the pot
  129.   //potVal = analogRead(potPin);
  130.  
  131.   //motor.target = -(3.49 + (potVal / 500.0));
  132.  
  133.   // iterative setting FOC phase voltage
  134.   motor.loopFOC();
  135.  
  136.  
  137.   // iterative function setting the outter loop target
  138.   // velocity, position or voltage
  139.   // if target not set in parameter uses motor.target variable
  140.   //motor.move(target_velocity);
  141.   motor.move();
  142.  
  143.   //PhaseCurrent_s currents = current_sense.getPhaseCurrents();
  144.   //float current_magnitude = current_sense.getDCCurrent();
  145.  
  146.   //Serial.print(currents.a*1000); // milli Amps
  147.   //Serial.print("\t");
  148.   //Serial.print(currents.b*1000); // milli Amps
  149.   //Serial.print("\t");
  150.   //Serial.println(current_magnitude*1000); // milli Amps
  151.  
  152.   //Serial.print("sensor min elapsed time: ");
  153.   // Serial.println(sensor.min_elapsed_time);
  154.  
  155.  
  156.   // user communication
  157.   command.run();
  158.  
  159.  
  160. //  time2 = micros();
  161. //  loopt = time2 - time1;
  162. //
  163. //  if(period >= 2000)
  164. //  {
  165. //    //Serial.print("POSITION: ");
  166. //    //Serial.println(loopt);
  167. //    Serial.println(sensor.getAngle());  
  168. //    period = 0;
  169. //  }
  170. // else
  171. //   period++;
  172.  
  173.  
  174. }
  175.  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment