1 Reply Latest reply on Sep 13, 2017 5:02 AM by JobinT_31

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

      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;  
                  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