George1988
Member
Hello, for a university project I am trying to command a motor using torque control. To do so I read two absolute encoder and estimate the torque based on a simple equation (Torque= Kp*(handlebarangle-forkangle).
I am getting the estimated torque as a float variable and I need to transform it to pwm to give it as an input to the motor drive.
I am getting the estimated torque as a float variable and I need to transform it to pwm to give it as an input to the motor drive.
Is it possible to transform a float variable to pwm?
Code:
//declare pin alocation on teensie 3.6 board
const int forkmotordrive = 30; // making High enables fork motor drive
const int pwmforkmotor = 9;
//declare pin as outputs
pinMode (forkmotordrive, OUTPUT);
pinMode (pwmforkmotor, OUTPUT);
// define variables
float Kp=0.139626 ;
float Kd=0.010472;//
float Tc= 0.0369;// Torque constant
float Torqueamp;
//calculation of torque
Torque= Kp*(handlebarangle-forkangle);
Torqueamp=(Torque*Tc); // divide with reduction ratio* torque constant to give a command in Amperes
digitalWrite(forkmotordrive,HIGH);
digitalWrite(pwmforkmotor,Torqueamp); // THIS ONLY SETS PIN TO HIGH OR LOW I WOULD LIKE TO GET PWM TO COMMAND MY MOTOR?
Serial.print(Torqueamp);
Serial.println();
}