Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Solution to Timing Loops (http://www.chiefdelphi.com/forums/showthread.php?t=16994)

Greg McCoy 29-01-2003 21:08

Yes! He explained it better than I did. One thing that I would add is if you are really strained for RAM is that you could keep the counter a byte and if you need numbers bigger than the byte will supply, just add steps until you get the length you need.

rbayer 29-01-2003 22:59

Quote:

Originally posted by mjt902
Once again you correct me!!! Oh, I'm lazy anyway but thanks for the free code. Only one question have I:
where'd you get the 8 from in SELECT(loopCntHigh<<8 + loopcnt)

i think you and i are on the same page (only i'm on a different plane) , and its great learning from you!!!

The 8 will shift it left by 8 bits, leaving enough space for the byte (8-bits) to fill in the newly created free space.

Here's an example:

Assume loopCntHigh is 3 (0011 in binary), meaning we've looped through 256 values of loopCnt 3 times.
Assume loopCnt is 199 (11000111).

Then: loopCntHigh<<8=001100000000
Add loopCnt: 001111000111, which is 967. This makes sense as 256*3+199=967.

Does that make any sense? Bit shifting operations are always messy, but sometimes they are the easiest solution to problems like this (a single number spanning multiple variables).

Caleb Fulton 30-01-2003 08:46

Rob: Is there a place online that documents the mathematical limits of PBasic?

I ask because I like your bytevariable<<8+nibblevariable trick :)

rbayer 30-01-2003 20:54

Quote:

Originally posted by Caleb Fulton
Rob: Is there a place online that documents the mathematical limits of PBasic?

I ask because I like your bytevariable<<8+nibblevariable trick :)

Depends on what limits you are talking about. The Stamp manual from Parallax contains all the math operators, etc that the Stamp supports, but it doesn't give many good examples of how you would use most of it.

For me, I've picked most of it up doing a LOT of C/C++ stuff, much of which includes bit-operations such as this. Randall Hyde's assembly language tutorial (http://webster.cs.ucr.edu/Page_asm/A..._ArtofAsm.html) has some really good info on binary math and logic, but it isn't exactly and easy read. Like always, I'm available to answer any questions anybody comes up with, so feel free to drop me a PM or email.

--Rob

Mike537Strategy 01-02-2003 15:29

I'm trying to use the original timer code from the first thread in a simple dead reckoning pattern. My code has the time calculator directly after the SERIN, has 2 lines of code changing the wheel numbers for the SEROUT, then the SEROUT. It loops this until realtime = whatever time I want. It goes forward, turns right, forward, right, forward, right, then forward. The problem is that after the first forward (Which works perfectly), it just turns right until I turn off the robot. Any ideas why? The variables are the same as the default, delta_t is declared and set to 1 in intilization. The forums aren't being nice and the SERINs are all on one line in the actual code.

Code:

do
  Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p2_x,p1_x,p4_x,p3_x,PB_mode,packet_num,p2_y,p1_y,p4_y,p3_y,p2_wheel,p1_wheel,p4_wheel,p3_wheel]
  time = time + delta_t + 1
  if time > 38 then
      time = time - 38
      realtime = realtime + 1
  endif
  p1_y = 254
  p2_y = 254
  Serout USERCPU, OUTBAUD, [255,255,p1_y,0,p2_y,0,127,127,127,127,127,127,
                            127,127,127,127]
loop until realtime = 2
do
  Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p2_x,p1_x,p4_x,p3_x,PB_mode,packet_num,p2_y,p1_y,p4_y,p3_y,p2_wheel,p1_wheel,p4_wheel,p3_wheel]
  p1_y = 254
  p2_y = 0
  time = time + delta_t + 1
  if time > 38 then
      time = time - 38
      realtime = realtime + 1
  endif
  Serout USERCPU, OUTBAUD, [255,255,p1_y,0,p2_y,0,127,127,127,127,127,127,
                            127,127,127,127]
loop until realtime = 1
do
  Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p2_x,p1_x,p4_x,p3_x,PB_mode,packet_num,p2_y,p1_y,p4_y,p3_y,p2_wheel,p1_wheel,p4_wheel,p3_wheel]
  time = time + delta_t + 1
  if time > 38 then
      time = time - 38
      realtime = realtime + 1
  endif
  p1_y = 254
  p2_y = 254
  Serout USERCPU, OUTBAUD, [255,255,p1_y,0,p2_y,0,127,127,127,127,127,127,
                            127,127,127,127]
loop until realtime = 2
do
  Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p2_x,p1_x,p4_x,p3_x,PB_mode,packet_num,p2_y,p1_y,p4_y,p3_y,p2_wheel,p1_wheel,p4_wheel,p3_wheel]
  p1_y = 254
  p2_y = 0
  time = time + delta_t + 1
  if time > 38 then
      time = time - 38
      realtime = realtime + 1
  endif
  Serout USERCPU, OUTBAUD, [255,255,p1_y,0,p2_y,0,127,127,127,127,127,127,
                            127,127,127,127]
