Stepper Motor Acceleration and Non-Blocking Step Count Update - PSoC5

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi all,

Working on a 2 wheel drive robot that requires a non-blocking form of code for the driving function of two stepper motors while monitoring other sensors. The drive function is currently achieved through the design schematic below, copied for both wheels. The code to drive the robot a certain number of cm is as follows, using global parameters CIRCUMFERENCE from the drive wheel circumference and the STEP_PER_REV from the number of microsteps required for a full revolution, currently set to 400 for a half stepped motor. The WheelDir pins (not shown in hardware screenshot) are connected to the direction pins for the stepper motor driver and are driven high for forward movement and low for reverse.

void DriveCM(int Dir, int Distance) {

           

            float NumSteps = Distance/CIRCUMFERENCE*STEPS_PER_REV;

            int NumSteps_Convert = (int) NumSteps;

            WheelDir_Right_Write(Dir);

            WheelDir_Left_Write(Dir);

           

            WheelTimer_Right_WritePeriod(NumSteps_Convert);

            WheelTimer_Left_WritePeriod(NumSteps_Convert);

           

            WheelEnable_Right_Write(1);

            WheelEnable_Left_Write(1);

            while(WheelTimer_Right_ReadCounter() && WheelTimer_Left_ReadCounter() != 0) // prevent overwriting

             {}

        } // end DriveCM

Screen Shot 2017-09-13 at 2.48.35 am.png

This post is split into 2 parts. Firstly, I'd like to ask about making the code non-blocking in a way that requests for a certain distance to be driven wouldn't be processed until the original distance has already been travelled. This is done in code right now through lines 15 & 16, but doesn't allow for any other function to run while the wheels are moving. If the function is called currently, lines 15 & 16 have to finish up before they any new requests are processed. However, if these lines are removed, the distance and direction can be updated at any point and can overwrite any previous requests. The requirement is for the requested movements to finish up completely before any new requests are processed, without being block by software inside the function. Is there a way to configure this in hardware?

Secondly, there seems to be a certain amount of missed steps due to the motors running up to full speed at every start and stop. Is there a way to incorporate acceleration into the hardware? I was considering 3 different clocks for "slow", "medium" and "fast" speeds, with a mux that determines which one feeds through to the design. This mux would be controlled by a counter that checks, for example, the first 200 steps, then increments its value for the mux to activate the next clock. The proposition to use 3 different clock was due to the fact that previous attempts at changing the Clock_1_SetDividerValue divider value in the main.c file resulted in some odd behaviour of the steppers and no proper movement. The mux and counter idea would take care of the acceleration up to speed, but what can I use for the deceleration in a similar manner?

Many thanks

0 Likes
1 Solution
JobinT_31
Employee
Employee
50 solutions authored 25 solutions authored 10 solutions authored

Hi,

1)  Instead of putting a while loop you can connect an ISR to the timer. To ignore any attempt to overwrite the timer. you can just have flag.

You will check the flag before writing a new timer value (and set the flag). Then clear the flag in the Timer ISR

2) Instead of using clock you can try using the PWM.

Jobin GT

View solution in original post

0 Likes
1 Reply
JobinT_31
Employee
Employee
50 solutions authored 25 solutions authored 10 solutions authored

Hi,

1)  Instead of putting a while loop you can connect an ISR to the timer. To ignore any attempt to overwrite the timer. you can just have flag.

You will check the flag before writing a new timer value (and set the flag). Then clear the flag in the Timer ISR

2) Instead of using clock you can try using the PWM.

Jobin GT

0 Likes