float variable to pwm

Status
Not open for further replies.
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.

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(); 
 
}
 
To output PWM, you may not use digitalWrite() but analogWrite() on a PWM capable pin. Before, you'll have to set analogWriteResolution() once. If you set it to 10 bits, for example, you might use values from 0 to 1023 for analogWrite(). Scaling your Torqueamp value by multiplying with a constant to obtain integer values from 0 to 1023 shouldn't be too difficult.

RTFM: https://www.pjrc.com/teensy/td_pulse.html
 
Thank you for your feedback. As you can see below I set my resolution to 16 bit =0- 65535 and the Torqueamp to signed int variable so I guess I will get values -32768 0 +65536. I have set my motor drive such as that it outputs 0 volt at 50% of the duty cycle so when my duty cycle is less the motor rotates clockwise and anticlockwise if duty cycle>50%. My Torqueamp variable can be either be positive or negative.
When Torqueamp is negative I would like to set the duty cycle above 50% (of course after multiplying with a constant to get a variable between 32768-65536) and vise versa. I am a newbie so I do not know this can be implemented software wise? am I missing something?:rolleyes:



[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 36.9 mNm/A = 0.0369 Nm/A
const int Tdac=1;
float Torque;
signed int Torqueamp;
//calculation of torque
Torque=Kp*(handlebarangle-forkangle);
Torqueamp=(Torque*Tc); // divide with reduction ratio* torque constant to give a command in Amperes
// Scale torque amp with a constant to obtain a dac number

analogWriteResolution(16);
analogWrite(pwmforkmotor,32768); I will change 32768 with Torque amp value
digitalWrite(forkmotordrive,HIGH);
Serial.print(",Torque(Nm)=");
Serial.print(Torque);
Serial.println();
][/CODE]
 
Before moving on, you should give a little more info about your motor driver circuit. Negative torque values mean probably reverse motor turning? In that case, most motor driver ICs need a separate direction signal because PWM can only output positive values.
 
Thank you for your answer. I solved the problem with the motor rotation, I will post all of the details when I will finish the project.
I would like to also know how I can differentiate the angular position to get acceleration
. I tried to estimate dt time using millis and micros functions but I think the time of 352 μs is wrong.As a first step I would like to know the time the handlebarangle and forkangle values are updated. Is what I am doing BELOW correct? (It seems that the magnitude of dv is too high if I assume that dt=352 us which is not what happens in reality)>
Code:
uint32_t ts1 = micros();

.................
float dv;
float dt;
dv=(handlebarangle-forkangle)/0.000352; // where dt=352 (us)
uint32_t ts2 = millis();
dt=ts2-ts1;
 
Status
Not open for further replies.
Back
Top