Go to Post If Andy Baker wants 9999 for his new team then who's gonna stop him? - IndySam [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 08-03-2008, 11:02
Lakeeffect1674 Lakeeffect1674 is offline
Registered User
FRC #1674
 
Join Date: Jan 2007
Location: Onekama, Michigan
Posts: 18
Lakeeffect1674 is an unknown quantity at this point
Code error with while loops??

-History-
I am a student who is currently learning C++, so I know simple commands like a while statement.
I was able to get this code working when I program a PC.
I searched the web for help but found nothing
----==================
Now when I program the Robot using C I get it to do a loop but after a few times the bot will say code error.
I just want the robot to travel for a certain number of seconds.
here is my code what is wrong? or is there a better way to have a robot on a timer?
(this code is under the void Autonomous(void) function)


if(stop == 0)
{
timer = 900;
while(timer > 0)
{
timer = timer - 1;
printf( "timer is %d\n", timer );
pwm13 = pwm14 = pwm15 = pwm16 = 255;
}

stop = 1; // this should make sure the code wont run again
}
printf( "stop is %d\n", stop );
pwm13 = pwm14 = pwm15 = pwm16 = 127;
printf( "pwm13 is %d\n", pwm13 );

Last edited by Lakeeffect1674 : 08-03-2008 at 11:04.
  #2   Spotlight this post!  
