Parallel Task Problem

Data B4

New Member
So I wrote a program to run a stepper motor. If I run the motor from the same Task where I am processing the angles and what not the motor runs slow and I cant get it to the right RPM (I assume because of the lines between the motor steps). My solution is running the motor in a Parallel Task which works great but I found a new problem: As soon as I use the label Start1: in the fist task the program runs through the program lines twice between all the labels ( Main:, STP1:, STP2:, .....) so I'm counting into B2 and simulating the program it does all lines in first task twice so on one of lines I have "Let B2=B2+1" is counts by multiples of 2 and when I want it to stop at an odd number with an "IF Then" command it does not work. I discovered this problem by running the program and when my "ReadADC" equals 255 the program gets stuck in an endless loop due to rollover and the angle I select the stepper motor to do (via "ReadADC") is half of what it is supposed to be. When I get rid of the Parallel task and change the "Start1:" to "Start:" the program works proper. I noticed I had trouble with Parallel Tasks before and I assume I had the same prob. I attached the program for others to see. If you debug when running or if you view the Ram during simulation you will notice the issue where B2 counts twice before the next line. Is there a way around this issue? I'm running editor 6.1.0.0. and using an 14M2 chip

Thanks much
 

Attachments

Last edited:

Data B4

New Member
Found my problem: You need to start with "Start0:" not with "Start1:" Like I did. So parallel task 1 is "Start0: and Parallel Task 2 is "Start1:" once I changed that the Program behaved the way it is supposed to.
 

AllyCat

Senior Member
Hi,

You're not using any "SubProcedures" (Subroutines) you're using Multi-tasking. IMHO that's a very inappropriate use of Multitasking and is forcing the execution speed to an undetermined 16 / 4 MHz (much better done with SETFREQs if needed). There doesn't seem any point in updating/outputting the "PinsB" value except when B0 is actually changed.

BTW, personally, I would much prefer (and you might get more responses) if you list programs of that size "in-line" (between [ code] [/code] tags) so that I don't have to clutter up my hard drive with programs that I'll never need to look at again, and run another instance of PE, etc..
Code:
;    Stepper motor driver
InSet:
    let dirsB = %00111100
Start1:
    Let B2=0
    ReadADC C.4,B1
    If PinC.0 = 1 Then Goto Btn1
    Goto Start1
    Btn1:
        If PinC.0=1 Then Goto Btn1
        Goto CW
    CW:
        If B0=40 Then Goto STP2
        If B0=36 Then Goto STP3
        If B0=20 Then Goto STP4
        STP1:
            Let B0=40
            Let B2=B2+1
            If B2=>B1 Then Goto Start1
        STP2:
            Let B0=36
            Let B2=B2+1
            If B2=>B1 Then Goto Start1
        STP3:
            Let B0=20
            Let B2=B2+1
            If B2=>B1 Then Goto Start1
        STP4:
            Let B0=24
            Let B2=B2+1
            If B2=>B1 Then Goto Start1
            Goto STP1
Start2:
    Let PinsB=B0
    Goto Start2
Cheers, Alan.

PS: For those who hate GOTOs or repetitive code, I think the equivalent flow follows, but I'm not sure of the intended functions of the pin or ADC inputs.
Code:
;  Stepper motor driver
symbol DELAY = 10    ; To set the speed (or use a higher setfreq if required)
Init:
    dirsB = %00111100
do  :  B2 = 0    
  do  :  ReadADC C.4 , B1
  loop while PinC.0 <> 1
  do : loop while PinC.0 = 1
CW:   :  B0 = 0
  do    
    lookup B0,(40,36,20,24),pinsB
    B0 = B0 + 1 AND 3
    inc B2
    if B2 => B1 then exit
    pause DELAY
  loop    
loop
 
Last edited:

Data B4

New Member
Thanks much for the info and sorry I used the wrong terms. I learned a lot from looking at your suggestion of programming and will make my life easier in the future.

Thanks
 

AllyCat

Senior Member
Hi,

Thanks for your reply. Actually, your use of GOTOs wasn't particularly "bad", but the "list" sequence of IF .. THENs .. will cause the output pulses to have somewhat different widths. However, the sometimes "preferred" SELECT .. CASE structure has the same issue, probably worse! An advantage of the LOOKUP method is that it's faster and (perhaps surprisingly) the pulses should be of exactly equal width.

Also, you may have noticed that the Lookup data is extremely easy to change for Anti-Clockwise operation, or even more advanced driving methods such as 8 phases per revolution.

Cheers, Alan.
 
Top