Thread: Random Numbers
View Single Post
  #8   Spotlight this post!  
Unread 18-01-2004, 21:26
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: Random Numbers

Quote:
Originally Posted by Dave Flowerday
I'm quite curious as to why you'd want to set your PWM outputs to a random value, though. Most of us prefer to remove random behavior from our robots
Quote:
Originally Posted by KevinB
If you are setting the values of the PWMs to a random number everytime the program loop is executed (every ~26ms) .... the motors probably wont do anything ... because theres no way they can change speeds that quickly.
I did this mostly as a programming exercise. But you can use random moving as a defensive technique (grap goal & start random). Specifically, you probably wouldn't use it. But it might surprise your oponents a little.

The full code is:
Code:
//...

#include <stdlib.h>


//// DEFINE USER VARIABLES AND INITIALIZE THEM HERE

const int gTime = 75;
#define Left_Forward 0
#define Right_Forward 255
#define Left_Back 255
#define Right_Back 0

//...

//Prototype
int rand(void);

//...

/*******************************************
* FUNCTION NAME: Random
* PURPOSE:       Returns a random char
* CALLED FROM:   this file, Default_Routine
* ARGUMENTS:     none
* RETURNS:       char
*******************************************/
unsigned char Random(void)
{
  unsigned int rnd;
  
  rnd = rand();
  
  rnd = rnd / RAND_MAX;
  return (unsigned char) (rnd & 0xFF);
}

/*******************************************
* FUNCTION NAME: Default_Routine
* PURPOSE:       Performs the default mappings of inputs to outputs for the
*                Robot Controller.
* CALLED FROM:   this file, Process_Data_From_Master_uP routine
* ARGUMENTS:     none
* RETURNS:       void
*******************************************/
//Random Mover

void Default_Routine(void)
{ 
 static unsigned int i_cCount;

 if (i_cCount > 1*gTime)
   {
    pwm01=pwm03=Random();
	pwm02=pwm04=Random();
    i_cCount = i_cCount;
   }
    i_cCount = i_cCount + 1;
  
  printf("i_cCount=%d\n",(int)i_cCount);
} // END Default_Routine();
I shortened it for point of discussion. the //... means I removed something unimportant, or unmodified from the default code.