loop until realtime = 1
do
  Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p2_x,p1_x,p4_x,p3_x,PB_mode,packet_num,p2_y,p1_y,p4_y,p3_y,p2_wheel,p1_wheel,p4_wheel,p3_wheel]
  time = time + delta_t + 1
  if time > 38 then
      time = time - 38
      realtime = realtime + 1
  endif
  p1_y = 254
  p2_y = 254
  Serout USERCPU, OUTBAUD, [255,255,p1_y,0,p2_y,0,127,127,127,127,127,127,
                            127,127,127,127]
loop until realtime = 2
do
  Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p2_x,p1_x,p4_x,p3_x,PB_mode,packet_num,p2_y,p1_y,p4_y,p3_y,p2_wheel,p1_wheel,p4_wheel,p3_wheel]
  p1_y = 254
  p2_y = 0
  time = time + delta_t + 1
  if time > 38 then
      time = time - 38
      realtime = realtime + 1
  endif
  Serout USERCPU, OUTBAUD, [255,255,p1_y,0,p2_y,0,127,127,127,127,127,127,
                            127,127,127,127]
loop until realtime = 1
do
  Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p2_x,p1_x,p4_x,p3_x,PB_mode,packet_num,p2_y,p1_y,p4_y,p3_y,p2_wheel,p1_wheel,p4_wheel,p3_wheel]
  time = time + delta_t + 1
  if time > 38 then
      time = time - 38
      realtime = realtime + 1
  endif
  p1_y = 254
  p2_y = 254
  Serout USERCPU, OUTBAUD, [255,255,p1_y,0,p2_y,0,127,127,127,127,127,127,
                            127,127,127,127]
loop until realtime = 2


rbayer 01-02-2003 16:32

Don't forget to reset realtime to 0 between your sections. Or, you could change the "loop until realtime=1" to "loop until realtime=3". From quickly looking over your code, I'd guess that this is the problem.

--Rob

Anthony Kesich 02-02-2003 20:40

Why make it so hard on yourselves
 
why are all of you making it so hard on yourselves?
just put the time counting loop at the beginning of your autonomous mode, then use if/then statements relating realtime and time. Why repeat it 7 times throughout your code along with serin again and again? plus, why worry about counting for 1 min 45 sec? unless i am extremely mistaken, the auton mode is only 15 seconds long, and i doubt you want to run your robot autonomously after that because no matter how godly of a programmer you are, human senses, adaptability, and recognization will always outdo straight computer logic.

-Anthony

wwrye 03-02-2003 23:52

redundance
 
Mike, what is up with all the identical code, don't you think you could go with the flow of the main loop and just do something like this:
Code:

':::Timer variables
cntr_cycles        VAR byte
seconds                VAR nib '15 sec timer,make byte for more time

seconds = 0          'same as your realtime variable
cntr_cycles = 0      'same as your time variable

...

if auton_mode = 1 then do_auton

        'non autonomous code       

       
goto skip_auton
do_auton:

':::15 second TIMER::
        if cntr_cycles >= 38 then update_timer

        goto skip_update_timer
        update_timer:
                seconds = seconds + 1
                cntr_cycles = 0
       
        skip_update_timer:

                select seconds
                        case <6  'until seconds = 5
                                drive_R = 180        'put drive commands accordingly
                                drive_L = 180
                        case <11
                                drive_R = 127
                                drive_L = 127
                        case <16
                                drive_R = 64
                                drive_L = 190       
                endselect

        cntr_cycles = cntr_cycles + 1 + delta_t
        'debug ?cntr_cycles, tab, ?delta_t, tab, ?cntr_cycles, cr
':::15 second TIMER::

Questions/comments, pm me

Steven Carmain 05-02-2003 13:12

A problem is your mixing syntaxes. The goto in the beginning is 2.0, but select case is 2.5

rbayer 05-02-2003 17:45

Quote:

Originally posted by Steven Carmain
A problem is your mixing syntaxes. The goto in the beginning is 2.0, but select case is 2.5
There is no problem with his code. You can mix 2.0 syntax into the 2.5 without any problems. It would be better from a style perspective to have said:

if auton_mode=1 then
'auto code goes here
else
'normal here
endif

but it isn't wrong to do it the other way.

Morgoth 06-02-2003 22:33

You don't need to worry abou the timing, just take your "cue" for the fifteen seconds from when auton_mode changes to zero and comp_mode (or something like that) changes to 1.

rbayer 07-02-2003 01:11

Quote:

Originally posted by Morgoth
You don't need to worry abou the timing, just take your "cue" for the fifteen seconds from when auton_mode changes to zero and comp_mode (or something like that) changes to 1.
But if you're doing dead-reckoning, you need to know how many seconds have passed so far so that you know what you should be doing.

Morgoth 07-02-2003 19:17

Ahhhh, now I understand. Thanks for the insight. We are going to follow the line so I didn't think of that.

Greg McCoy 08-02-2003 10:32

There are many ways to skin the cat, but some ways are better than others :D

Nate Smith 08-02-2003 15:00

Quote:

Originally posted by rbayer
But if you're doing dead-reckoning, you need to know how many seconds have passed so far so that you know what you should be doing.
Not necessarily the number of seconds, but rather the amount of time(program cycles work here) your program has been running...and it's easy enough to have a program you can "train"...i'm going to be putting together a white paper sometime later in the season about the autonomous training system I came up with for a team that I got asked to help...


All times are GMT -5. The time now is 14:50.

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