Shop OBEX P1 Docs P2 Docs Learn Events
How do I use complex PUB's in newcog — Parallax Forums

How do I use complex PUB's in newcog

EXPO5EXPO5 Posts: 2
edited 2011-05-31 08:23 in Propeller 1
I'm using the folowing code to control 4 motors using 3 sensors.
The code is already working, when only using 1 cog. the problem with this is, that, while prefroming an evasive manouvre, I can't get any sensordata. So using a cog to read the sensors simultaniously, should solve my problem, but I can't get it working.

can anybody help me with this problem?
pub Start  
Init  
  repeat      
    cognew(ReadSensors, @Sensorstack)
    cognew(WritePST, @PSTstack)
 
      if data1>100         
        evaderight            
      else
         goBack
 
     if data2>100           
        evadeleft         
     else
        goback
     if data3>100         
        gostraight         
     else
        goback
 
PUB writePST
     PST.dec(data1)
     PST.tab
     PST.dec(data2)
     PST.tab
     PST.dec(data3)
     PST.Newline
 
PUB ReadSensors
AcquireValue1
AcquireValue2
AcquireValue3
data:=AcquireValue1
data2:=AcquireValue2
data3:=AcquireValue3
 
PUB goStraight
QiK.SetSpeedM0(10,20)
QiK.SetSpeedM1(10,-20)
QiK2.SetSpeedM0(10,-20)
QiK2.SetSpeedM1(10,20)

Comments

  • RsadeikaRsadeika Posts: 3,837
    edited 2011-05-27 05:16
    First off, you should really show your complete program! The most obvious problem that I see is that you are declaring a cognew within the repeat statement. I would put the cognew right after the:

    PUB Start
    cognew...
    cognew...

    repeat
  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-05-27 05:21
    Variables are local to objects (so to speak), so I think your issue is that your trying to access the variables of the "ReadSensors" object from your "Start" object.

    I had a similar concern when I wrote my object for the 74C922 Keypad object.

    So instead of ;
    data1 := AcquireValue1
    you should use;
    byte[@data1] := AcquireValue1

    and in your Start method use;

    if byte[@data1] > 100
    evaderight

    etc

    Reading/Writing Bytes of Main Memory (Syntax 3) -- page 53 of the manual.

    (Also just in case you copied your methods straight from the Prop Tool -- you have data1 in the top method, and just data in the bottom method -- TYPO?)
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-05-27 07:07
    Reading sensors and driving motors are separate processes. I would suggest you create a motor driver cog, a sensor cog, and then knit them together with your main code. This slave coprocessor approach is easy to implement and part of the fun of using the Propeller.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-05-27 07:29
    EXPO5,

    Your code should look something like what I show below. I'm assuming that all the routines are in the same file (or object) so you can access data1, data2 and data3 directly. BTW, I changed "data" to "data1" in ReadSensors. I think that's what you intended. The cognew's should only be called once, so I moved them out of the repeat loop. I added a repeat loop to the two routines ReadSensors and writePST.

    Note that your three cogs are all running repeat loops independent of each other, so there is no synchronization among them. You may want to use waitcnt to make them run at the same loop interval, or better yet, use a variable that is set and cleared by one of the cogs to make the other two cogs wait. That way, only your main cog needs to use waitcnt and the other cogs will be synchronized with it.

    I don't know what your AcquireValue routines look like, so I don't know if you need to call them twice per loop. I suspect you only need to call them once per loop when setting the data value.

    It would help if you posted all your code. You can attach the files to your post by clicking on the "Go Advanced" link.

    Dave
    var
      long data1
      long data2
      long data3
      long Sensorstack[20]
      long PSTstack[20]
    
    pub Start  
      Init  
      cognew(ReadSensors, @Sensorstack)
      cognew(WritePST, @PSTstack)
      
      repeat
          if data1>100         
            evaderight            
          else
             goBack
     
         if data2>100           
            evadeleft         
         else
            goback
         if data3>100         
            gostraight         
         else
            goback
     
    PUB writePST
      repeat
         PST.dec(data1)
         PST.tab
         PST.dec(data2)
         PST.tab
         PST.dec(data3)
         PST.Newline
     
    PUB ReadSensors
      repeat
        AcquireValue1
        AcquireValue2
        AcquireValue3
        data1:=AcquireValue1
        data2:=AcquireValue2
        data3:=AcquireValue3
     
    PUB goStraight
      QiK.SetSpeedM0(10,20)
      QiK.SetSpeedM1(10,-20)
      QiK2.SetSpeedM0(10,-20)
      QiK2.SetSpeedM1(10,20)
    
  • EXPO5EXPO5 Posts: 2
    edited 2011-05-31 01:19
    data was indeed a typo and should be data1.

    I'm also trying something with a variable infront of cognew, but it's not yet in this version of the code.
    When I write cognew outside of a repeatloop, it seems to be doing even less.
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-05-31 07:37
    if the command cognew is in a repeat loop you run out of cogs in the fourth loop.
    Stating that cognew outside of the loop is doing even less makes a buggy code surely not working

    To say it really clear
    your use of cognew is like this

    you want to drill a whole into a piece of wood taking a drilling machine into your hand
    now shortly after start drilling the whole with the first drilling machine you take a second drilling machine
    and want to drill into the EXACTLY same whole as the first drilling machine is working on

    That's just something that MUST go wrong under all circumstances.

    So the bugs must be somewhere else.

    As you haven't provided much information about what you are trying to do. Please post a description of how your project works.
    I'm willing to help but I won't analyse your code line by line until I know what your code does
    best way to do this is to comment your code what all important things.

    And to have an easier comparison attach the code that already works for one motor.
    Please use the archive-function of the propellertool for this. It is much easier as you only create ONE zip-file containing ALL *.spin-files at once and the propellertool adds hierarchy-information
    how the spin-files depend from each other.

    Please give a detailed description of what do you expect the motors doing and what the do instead.

    best regards

    Stefan
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-05-31 08:23
    EXPO5, I pointed out the problem with your code, and posted a solution to the problem. It seems you ignored my suggestion, which means your code won't work until you implement my suggestion. It appears that you don't understand how cognew works. I would suggest that you carefully read the Prop manual, which has a good description of cognew. You should then try a few simple examples that use cognew so that you understand it better.
Sign In or Register to comment.