Shop OBEX P1 Docs P2 Docs Learn Events
cognew issue — Parallax Forums

cognew issue

lenny1337lenny1337 Posts: 26
edited 2008-02-23 22:47 in Propeller 1
Hi,

relative novice here and I just started programming the robot once again, and i'm having issue with two processes i want to run parallel. the lines of code i have are the following (in an object called movement):

·· cognew(FrontPing.go(5, 650, 2050), @Stack)··············································
·· servo.move_to(1,(1607 + SpeedmodeL[noparse][[/noparse]0]),20)············· ' specific for left motor configuration 1607 - 2527 fixed to (0-920)
·· servo.move_to(2,(1447 - speedmodeR[noparse][[/noparse]0]),20)············· ' specific for right motor configuration 1447 - 527· fixed to (0-920)

the issue im having is i cant seem to move to the servo.move_to(1 ... line. I cant really figure out why. FrontPing.go is an infinite loop (sweeps the front ping and checks for buffer restrictions) and doesnt return anything (yet, atleast)

any suggestions?

the code for both FrontPing and Movement can be found below

***************************************************************************************************************



MOVEMENT OBJECT:


CON
· _CLKMODE = XTAL1 + PLL16X
· _XINFREQ = 5_000_000
· LPING_PIN = 16
· FPING_PIN = 17
· RPING_PIN =· 18
· Front_Servo = 2
· Right_Servo = 1
· Left_Servo = 0
· LM = 0
· RM = 1

OBJ·······
· SERVO :· "Servo4"
· ping : "ping"
· PC_Text: "PC_text"
· kb···· : "PC_Keyboard"··· 'You can also use Keyboard object for PS2-Keyboard
· FrontPing : "FrontPing_H_1"····················
VAR
· long speedmodeL[noparse][[/noparse]2]
· long speedmodeR[noparse][[/noparse]2]
· long LeftView[noparse][[/noparse]3]
· long RightView[noparse][[/noparse]3]
· long FrontView[noparse][[/noparse]3] '(one for data, one for position)
· byte delay,frontobs, buffer, CorrectionStep
· long LeftStray, RightStray
· long Stack[noparse][[/noparse]9]

PUB go(LMVar, RMVar, Speed) | unit0, i, j, k, temprange
· servo.start(0,Front_Servo,1500,LM,1500,RM,0,3)
···· PC_Text.start(1)······································· ' Initialize TV Object
· SpeedmodeL[noparse][[/noparse]0] := 410
· SpeedmodeR[noparse][[/noparse]0] :=400·
· i := 0
·' Speed := 3
'FrontPing.go(20, 300, 2200)

repeat
·· cognew(FrontPing.go(5, 650, 2050), @Stack)··············································
·· servo.move_to(1,(1607 + SpeedmodeL[noparse][[/noparse]0]),20)············· ' specific for motor configuration 1607 - 2527 fixed to (0-920)
·· servo.move_to(2,(1447 - speedmodeR[noparse][[/noparse]0]),20)············· ' specific for motor configuration 1447 - 527· fixed to (0-920)
··· 'waitcnt(cnt + 150_000_000)···························· ' wait for trajector change to become evident
·· LeftView[noparse][[/noparse]0] := LeftView[noparse][[/noparse]1] := ping.centimeters(LPING_PIN)
·· RightView[noparse][[/noparse]0] := RightView[noparse][[/noparse]1] := ping.centimeters(RPING_PIN)
·· FrontObs := 0
·· buffer := 30
'*********************************DATA AVG BLOCK*************************************
·· repeat i from 0 to 2
········ LeftView[noparse][[/noparse]1] := ping.centimeters(LPING_PIN)
········ RightView[noparse][[/noparse]1] := ping.centimeters(RPING_PIN)
········
······ if LeftView[noparse][[/noparse]1] > LeftView[noparse][[/noparse]0]
········ LeftView[noparse][[/noparse]0] := LeftView[noparse][[/noparse]1]
······ if RightView[noparse][[/noparse]1] > RightView[noparse][[/noparse]0]
········ RightView[noparse][[/noparse]0] := RightView[noparse][[/noparse]1]
···· 'waitcnt(cnt + 10_000)
·waitcnt(cnt + 1_000_000)
·
···· repeat i from 0 to 2
········ LeftView[noparse][[/noparse]2] := ping.centimeters(LPING_PIN)
········ RightView[noparse][[/noparse]2] := ping.centimeters(RPING_PIN)
········
······ if LeftView[noparse][[/noparse]2] > LeftView[noparse][[/noparse]1]
········ LeftView[noparse][[/noparse]1] := LeftView[noparse][[/noparse]2]
······ if RightView[noparse][[/noparse]2] > RightView[noparse][[/noparse]1]
········ RightView[noparse][[/noparse]1] := RightView[noparse][[/noparse]2]
·
···· 'waitcnt(cnt + 10_000)
'*********************************DATA AVG BLOCK*************************************

· if (LeftView[noparse][[/noparse]1] =< LeftView[noparse][[/noparse]0])···
··· LeftStray := LeftView[noparse][[/noparse]0] - LeftView[noparse][[/noparse]1]··················· 'Alignment info about left wall
··· RightStray := 0
· else
··· RightStray := ||(LeftView[noparse][[/noparse]0] - LeftView[noparse][[/noparse]1])
··· LeftStray := 0
· {if (FrontView[noparse][[/noparse]1] =< Buffer)
··· FrontObs :=· FrontView[noparse][[/noparse]1]}
·
· if (LeftStray => Rightstray)
·· case LeftStray
····· 1..5: CorrectionStep :=·· 30
····· '5..10: CorrectionStep :=· 80 + LeftStray·
····· '10..20: CorrectionStep := 120 + LeftStray
····· OTHER: CorrectionStep := 0
· else
··· case RightStray
····· 1..5: CorrectionStep :=· 30
····· '5..10: CorrectionStep := 80 +· RightStray·
····· '10..20: CorrectionStep := 120 + RightStray
····· OTHER: CorrectionStep := 0

· PC_Text.dec(LeftView[noparse][[/noparse]0])
· PC_Text.str(string(" - "))
· PC_Text.dec(LeftView[noparse][[/noparse]1])
· PC_Text.str(string("· "))
·PC_Text.dec(RightView[noparse][[/noparse]0])
· PC_Text.str(string(" - "))
· PC_Text.dec(RightView[noparse][[/noparse]1])
· PC_Text.str(string("· "))
·{ PC_Text.dec(FrontView[noparse][[/noparse]0])
· PC_Text.str(string(" - "))}
· PC_Text.dec(FrontView[noparse][[/noparse]1])··
·
· {PC_Text.str(string("· LS: "))
· PC_Text.dec(LeftStray)···························· ' Pad For Clarity
· PC_Text.str(string("· RS: "))
· PC_Text.dec(RightStray)
· PC_Text.str(string("· CS: "))
· PC_Text.dec(CorrectionStep)·····
··· if LeftStray > 0
····· PC_Text.str(string(" RIGHT"))
··· if RightStray > 0
····· PC_Text.str(string(" LEFT"))
··· if FrontObs > 0
····· PC_Text.str(string(" FRONTOBS = "))
····· PC_Text.dec(FrontObs) }
··· PC_Text.out($0D)···············
··
·if (LeftStray > RightStray)······························ ' speed up left, slow down right'
· SpeedModeL[noparse][[/noparse]0] := SpeedModeL[noparse][[/noparse]0] + CorrectionStep
· SpeedModeR[noparse][[/noparse]0] := SpeedModeR[noparse][[/noparse]0] - CorrectionStep
·else······················································ ' speed up right, slow down left'
· SpeedModeL[noparse][[/noparse]0] := SpeedModeL[noparse][[/noparse]0] - CorrectionStep
· SpeedModeR[noparse][[/noparse]0] := SpeedModeR[noparse][[/noparse]0] + CorrectionStep
· if (40 < 30)
·· SpeedModeL[noparse][[/noparse]0] := 0
·· SpeedModeR[noparse][[/noparse]0] := 0·
· else
·· if ((SpeedModeL[noparse][[/noparse]0] < 250) or (SpeedModeL[noparse][[/noparse]0] >600))
···· if (SpeedModeL[noparse][[/noparse]0] < 250)
········ SpeedModeL[noparse][[/noparse]0] := 250
···· else
········ SpeedModeL[noparse][[/noparse]0] := 600
·······
·· if ((SpeedModeR[noparse][[/noparse]0] < 250) or (SpeedModeR[noparse][[/noparse]0] > 600))
···· if (SpeedModeR[noparse][[/noparse]0] < 250)
······ if (SpeedModeR[noparse][[/noparse]0] < 250)
········ SpeedModeR[noparse][[/noparse]0] := 250
······ else
········ SpeedModeR[noparse][[/noparse]0] := 600

·servo.move_to(1,(1607 + SpeedmodeL[noparse][[/noparse]0]),20)············· ' specific for motor configuration 1607 - 2527 fixed to (0-920)
·servo.move_to(2,(1447 - speedmodeR[noparse][[/noparse]0]),20)············· ' specific for motor configuration 1447 - 527· fixed to (0-920)


***************************************************************************************************************
AND FRONT PING OBJECT:

CON
· _CLKMODE = XTAL1 + PLL16X
· _XINFREQ = 5_000_000
· LPING_PIN = 16
· FPING_PIN = 17
· RPING_PIN =· 18
· Front_Servo = 2
· Right_Servo = 1
· Left_Servo = 0
· LM = 0
· RM = 1
OBJ·······
· SERVO :· "Servo4_2"
· ping :·· "ping"
· PC_Text: "PC_text"
· kb···· : "PC_Keyboard"··· 'You can also use Keyboard object for PS2-Keyboard·························
VAR
· long PosVar
· byte Range[noparse][[/noparse]20]
· long Stack[noparse][[/noparse]9]···
· byte buffer

PUB go(snap, fromR, toR) | i, j, k, temprange
servo.start(1300,Front_Servo,0,4,0,5,0,3)
· 'snap := 10································ ' how many pings to take
· 'fromR := 600······························· ' beginning where
· 'toR := 2000································ ' ending where
· snap := snap - 1
· 'PosVar := 400
· i := ((toR - fromR)/(snap))-1
· j := 0
·············
· buffer := 20
·
· 'servo.move_to(1,1500,1)··· 'move two at once
· 'servo.move_to(2,1500,1)
·
· waitcnt (cnt+50_000_000)
repeat
··· i := ((toR - fromR)/(snap))-1
··· j := 0
··· servo.move_to(0,fromR,1)
·
··· repeat PosVar from fromR-1 to toR·· ''300 is far left, 2200 is far right, 1300 is right in the middle
····· servo.move_to(0,PosVar,1)
······ 'servo.wait(0)
····· waitcnt (cnt+30_000)
····· i++
····· if i == ((toR - fromR)/snap)
······· j := j + 1
······· temprange := ping.Millimeters(FPING_PIN)········· ' Get Range In millimeters·····
······· Range[noparse][[/noparse]j-1] := temprange
······· if (Range[noparse][[/noparse]j-1] <= buffer)
········· 'servo.move_to(1,0,20)
········· 'servo.move_to(2,0,20)
········· 'servo.wait(1)
········· 'servo.wait(2)
······· i := 0
··
·{ PC_Text.start(1)······································· ' Initialize TV Object
··· repeat i from 0 to snap
····· PC_Text.dec(i*((toR - fromR)/snap)+fromR)
····· PC_Text.str(string("-->"))··························· ' Pad For Clarity
····· PC_Text.dec(Range[noparse][[/noparse]i])
····· PC_Text.out($0D)········· }···
·····
· repeat PosVar from toR to fromR-1·· ''300 is far left, 2200 is far right, 1300 is right in the middle
····· servo.move_to(0,PosVar,1)
······ 'servo.wait(0)
····· waitcnt (cnt+30_000)
····· i++
····· if i == ((toR - fromR)/snap)
······· j := j + 1
······· temprange := ping.Millimeters(FPING_PIN)········· ' Get Range In millimeters·····
······· Range[noparse][[/noparse]j-1] := temprange
········ if (Range[noparse][[/noparse]j-1] <= buffer)
········· 'servo.move_to(1,0,20)
········· 'servo.move_to(2,0,20)······················ ' Pause 1/10 Second
·········· 'servo.wait(1)
·········· 'servo.wait(2)
······· i := 0
·······
{··· repeat i from 0 to snap
····· PC_Text.dec(i*((toR - fromR)/snap)+fromR)
····· PC_Text.str(string("-->"))··························· ' Pad For Clarity
····· PC_Text.dec(Range[noparse][[/noparse]i])
····· PC_Text.out($0D)···················· }
·····
···
PUB exact(position)
··· servo.start(position,Front_Servo,0,LM,0,RM,0,3)
··· servo.move_to(0,position,1)
··· waitcnt (cnt+20_000)
··· range := ping.Inches(FPING_PIN)
··· return range
··
·
· ***************************************************************************************************************




▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Lenny Bogdanov
Systems Concept Center

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 22:27
    You must not use a routine in the other object with COGNEW
    cognew(FrontPing.go(5, 650, 2050), @Stack)
    


    Rather do this:
    FrontPing.start(5, 650, 2050)
    
    ....
    
    start(a,b,c)
        cognew(go(a,b,c), @Stack)
    


    Or similar - many possibilities...
  • Chuck McManisChuck McManis Posts: 65
    edited 2008-02-23 22:32
    In addition to what deSilva said I wonder if you mean to be starting a new cog each time through the loop? Unless Frontping exits and frees up the cog you will quickly run out of COGs to run on.

    --Chuck
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 22:41
    O dear.. There is more of course, but is difficult to read through your code...

    A stacksize of 9 is not apprropriate in most cases.. Start with 20, and when you encounter strange effects increase it after a analysis of what could happen in your calling sequence (recursion? masses of local parameters?...) Think of calls to library routines with many parameters...

    Best you change those 3 main issues and report back smile.gif
  • lenny1337lenny1337 Posts: 26
    edited 2008-02-23 22:47
    thanks for the quick reply

    i'm not much of a coder, just a kid in college overloaded with a senior design project. Im working on your advice and will let you know....

    all advice is appreciated.

    thanks again


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Lenny Bogdanov
    Systems Concept Center

    Post Edited (lenny1337) : 2/23/2008 10:54:59 PM GMT
Sign In or Register to comment.