It is clear to me now that GPIO in Raspberry pi and softmotion tool in Codesys do not work simultanously. Actually, I do not understand why such a problem exist since its important to read data through GPIO from sensors to be able to control a step or servo motor or even other purposes that you need in your Automation project!. I hope in the next version of Raspberry pi or Codesys this problem to be solved somehow.
Any way, as you saw in the video, I do not have a license for softmotion toolbox in codesys which cost me around 130 euro beside the cost of a single license for raspberry pi which is around 50 Euro. I decided to write a PWM block in structured text in order to create pulses with specific Frequency and duty cycle. This PWM block works like a fast timer that makes step signals in ms and then reset itself for another cycle. but unfortunately, the motor do not function as i want. below take a look at the code.
The PWM timer block
//start the timer
Timer(IN := GVL.Run_pwm2, PT:= T#50MS);
IF Timer.IN > Timer.Q THEN
IoConfig_Globals_Mapping.stepmotor_enable:= TRUE;
IoConfig_Globals_Mapping.stepmotor_dir:=TRUE;
ELSE
IoConfig_Globals_Mapping.stepmotor_enable:= FALSE;
IoConfig_Globals_Mapping.stepmotor_dir:=TRUE;
END_IF;
// Check if the timer is done
IF Timer.Q THEN
Timer(IN := FALSE, PT:= T#50MS); // Reset the timer
IoConfig_Globals_Mapping.stepmotor_step := NOT IoConfig_Globals_Mapping.stepmotor_step; // Toggle the PWM output
IoConfig_Globals_Mapping.stepmotor_enable:= FALSE;
IoConfig_Globals_Mapping.stepmotor_dir:=TRUE;
// Calculate the on-time (in milliseconds) based on duty cycle
// Check if the PWM output should be turned off
END_IF;
What is the result of this PWM block is not satisfactory, below you can see a video of the motor:
I do not know why the bahaviour of motor with this PWM block is so jittery. In the next episode, I would use the Adafruit step motor HAT and compare the result. If you knowhow i can improve this code, Please leave your comment below 🙂

Donnation!
Please support me by buying a coffee for new test and exploration 🙂
$3.00





Leave a comment