Go to Post It's up to veteran teams and mentors to help keep the original spirit of FIRST alive. - It's up to us to make these boards exemplify the FIRST principals in every post. - Ben Mitchell [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 19-02-2003, 18:31
manodrum manodrum is offline
Registered User
#1127
 
Join Date: Feb 2003
Location: Alpharetta, GA
Posts: 3
manodrum is an unknown quantity at this point
Arrow loops inside of subs???

hi, i have written a little code that will fire our pistons for approx. 3 seconds when one of the joystick buttons is pushed, so I wrote an if statement after the SERIN that

"if p2_sw_trig = 1 then GOSUB FIRE3"

that works and sends it to my sub code which looks like this:


FIRE3:
relay1_fwd = 1
MyCounter VAR byte
StepSize VAR byte

MyCounter = 1

FOR MyCounter = 1 TO 3001 STEP StepSize
StepSize = delta_t * 25
NEXT

relay1_fwd = 0
RETURN


but once i push the trigger, i get a basic run error, but, when i take away the FOR...NEXT statements, everything works, what's up with this? can i not do loops inside of subs? thanks a lot

Stephen
  #2   Spotlight this post!  
Unread 19-02-2003, 18:34
Joel J's Avatar
Joel J Joel J is offline
do you..
no team
 
Join Date: May 2001
Rookie Year: 2000
Location: San Jose, CA
Posts: 1,445
Joel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond repute
Re: loops inside of subs???

Quote:
Originally posted by manodrum
hi, i have written a little code that will fire our pistons for approx. 3 seconds when one of the joystick buttons is pushed, so I wrote an if statement after the SERIN that

"if p2_sw_trig = 1 then GOSUB FIRE3"

that works and sends it to my sub code which looks like this:


FIRE3:
relay1_fwd = 1
MyCounter VAR byte
StepSize VAR byte

MyCounter = 1

FOR MyCounter = 1 TO 3001 STEP StepSize
StepSize = delta_t * 25
NEXT

relay1_fwd = 0
RETURN


but once i push the trigger, i get a basic run error, but, when i take away the FOR...NEXT statements, everything works, what's up with this? can i not do loops inside of subs? thanks a lot

Stephen
MyCounter is declared as a byte. A byte can store a number no larger than 254. After that it loops around to a negative number (an illusion).
__________________
Joel Johnson

Division By Zero (229) Alumni, 2003-2007
RAGE (173) Alumni, 1999-2003
  #3   Spotlight this post!  
Unread 19-02-2003, 19:59
rbayer's Avatar Unsung FIRST Hero
rbayer rbayer is offline
Blood, Sweat, and Code
no team (Teamless Orphan)
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Minnetonka, MN
Posts: 1,087
rbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of light
Send a message via AIM to rbayer
Re: Re: loops inside of subs???

Quote:
Originally posted by Joel J.
MyCounter is declared as a byte. A byte can store a number no larger than 254. After that it loops around to a negative number (an illusion).
While true, that is not the problem here. The problem is that the controllers need a SEROUT at least once every 125ms (that's once every five normal loops) in order to function properly. During your 3000-loop for structure, no data is sent or received from the rest of the RC causing it to die miserably. You need to find some way of putting a SEROUT in there somewhere.
__________________
New C-based RoboEmu2 (code simulator) available at: http://www.robbayer.com/software.php
  #4   Spotlight this post!  
Unread 19-02-2003, 20:03
ChrisA's Avatar
ChrisA ChrisA is offline
Registered User
#0857 (Superior Roboworks)
Team Role: College Student
 
Join Date: Feb 2002
Location: Michigan Tech
Posts: 157
ChrisA is on a distinguished road
Send a message via AIM to ChrisA
Re: Re: loops inside of subs???

Quote:
Originally posted by Joel J.
A byte can store a number no larger than 254. After that it loops around to a negative number (an illusion).
Unfortunately this is not the case. It can hold a number between 0 and 255 (256 values). If you have 255 and add one to it you will get 0. When you add 1 to 1 in binary you get 10. The 0 appears and the 1 is carried over to the next slot. So when you have 11111111 and you add 1 to it all 8 values turn to 0 and the 1 is carried over into the next available space. In this case there is no space left so it is simply dropped leaving you with 00000000.

[edit]As previously stated, MyCounter is a byte which can never reach 3001. As hard as the processor tries, it can never continuously add numbers to a byte and reach 3001. The byte will reach 255 and then drop back to 0 and then repeat which causes an infinite loop. there is no problem with running that loop where you have it. Try using a word (2 bytes stuck together) instead of a byte. If what rbayer says is true then you will never be able to do a loop for that long anyways, but I don't think there is any harm in trying it.[/edit]
__________________
Programmer
----------------
Team#857
Superior Roboworks

WildStang Alum

Last edited by ChrisA : 19-02-2003 at 20:12.
  #5   Spotlight this post!  
Unread 19-02-2003, 20:05
redbeard0531 redbeard0531 is offline
Registered User
#0686 (Bovine Intervention)
 
Join Date: Jan 2003
Location: New Market, MD
Posts: 40
redbeard0531 is an unknown quantity at this point
Send a message via AIM to redbeard0531
<edit>Removed stuff covered by people who replied faster that me.</edit>

Here is what I suggest.

GoSub Fire3:

Fire3:

count Var word 'Number of 26ms periods
Fire3On Var Bit ' 1 if the piston should fire

If p2_sw_trig = 1 then Fire3On = 1

If Fire3On = 0 Then DontFire:

If count < 3000 then
relay1_fwd = 1
count = count + 1 + delta_t
Else
relay1_fwd = 0
count = 0
Fire3On = 0
EndIf

DontFire:

Return

That is how I would do it, but it may not be exactly what you need.

Last edited by redbeard0531 : 19-02-2003 at 20:07.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Grease inside the Drill Motor gear box? archiver 2000 2 23-06-2002 23:07


All times are GMT -5. The time now is 21:22.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi