Advertisement
microrobotics

Makerbase Gimbal Motor 2804 with an AS5600 magnetic encoder. The SimpleFOC library provides an easy

Jul 24th, 2023
2,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Makerbase Gimbal Motor 2804 with an AS5600 magnetic encoder. The SimpleFOC library provides an easy way to control BLDC motors using Arduino.
  3.  
  4. Please note, you need to install the SimpleFOC library first. You can do that by going to Sketch > Include Library > Manage Libraries in the Arduino IDE, then search for SimpleFOC and install it.
  5.  
  6. The AS5600 communicates over the I2C protocol. Here is a sample code to run a gimbal motor using SimpleFOC:
  7. In this example, a gimbal motor is linked to a magnetic sensor (AS5600). The voltage limit is set to 3 volts, and the motor will try to reach the target position provided via the Serial interface. You set a target position by sending a command like "T1.0" over Serial.
  8.  
  9. Remember to connect the sensor and driver pins correctly, and power the motor with an appropriate power source.
  10.  
  11. Disclaimer: The code might not work as expected if the encoder, motor, and driver are not connected and configured correctly. Always make sure to follow the guidelines provided in the datasheet/manual of your specific devices.
  12. */
  13.  
  14. #include <SimpleFOC.h>
  15.  
  16. // Magnetic sensor instance - AS5600
  17. MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
  18.  
  19. // BLDCMotor instance
  20. BLDCMotor motor = BLDCMotor(11);
  21.  
  22. // BLDCDriver instance
  23. BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11, 8);
  24.  
  25. void setup() {
  26.  
  27.   // Initialise magnetic sensor hardware
  28.   sensor.init();
  29.  
  30.   // Link the motor to the sensor
  31.   motor.linkSensor(&sensor);
  32.  
  33.   // Choose FOC modulation (optional)
  34.   motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
  35.  
  36.   // Initialize motor
  37.   motor.init();
  38.  
  39.   // Link the motor to the driver
  40.   motor.linkDriver(&driver);
  41.  
  42.   // Set voltage limit
  43.   motor.voltage_limit = 3.0;
  44.  
  45.   // Use monitoring with the following function
  46.   Serial.begin(115200);
  47.  
  48.   // Initialize motor
  49.   motor.initFOC();
  50.  
  51.   // Add target command T
  52.   SerialCommand cmdT = SerialCommand("T", [](Cmd* cmd)-> void {
  53.     motor.target = cmd->getArg(0);
  54.   });
  55.   SerialManager.add(cmdT);
  56. }
  57.  
  58. void loop() {
  59.   // FOC algorithm function
  60.   motor.loopFOC();
  61.  
  62.   // Monitoring function
  63.   motor.monitor();
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement