// Configure BLDC motor with incremental encoderdriver.motorConfig.setType(tmc9660::tmcl::MotorType::BLDC_MOTOR,7);driver.feedbackSense.configureABNEncoder(2048);// 2048 counts/revdriver.motorConfig.setCommutationMode(tmc9660::tmcl::CommutationMode::FOC_ENCODER);// Set target velocitydriver.focControl.setTargetVelocity(1500);
Stepper Motor Control
1
2
3
4
5
6
7
8
// Configure stepper motordriver.motorConfig.setType(tmc9660::tmcl::MotorType::STEPPER_MOTOR);driver.feedbackSense.configureABNEncoder(4000);driver.motorConfig.setCommutationMode(tmc9660::tmcl::CommutationMode::FOC_ENCODER);// Move to positiondriver.position.moveTo(1000);
Step/Dir Interface
1
2
3
4
// Configure stepper for step/direction interfacedriver.motorConfig.setType(tmc9660::tmcl::MotorType::STEPPER_MOTOR);driver.stepDir.setMicrosteps(8);// 1/8 microsteppingdriver.stepDir.enable();
Telemetry Reading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Read motor temperatureint16_ttemperature;if(driver.telemetry.getTemperature(temperature)){printf("Temperature: %d°C\n",temperature);}// Read motor currentint16_tcurrent;if(driver.telemetry.getCurrent(current)){printf("Current: %d mA\n",current);}// Read positionint32_tposition;if(driver.telemetry.getPosition(position)){printf("Position: %ld\n",position);}
Error Handling
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
boolconfigureMotor(tmc9660::TMC9660&driver){// Set motor typeif(!driver.motorConfig.setType(tmc9660::tmcl::MotorType::BLDC_MOTOR,7)){printf("Failed to set motor type\n");returnfalse;}// Set current limitsif(!driver.motorConfig.setMaxTorqueCurrent(2000)){printf("Failed to set torque current\n");returnfalse;}returntrue;}
boolconfigureBLDCMotor(tmc9660::TMC9660&driver){// Step 1: Set motor type and pole pairsif(!driver.motorConfig.setType(tmc9660::tmcl::MotorType::BLDC_MOTOR,7)){returnfalse;}// Step 2: Configure current limits (SAFETY FIRST!)if(!driver.motorConfig.setMaxTorqueCurrent(2000)){// 2A maxreturnfalse;}if(!driver.motorConfig.setMaxFluxCurrent(1000)){// 1A fluxreturnfalse;}// Step 3: Configure feedback sensorsif(!driver.feedbackSense.configureHall()){returnfalse;}// Step 4: Set PWM frequency and switching schemeif(!driver.motorConfig.setPWMFrequency(25000)){// 25 kHzreturnfalse;}if(!driver.motorConfig.setPWMSwitchingScheme(tmc9660::tmcl::PwmSwitchingScheme::SVPWM)){returnfalse;}// Step 5: Configure FOC control gainsif(!driver.focControl.setCurrentLoopGains(50,100)){returnfalse;}if(!driver.focControl.setVelocityLoopGains(800,1)){returnfalse;}// Step 6: Enable commutation (LAST STEP!)if(!driver.motorConfig.setCommutationMode(tmc9660::tmcl::CommutationMode::FOC_HALL)){returnfalse;}returntrue;}
boolconfigureStepperMotor(tmc9660::TMC9660&driver){// Step 1: Set motor type (steppers use 1 pole pair)if(!driver.motorConfig.setType(tmc9660::tmcl::MotorType::STEPPER_MOTOR,1)){returnfalse;}// Step 2: Configure encoder for feedbackif(!driver.feedbackSense.configureABNEncoder(1024)){// 1024 CPRreturnfalse;}// Step 3: Set current limitsif(!driver.motorConfig.setMaxTorqueCurrent(1500)){// 1.5Areturnfalse;}// Step 4: Configure for FOC with encoder feedbackif(!driver.motorConfig.setCommutationMode(tmc9660::tmcl::CommutationMode::FOC_ENCODER)){returnfalse;}returntrue;}
Velocity Control
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
boolsetVelocityControl(tmc9660::TMC9660&driver,int32_ttarget_rpm){// Configure velocity sensorif(!driver.focControl.setVelocitySensor(tmc9660::tmcl::VelocitySensorSelection::ABN1_ENCODER)){returnfalse;}// Set velocity PI gains (tune these for your system)if(!driver.focControl.setVelocityLoopGains(800,1)){returnfalse;}// Command target velocityif(!driver.focControl.setTargetVelocity(target_rpm)){returnfalse;}returntrue;}
boolsetPositionControl(tmc9660::TMC9660&driver,int32_ttarget_position){// Configure position sensorif(!driver.focControl.setPositionSensor(tmc9660::tmcl::PositionSensorSelection::ABN1_ENCODER)){returnfalse;}// Set position PI gainsif(!driver.focControl.setPositionLoopGains(100,0)){// P=100, I=0returnfalse;}// Set position limits for safetyif(!driver.focControl.setPositionLimitLow(-100000)){returnfalse;}if(!driver.focControl.setPositionLimitHigh(100000)){returnfalse;}// Command target positionif(!driver.focControl.setTargetPosition(target_position)){returnfalse;}returntrue;}