### Get Motor Current (Iq) - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Retrieves the motor's current Iq value in amperes, where Iq represents the current component that generates torque. A low-pass filter is used to smooth the data. The runFOC() function must be used in conjunction to update current sampling. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } int count = 0; void loop() { runFOC(); // Update sensor data (including current sampling) count++; if(count > 30) { count = 0; // Get M0 motor current float motor_current = DFOC_M0_Current(); Serial.printf("Motor current Iq: %.3f A\n", motor_current); } } ``` -------------------------------- ### Get Motor Angle - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Retrieves the current mechanical angle of the motor in radians, supporting multi-turn cumulative counting. The returned value is corrected for sensor direction. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { // Get current angle of M0 motor (radians) float current_angle = DFOC_M0_Angle(); // Convert to degrees for display float angle_degrees = current_angle * 180.0 / PI; Serial.printf("Current angle: %.2f rad (%.2f deg)\n", current_angle, angle_degrees); delay(100); } ``` -------------------------------- ### Get Motor Velocity - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Retrieves the current rotational speed of the motor in radians per second. It includes a built-in low-pass filter to automatically remove high-frequency noise. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { // Get current velocity of M0 motor (rad/s) float current_velocity = DFOC_M0_Velocity(); // Convert to revolutions per minute (RPM) float rpm = current_velocity * 60.0 / (2.0 * PI); Serial.printf("Current velocity: %.2f rad/s (%.2f RPM)\n", current_velocity, rpm); delay(50); } ``` -------------------------------- ### Initialize DengFOC System and PWM - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Initializes the DengFOC system, including setting the supply voltage and configuring all PWM channels, I2C bus, encoder, and current sensors. This is the first mandatory function to call when using the DengFOC library. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); // Initialize system, set supply voltage to 12.6V (3S Li-Po full charge voltage) DFOC_Vbus(12.6); // Output: "PWM initialization setup complete" // Output: "Encoder loaded successfully" } ``` -------------------------------- ### Implement Motor Force-Position Closed Loop Control Source: https://github.com/toantech/dengfoc_lib/blob/main/README.md This snippet demonstrates how to initialize the DengFOC library and configure a force-position closed-loop control system. It sets up the motor parameters, aligns the sensor, and defines PID controllers for both angle and current loops. ```cpp #include "DengFOC.h" int Sensor_DIR=-1; //传感器方向 int Motor_PP=7; //电机极对数 void setup() { Serial.begin(115200); DFOC_Vbus(12.6); //设定驱动器供电电压 DFOC_alignSensor(Motor_PP,Sensor_DIR); } void loop() { //设置PID DFOC_M0_SET_ANGLE_PID(0.5,0,0.003,100000,0.1); //角度PID DFOC_M0_SET_CURRENT_PID(1.25,50,0,100000); //电流环PID DFOC_M0_set_Force_Angle(serial_motor_target()); //力位控制指令 //接收串口 serialReceiveUserCommand(); } ``` -------------------------------- ### Implement Serial Command Interface Source: https://context7.com/toantech/dengfoc_lib/llms.txt Uses serial communication to receive real-time target values and control commands, facilitating easier debugging and dynamic parameter adjustment. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { runFOC(); float target = serial_motor_target(); DFOC_M0_set_Force_Angle(target); serialReceiveUserCommand(); } ``` -------------------------------- ### Implement Basic Open-Loop Torque Control Source: https://context7.com/toantech/dengfoc_lib/llms.txt Demonstrates the use of M0_setTorque to apply motor torque via SVPWM. This function serves as the foundation for all higher-level control modes by setting the q-axis voltage. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { float electrical_angle = S0_electricalAngle(); float Uq = 3.0; M0_setTorque(Uq, electrical_angle); } ``` -------------------------------- ### Configure Angle PID Controller - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Sets the PID parameters for the motor angle closed-loop control. P is the proportional gain, I is the integral gain, D is the derivative gain, ramp is the output slew rate limit, and limit is the output amplitude limit. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { // Set angle loop PID parameters // P=0.5, I=0, D=0.003, ramp=100000, limit=0.1 DFOC_M0_SET_ANGLE_PID(0.5, 0, 0.003, 100000, 0.1); // Use position closed-loop control to target angle float target_angle = 3.14; // Target angle (radians) DFOC_M0_set_Force_Angle(target_angle); serialReceiveUserCommand(); } ``` -------------------------------- ### Implement Inline Current Sensing Source: https://context7.com/toantech/dengfoc_lib/llms.txt The CurrSense class provides drivers for inline current sensors like the INA240. It supports three-phase current sampling and automatic bias calibration for accurate motor current monitoring. ```cpp #include "InlineCurrent.h" CurrSense current_sensor = CurrSense(0); void setup() { current_sensor.init(); } void loop() { current_sensor.getPhaseCurrents(); float Ia = current_sensor.current_a; float Ib = current_sensor.current_b; float Ic = current_sensor.current_c; } ``` -------------------------------- ### Implement Dual-Motor Synchronized Control Source: https://context7.com/toantech/dengfoc_lib/llms.txt Configures and controls two independent motors (M0 and M1) simultaneously, each with its own PID parameters and target inputs. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_enable(); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); DFOC_M1_alignSensor(7, -1); } void loop() { runFOC(); DFOC_M0_SET_ANGLE_PID(1, 0, 0, 100000, 30); DFOC_M0_set_Velocity_Angle(serial_motor_target()); DFOC_M1_SET_ANGLE_PID(1, 0, 0, 100000, 30); DFOC_M1_set_Velocity_Angle(serial_motor_target()); } ``` -------------------------------- ### Configure Velocity PID Controller - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Sets the PID parameters for the motor velocity closed-loop control. This is used for precise speed control, such as in constant speed or speed tracking applications. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { // Set velocity loop PID parameters // P=0.005, I=0, D=0, ramp=0, limit determined by supply voltage DFOC_M0_SET_VEL_PID(0.005, 0, 0, 0, 6.3); // Set target velocity (rad/s) float target_velocity = 10.0; DFOC_M0_setVelocity(target_velocity); serialReceiveUserCommand(); } ``` -------------------------------- ### Configure PID Controller for Motor Control Source: https://context7.com/toantech/dengfoc_lib/llms.txt The PIDController class implements a standard PID control loop with support for output ramp limiting and absolute output clamping. It is used for position, velocity, and current control loops within DengFOC. ```cpp #include "pid.h" // Create PID controller with P, I, D, ramp limit, and output limit PIDController position_pid = PIDController{ .P = 2.0, .I = 0.0, .D = 0.0, .ramp = 100000, // Max rate of change .limit = 10.0 // Max output value }; float error = target_position - current_position; float output = position_pid(error); ``` -------------------------------- ### Implement Three-Loop Cascaded Control Source: https://context7.com/toantech/dengfoc_lib/llms.txt Provides high-precision positioning using a cascaded structure: Position Loop (outer) -> Velocity Loop (middle) -> Current Loop (inner). ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_enable(); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { runFOC(); DFOC_M0_SET_ANGLE_PID(1, 0, 0, 100000, 30); DFOC_M0_SET_VEL_PID(0.02, 1, 0, 100000, 0.5); DFOC_M0_SET_CURRENT_PID(5, 200, 0, 100000); float target_position = serial_motor_target(); DFOC_M0_set_Velocity_Angle(target_position); } ``` -------------------------------- ### DFOC_M0_setVelocity Source: https://context7.com/toantech/dengfoc_lib/llms.txt Implements velocity closed-loop control using velocity PID and current inner loop. ```APIDOC ## [FUNCTION] DFOC_M0_setVelocity ### Description Controls motor speed using a cascaded PID structure (velocity outer loop, current inner loop). ### Parameters - **target_velocity** (float) - Required - Target speed in rad/s. ### Request Example ```cpp DFOC_M0_SET_VEL_PID(3, 2, 0, 100000, 0.5); DFOC_M0_setVelocity(target_velocity); ``` ``` -------------------------------- ### Configure Current PID Controller - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Sets the PID parameters for the motor current (torque) closed-loop control. The current loop is the innermost control loop, used for precise torque control. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { runFOC(); // Current loop requires runFOC() to update current values // Set current loop PID parameters // P=5, I=200, D=0, ramp=100000 DFOC_M0_SET_CURRENT_PID(5, 200, 0, 100000); // Set target current/torque float target_current = 0.5; // Target current (A) DFOC_M0_setTorque(target_current); serialReceiveUserCommand(); } ``` -------------------------------- ### Update Sensor Data with runFOC - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Updates all sensor data, including encoder angle/velocity and current sensor readings. This function must be called in the main loop when using current closed-loop control. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { // Update sensor data on each loop iteration runFOC(); // Now you can read the latest sensor values float angle = DFOC_M0_Angle(); float velocity = DFOC_M0_Velocity(); float current = DFOC_M0_Current(); Serial.printf("Angle:%.2f, Velocity:%.2f, Current:%.3f\n", angle, velocity, current); } ``` -------------------------------- ### Calibrate Motor Sensor - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Calibrates the motor's zero electrical angle, sets the motor pole pairs, and sensor direction. The calibration process applies a fixed torque to the motor to move it to a specific position and then reads the encoder angle as a zero reference. M0 and M1 correspond to the two motor channels. ```cpp #include "DengFOC.h" int Sensor_DIR = -1; // Sensor direction: 1 or -1, adjust based on actual wiring int Motor_PP = 7; // Motor pole pairs: check motor parameters or count magnetic poles divided by 2 void setup() { Serial.begin(115200); DFOC_Vbus(12.6); // Calibrate M0 motor sensor (single motor mode) DFOC_M0_alignSensor(Motor_PP, Sensor_DIR); // Output: "M0 electrical angle: x.xxxx" // If using dual motors, calibrate M1 as well // DFOC_M1_alignSensor(Motor_PP, Sensor_DIR); } ``` -------------------------------- ### Implement Current Torque Closed-Loop Control Source: https://context7.com/toantech/dengfoc_lib/llms.txt Utilizes a PID controller to track target current precisely. This method provides more stability and accuracy than open-loop voltage control. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { runFOC(); DFOC_M0_SET_CURRENT_PID(5, 200, 0, 100000); float target_current = serial_motor_target(); DFOC_M0_setTorque(target_current); } ``` -------------------------------- ### DFOC_M0_set_Force_Angle Source: https://context7.com/toantech/dengfoc_lib/llms.txt Implements position closed-loop control using angle PID and current inner loop. ```APIDOC ## [FUNCTION] DFOC_M0_set_Force_Angle ### Description Maintains motor position using an angle PID controller. ### Parameters - **target_angle** (float) - Required - Target position in radians. ### Request Example ```cpp DFOC_M0_SET_ANGLE_PID(0.5, 0, 0.003, 100000, 0.1); DFOC_M0_set_Force_Angle(target_angle); ``` ``` -------------------------------- ### Implement Velocity Closed-Loop Control Source: https://context7.com/toantech/dengfoc_lib/llms.txt Uses a velocity PID loop combined with an internal current loop to maintain constant speed or track velocity profiles. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { runFOC(); DFOC_M0_SET_VEL_PID(3, 2, 0, 100000, 0.5); DFOC_M0_SET_CURRENT_PID(0.5, 50, 0, 100000); float target_velocity = serial_motor_target(); DFOC_M0_setVelocity(target_velocity); } ``` -------------------------------- ### Implement Force-Angle Position Control Source: https://context7.com/toantech/dengfoc_lib/llms.txt Achieves precise position control by using an angle PID loop and an internal current loop to hold the motor at a specific target position. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void loop() { runFOC(); DFOC_M0_SET_ANGLE_PID(0.5, 0, 0.003, 100000, 0.1); DFOC_M0_SET_CURRENT_PID(1.25, 50, 0, 100000); float target_angle = serial_motor_target(); DFOC_M0_set_Force_Angle(target_angle); } ``` -------------------------------- ### Implement Low Pass Filter for Sensor Data Source: https://context7.com/toantech/dengfoc_lib/llms.txt The LowPassFilter class provides a first-order filter to smooth sensor inputs by reducing high-frequency noise. It uses a time constant (Tf) to balance between filtering strength and response latency. ```cpp #include "lowpass_filter.h" // Create a low-pass filter with a 10ms time constant LowPassFilter velocity_filter = LowPassFilter(0.01); float raw_velocity = 100.5; // Raw velocity data float filtered_velocity = velocity_filter(raw_velocity); // Filtered velocity output ``` -------------------------------- ### DFOC_M0_setTorque (Closed Loop) Source: https://context7.com/toantech/dengfoc_lib/llms.txt Performs current-based closed-loop torque control using PID regulation for stable output. ```APIDOC ## [FUNCTION] DFOC_M0_setTorque (Closed Loop) ### Description Uses a PID controller to track target current for precise torque output. ### Parameters - **target_current** (float) - Required - The target current in Amperes. ### Request Example ```cpp DFOC_M0_SET_CURRENT_PID(5, 200, 0, 100000); DFOC_M0_setTorque(target_current); ``` ``` -------------------------------- ### M0_setTorque / M1_setTorque Source: https://context7.com/toantech/dengfoc_lib/llms.txt Sets the motor torque using SVPWM algorithm based on q-axis voltage and electrical angle. ```APIDOC ## [FUNCTION] M0_setTorque / M1_setTorque ### Description Performs low-level torque control using the SVPWM algorithm to calculate three-phase PWM duty cycles. ### Parameters - **Uq** (float) - Required - q-axis voltage (range: -voltage_power_supply/2 to +voltage_power_supply/2) - **angle_el** (float) - Required - Current electrical angle ### Request Example ```cpp float Uq = 3.0; float electrical_angle = S0_electricalAngle(); M0_setTorque(Uq, electrical_angle); ``` ``` -------------------------------- ### DFOC_M0_set_Velocity_Angle Source: https://context7.com/toantech/dengfoc_lib/llms.txt Implements full three-loop cascaded control: Position -> Velocity -> Current. ```APIDOC ## [FUNCTION] DFOC_M0_set_Velocity_Angle ### Description Provides high-precision position control using a three-loop cascaded PID architecture. ### Parameters - **target_position** (float) - Required - Target position in radians. ### Request Example ```cpp DFOC_M0_SET_ANGLE_PID(1, 0, 0, 100000, 30); DFOC_M0_SET_VEL_PID(0.02, 1, 0, 100000, 0.5); DFOC_M0_SET_CURRENT_PID(5, 200, 0, 100000); DFOC_M0_set_Velocity_Angle(target_position); ``` ``` -------------------------------- ### Enable/Disable Motor Driver - Arduino/C++ Source: https://context7.com/toantech/dengfoc_lib/llms.txt Controls the motor driver's enable pin to turn the motor output on or off. This is useful for safety features or power-saving scenarios. ```cpp #include "DengFOC.h" void setup() { Serial.begin(115200); DFOC_enable(); // Enable motor driver DFOC_Vbus(12.6); DFOC_M0_alignSensor(7, -1); } void emergencyStop() { DFOC_disable(); // Emergency stop, disable motor output } ``` -------------------------------- ### Interface AS5600 Magnetic Encoder Source: https://context7.com/toantech/dengfoc_lib/llms.txt The Sensor_AS5600 class handles communication with AS5600 magnetic encoders via I2C. It provides methods to retrieve absolute mechanical angles, multi-turn cumulative angles, and angular velocity. ```cpp #include "AS5600.h" TwoWire i2c_bus = TwoWire(0); Sensor_AS5600 encoder = Sensor_AS5600(0); void setup() { i2c_bus.begin(19, 18, 400000UL); encoder.Sensor_init(&i2c_bus); } void loop() { encoder.Sensor_update(); float angle = encoder.getAngle(); float mech_angle = encoder.getMechanicalAngle(); float velocity = encoder.getVelocity(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.