Unread 08-03-2008, 11:06
EHaskins EHaskins is offline
Needs to change his user title.
AKA: Eric Haskins
no team (CARD #6 (SCOE))
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Elkhorn, WI USA
Posts: 998
EHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond repute
Send a message via MSN to EHaskins
Re: Code error with while loops??

If you aren't communicating with the master proc, by callin GetData and PutData, the master assumes the user proc is locked.

EDIT:
Code:
  int timer;
  while (autonomous_mode)   /* DO NOT CHANGE! */
  {
	  if (statusflag.NEW_SPI_DATA)      /* 26.2ms loop area */
	  {
                            timer++;
		  Getdata(&rxdata);   /* DO NOT DELETE, or you will be stuck here forever! */
		  //Put your code here
                           if (timer < 900){}
                           else if (timer < 1000 {}
                           //ect.

		  Putdata(&txdata);   /* DO NOT DELETE, or you will get no PWM outputs! */
	  }
  }
__________________
Eric Haskins KC9JVH

Last edited by EHaskins : 08-03-2008 at 11:11.
  #3   Spotlight this post!  
Unread 08-03-2008, 11:25
Lakeeffect1674 Lakeeffect1674 is offline
Registered User
FRC #1674
 
Join Date: Jan 2007
Location: Onekama, Michigan
Posts: 18
Lakeeffect1674 is an unknown quantity at this point
Re: Code error with while loops??

what function do I put that code under?
I tried puting it in the same place as my while loop, but it didn't do anything.
timer = 0;
while (autonomous_mode) /* DO NOT CHANGE! */
{
if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */
{
timer++;
Getdata(&rxdata); /* DO NOT DELETE, or you will be stuck here forever! */
//Put your code here
if (timer < 900){
pwm13 = pwm14 = pwm15 = pwm16 = 255;
}
else {
pwm13 = pwm14 = pwm15 = pwm16 = 127;
}
//ect.

Putdata(&txdata); /* DO NOT DELETE, or you will get no PWM outputs! */
}
}
  #4   Spotlight this post!  
Unread 08-03-2008, 19:51
EHaskins EHaskins is offline
Needs to change his user title.
AKA: Eric Haskins
no team (CARD #6 (SCOE))
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Elkhorn, WI USA
Posts: 998
EHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond repute
Send a message via MSN to EHaskins
Re: Code error with while loops??

If you're using IFI defualt code, then you put that entire code block in User_autonomous_code() in user_routines_fast.c.

If you're using Kevin's c18 v3 code, then you put the code contained WITHIN "if (statusflag.NEW_SPI_DATA)" in Autonomous() in autonomous.c. You also need to make timer static if you're using Kevin's code.
__________________
Eric Haskins KC9JVH
  #5   Spotlight this post!  
Unread 08-03-2008, 20:05
Jake M Jake M is offline
void* Jake;
FRC #1178 (DURT)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Missouri
Posts: 118
Jake M has a spectacular aura aboutJake M has a spectacular aura about
Re: Code error with while loops??

I made this exact same mistake when I first started FRC Programming. What you need to realize is that the architecture of PC Terminal-style programming (When you write a program in C that runs in a black DOS-style window) and the architecture of microcontroller programming is inherently different.

When you write a program in C to run in a DOS-style terminal window, everything executes linearly. The program starts at main() and when it gets to the end, the program terminates. This fact doesn't change, but there's a lot more going onin the RC that just what you write. In reality, 90% of what the RC is actually doing is being done in the background, and you don't ever have to deal with it.

You may not have programmed it, but you know how the OI and the RC communicate through the Radio modems, and how you can program the copper pins on the RC through the PWM and Digial IO and Analog Input variables in the code? All of that stuff is being controlled within the code, primarily by the Getdata() and Putdata() routines which you may have seen and been warned not to mess with.

It probably seems to you like the code variables represent what's going on in the hardware at the exact moment you look at them, and that when you change one of these variables, the hardware immediately changes what it's doing. But it only seems that way because Getdata() and Putdata() are being called roughly 38 times per second. Take a look at the main.c file. The program starts at main(), yes, but if you look within that function, you'll see an infinite loop (while(1)). As a programmer, you know that this loop will never end. Well, this is completely intentional. Within the loop, you'll see the routine Process_Data_From_Master_uP(), which is called within an if() statement (the NEW_SPI_DATA flag is set automatically when the microprocessor has new data that needs to be processed. This is where the 38 times per second comes in). The function Process_Data_From_Master_uP() basically just calls Getdata(), calls Default_Routine(), calls Putdata(), and ends. Default Routine is probably where you do all of your stuff, like reading the josytick values and setting pwms accordingly to drive the robot, etc. The autonomous routine doesn't actually call Process_Data_From_Master_uP(), but the Getdata(), do something, Putdata() structure is the same.

Here's the main point. When you write your own code, you need to realize that the microprocessor is running through the entire thing 38 times per second. Not just that, it NEEDS to run through the entire thing 38 times per second. If you hold everything up in a while loop, then Getdata() and Putdata() don't get called, and the RC stops communicating with itself and with the OI. The code you posted is actually fine, but the problem is that it only takes a fraction of a second for it to count to 900. From what you say, it seems like the counting to 900, even though it takes under a second, it taking a little bit too long and it throws off Getdata() and Putdata(), or maybe it's just overtaxing the processor.

The solution to this problem would be to use a timer that increments once during each overall program loop, and check the timer once during each loop to see if it's time to stop. Like so.

Code:
void Auto_Drive(void)
 {
  // The static keyword is required because this function is being called and 
  // terminated 38 times per second. Thus a non-static timer would loose its
  // value every time the function terminated. A static variable remains even
  // after the function terminates and retains its value. Also, it is set to 0 only
  // when it is first defined.
  static unsigned int timer = 0;

  // Let's drive forward for 5 seconds.
  if(timer < 190)
   {
    // Drive straight forward at full speed.
    Main_Drive(127 /*X Joystick Value*/, 255 /*Y Joystick Value*/);
    ++timer;
   }
  else if(timer == 190)
   {
    //Stop moving
    Main_Drive(127 /*X Joystick Value*/, 127 /*Y Joystick Value*/);
   }
 }
I used 190, because the Getdata()/Putdata() combo takes about 26.2milliseconds to execute. So, if one increment of the timer takes .0262seconds, and we want to drive forward for 5 seconds...

Code:
              1 program loop
5 seconds  X  ------per------
              .0262 seconds
The seconds cancel each other out, and we're left with 190.8 program loops. Thus, we increment timer 190 times.

Hope you've learned something.
__________________
Code:
void function(void)
 {
  function();
 }
  #6   Spotlight this post!  
Unread 08-03-2008, 22:02
garyk garyk is offline
Programming Mentor: 668, 972, 2643
FRC #0668 (Apes of Wrath)
Team Role: Mentor
 
Join Date: Dec 2006
Rookie Year: 2005
Location: Santa Clara (Silicon Valley) Calif.
Posts: 93
garyk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the rough
Re: Code error with while loops??

Along with JakeM's great explanation, I'd like to point you to my white paper "A programming template for Autonomous Mode": Chief Delphi => CD Media => Papers => Search => keyword => Autonomous mode.

If you hardcode constants like 900 in your example, in order to tune the routine you'll have to change the constant within the code (and maybe that 900 in several places), and the work will cascade as you do more counts in subsequent steps.

A state machine is a great way to implement steps within Auton. mode and I hope the paper helps. There's a lot of good information in the White Papers section, and when things calm down you can spend some time looking at what's there, and see if they will help with the things you're learning.

GaryK
__________________

Silicon Valley Regional 2005, 2006 972
Silicon Valley Regional 2007 668 Xerox Creativity Award
Championship Event 2007 668
Portland Regional 2008 668
Silicon Valley Regional 2008 668, 972
Beta Test Team 2008 668 (with 100 & 254)
Silicon Valley Regional 2009 668 Regional Chairman's Award; 2643
Sacramento Regional 2009 668 Winning Alliance (thanks, 1717 & 2473!), 2010 Winning Alliance 3256
CalGames 2006, 2007, 2008, 2009, 2010, 2011 Field Tech
NorCal FTC Regional 2008, 2009 Inspector
Championship Event 2009
San Diego, Silicon Valley Regionals; Champ. Event 2010 668, 2643, 3256
Silicon Valley, Madera Regional 2012 2643
WRRF Programming Instructor 2006-2016
Regional Woodie Flowers Award 2014 2643 Utah Regional

  #7   Spotlight this post!  
Unread 09-03-2008, 13:19
Lakeeffect1674 Lakeeffect1674 is offline
Registered User
FRC #1674
 
Join Date: Jan 2007
Location: Onekama, Michigan
Posts: 18
Lakeeffect1674 is an unknown quantity at this point
Re: Code error with while loops??

thanks for your long reply it works now!
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
"Code Error" with RegisterRepeatingTimer d235j Programming 4 27-02-2008 21:51
Error in code light on, trouble finding error Bryan Herbst Programming 16 12-10-2007 21:59
While loops Guy_E VEX 6 01-11-2006 22:04
Moving robot while tracking with camera questions... Steve Orr Programming 6 02-02-2006 22:24
Code error on RC after downloading "bells and whistles" version of Kevins camera code DanDon Programming 6 10-01-2006 18:07


All times are GMT -5. The time now is 00:54.

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