zaguoba

gramophonEngine

Feb 23rd, 2026
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.60 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.  
  15. // LOOP TIME
  16. unsigned long time1, time2, loopt, period = 0;
  17.  
  18. const int speeed = 32;
  19. float target_velocity = 0.0;
  20.  
  21. // POT
  22. int potPin = A19; // pin 26
  23. int potVdd = 27;
  24. int potVal = 0;
  25.  
  26. // magnetic sensor instance - SPI
  27. MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, 5);
  28. // magnetic sensor instance - MagneticSensorI2C
  29. //MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
  30. // magnetic sensor instance - analog output
  31. // MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
  32.  
  33.  
  34. // BLDC motor & driver instance
  35. BLDCMotor motor = BLDCMotor(11);
  36. BLDCDriver3PWM driver = BLDCDriver3PWM(16, 17, 21, 22);
  37. // Stepper motor & driver instance
  38. //StepperMotor motor = StepperMotor(50);
  39. //StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6,  8);
  40.  
  41. // inline current sensor instance
  42. // ACS712-05B has the resolution of 0.185mV per Amp
  43. InlineCurrentSense current_sense = InlineCurrentSense(185.0f, A4, A5);
  44.  
  45. // commander interface
  46. Commander command = Commander(Serial);
  47. void onMotor(char* cmd){ command.motor(&motor, cmd); }
  48.  
  49. void setup() {
  50.  
  51.   //POT SETUP
  52.   pinMode(potVdd, OUTPUT); // voltage supply for potentiometer
  53.  
  54.   // use monitoring with serial
  55.   Serial.begin(115200);
  56.   // enable more verbose output for debugging
  57.   // comment out if not needed
  58.   SimpleFOCDebug::enable(&Serial);
  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::velocity;
  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;
  83.   motor.PID_velocity.I = 1;
  84.   motor.PID_velocity.D = 0;
  85.   // default voltage_power_supply
  86.   motor.voltage_limit = 24;
  87.  
  88.   // velocity low pass filtering time constant
  89.   motor.LPF_velocity.Tf = 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 = -3.49; //-6.98;
  110.  
  111.   // define the motor id
  112.   command.add('A', onMotor, "motor");
  113.  
  114.   // Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
  115.   Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 2V."));
  116.  
  117.   _delay(1000);
  118. }
  119.  
  120.  
  121. void loop() {
  122.   // loop time measure
  123. //  time1 = micros();
  124.  
  125.   // read the value from the pot:
  126.   digitalWrite(potVdd, HIGH); // 3.3V to the pot
  127.   potVal = analogRead(potPin);
  128.  
  129.   motor.target = -(3.49 + (potVal / 500.0));
  130.  
  131.   // iterative setting FOC phase voltage
  132.   motor.loopFOC();
  133.  
  134.   //target_velocity = -3.49 + (analogRead(speeed)-2048)/1024.0;
  135.   //Serial.println(target_velocity);
  136.  
  137.   // iterative function setting the outter loop target
  138.   // velocity, position or voltage
  139.   // if tatget 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.  
  153.  
  154.  
  155.   // user communication
  156.   command.run();
  157.  
  158.  
  159. //  time2 = micros();
  160. //  loopt = time2 - time1;
  161. //
  162.   if(period >= 1000)
  163.   {
  164. //    Serial.print("loop time: ");
  165. //    Serial.println(loopt);
  166.     Serial.println(sensor.getAngle());  
  167.     period = 0;
  168.   }
  169.   else
  170.     period++;
  171.  
  172.  
  173. }
  174.  
  175.  
Advertisement
Add Comment
Please, Sign In to add comment