Go to Post The biggest thing is you don't have to make your robot like another teams. Come up with something unique and go from there. RAISE THE BAR..... - camtunkpa [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 09-02-2005, 17:39
Orborde Orborde is offline
Registered User
FRC #1747
Team Role: Mentor
 
Join Date: Apr 2004
Rookie Year: 2003
Location: Indianapolis, IN
Posts: 44
Orborde has a spectacular aura aboutOrborde has a spectacular aura about
Send a message via AIM to Orborde
System Clock?

I'm pretty new at this programming stuff, and as we're trying to get a speed control on our robot's wheels together, I concluded that we needed some way to record time. I came up with the following, which will keep track of the time since powerup:

Code:
/* clock.h */ 

#ifndef CLOCK_H
#define CLOCK_H

typedef unsigned long int clock_int;

/* sets up the clock interrupts and such */
void clock_init();

/* runs every time the clock hits an interrupt */
void Clock_Interrupt_Handler();

/* starts the clock ticking */
void clock_start();

/* halts the clock */
void clock_stop();

/* returns the current clock time safely */
clock_int Get_Clock_Time();

#endif


/* clock.c */
#include "ifi_aliases.h"
#include "ifi_default.h"
#include "ifi_utilities.h"
#include "clock.h"

volatile clock_int clock_time;	// time elapsed since last boot * .1msec

/* sets up the clock interrupts and such */
/* call this from User_Initialization */
void clock_init()
{
	clock_time = 0;
};

/* runs every time the clock hits an interrupt */
void Clock_Interrupt_Handler()
{
	//
};
/* starts the clock ticking */
void clock_start()
{
	// enable the clocking interrrupt
};

/* halts the clock */
void clock_stop()
{
	// disable the clocking interrupt
};

unsigned long int Get_Clock_Time()
{
	unsigned long int time;
	
	/* halt the clock for reading */
	clock_stop();

	/* read the time */
	time = clock_time;

	/* start the clock again ASAP */
	clock_start();

	return time;
};
Explanation of the above:
All times are of type clock_int, which is an unsigned long int (32 bits).
clock_init() is called in User_Initialization and will set up the interrupts for the timer.
Clock_Interrupt_Handler() is called from the interrupt handling routine; it checks to see if the timer triggered the interrupt, and if so, increments clock_time.
Get_Clock_Time() safely returns the value of clock_time.

The timer interrupt will be so arranged as to trigger every .1 msec. Thus, the clock ticks every .1 msec. Doing the math on that shows me that the RC could run for nearly 5 days continuously before the clock resets to 0. I think it's a pretty safe bet that no match will go on that long, so I now have a safe way of timing things by simple subtraction.

Comments? I'll be the first to admit I'm a n00b. Is this a totally insane way of doing this? Is there some RC feature that does this for me? Or am I a Clever Boy for putting it together?
  #2   Spotlight this post!  
Unread 10-02-2005, 07:18
stephenthe1 stephenthe1 is offline
Registered User
#1008
 
Join Date: Dec 2004
Location: Ohio
Posts: 113
stephenthe1 is on a distinguished road
Re: System Clock?

you claimed to be "new" at this, have you tried the clock? I plan on making use of it, it's a good idea.
  #3   Spotlight this post!  
Unread 10-02-2005, 15:45
Orborde Orborde is offline
Registered User
FRC #1747
Team Role: Mentor
 
Join Date: Apr 2004
Rookie Year: 2003
Location: Indianapolis, IN
Posts: 44
Orborde has a spectacular aura aboutOrborde has a spectacular aura about
Send a message via AIM to Orborde
Re: System Clock?

Another question: Is .1msec too small an interval? I'm concerned that it will chew up all the processor time and make everything die a horrible death.
  #4   Spotlight this post!  
Unread 10-02-2005, 16:49
sciguy125 sciguy125 is offline
Electrical Engineer
AKA: Phil Baltar
FRC #1351
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Sunnyvale, CA
Posts: 519
sciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond repute
Send a message via AIM to sciguy125 Send a message via MSN to sciguy125 Send a message via Yahoo to sciguy125
Re: System Clock?

Programming isn't my field of expertise, so correct me if I'm wrong, but isn't user_routines (or whatever that user function is called) called every 26.5ms? If so, couldn't you just count how many time's it's been called. Increment some variable or something. I'm not sure how exact the time is on it though...
__________________

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GE/S/P a-- e y-- r-- s:++ d+ h! X+++
t++ C+ P+ L++ E W++ w M-- V? PS+ PE+
5- R-- tv+ b+ DI+++ D- G
------END GEEK CODE BLOCK------
  #5   Spotlight this post!  
Unread 10-02-2005, 18:46
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: System Clock?

Quote:
Originally Posted by sciguy125
Programming isn't my field of expertise, so correct me if I'm wrong, but isn't user_routines (or whatever that user function is called) called every 26.5ms? If so, couldn't you just count how many time's it's been called. Increment some variable or something. I'm not sure how exact the time is on it though...
26.2ms. This can work, it's easier to just use given milliseconds.

Quote:
Originally Posted by Orborde
Another question: Is .1msec too small an interval? I'm concerned that it will chew up all the processor time and make everything die a horrible death.
It is probably a little small.
  #6   Spotlight this post!  
Unread 10-02-2005, 22:16
devicenull devicenull is offline
Robot? We need a robot?
no team
 
Join Date: Sep 2004
Rookie Year: 1234
Location: n/a
Posts: 359
devicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nice
Re: System Clock?

Quote:
Originally Posted by sciguy125
Programming isn't my field of expertise, so correct me if I'm wrong, but isn't user_routines (or whatever that user function is called) called every 26.5ms? If so, couldn't you just count how many time's it's been called. Increment some variable or something. I'm not sure how exact the time is on it though...

It's not 26.2ms, unless my conversion was wrong..
I had
(counter*26.2)/1000 or..maybe it was 10000
Which should give me seconds.. but didn't. Not too sure what was going on there, but it didn't seem that accurate
  #7   Spotlight this post!  
Unread 11-02-2005, 13:29
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: System Clock?

Quote:
Originally Posted by devicenull
It's not 26.2ms, unless my conversion was wrong..
I had
(counter*26.2)/1000 or..maybe it was 10000
Which should give me seconds.. but didn't. Not too sure what was going on there, but it didn't seem that accurate
If you're going to use the loop time to keep track of elapsed time, you'll need to check the packet number, and account for missed packets (loops).

What happens is that if your code is too slow, it might not make it back in time to read the very next data packet, and thus might miss a loop.

What you need to do is to keep track of the packet number of the last data packet you read, and multiply the difference between that number and the current packet number by 26.2 before adding to your match timer.

BTW, DON'T multiply by 26.2 (a float). DO multiply by 262, and simply consider your match timer to be in units of 10ths of milliseconds instead of milliseconds. The same goes for any other floating point operations.

You need to realize that the PIC processor does not natively support floating point operations, and any floating point calculations you request will be performed in software -- which takes up more code space, AND takes MUCH longer. IF you are currently using floats, this COULD be why you are missing data packets, and why your timer was not accurate.

Better yet than multiplying by 262 is to not multiply at all! Just leave your timer in units of 26.2ms clock ticks. (You still need to account for missed packets.)

OK. this post is getting too long. If you need more details, just ask. (Or search. I'm pretty sure this has all been discussed before. )
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me

Last edited by Greg Ross : 11-02-2005 at 13:31.
  #8   Spotlight this post!  
Unread 11-02-2005, 18:06
devicenull devicenull is offline
Robot? We need a robot?
no team
 
Join Date: Sep 2004
Rookie Year: 1234
Location: n/a
Posts: 359
devicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nice
Re: System Clock?

Quote:
Originally Posted by gwross
If you're going to use the loop time to keep track of elapsed time, you'll need to check the packet number, and account for missed packets (loops).

What happens is that if your code is too slow, it might not make it back in time to read the very next data packet, and thus might miss a loop.

What you need to do is to keep track of the packet number of the last data packet you read, and multiply the difference between that number and the current packet number by 26.2 before adding to your match timer.

BTW, DON'T multiply by 26.2 (a float). DO multiply by 262, and simply consider your match timer to be in units of 10ths of milliseconds instead of milliseconds. The same goes for any other floating point operations.

You need to realize that the PIC processor does not natively support floating point operations, and any floating point calculations you request will be performed in software -- which takes up more code space, AND takes MUCH longer. IF you are currently using floats, this COULD be why you are missing data packets, and why your timer was not accurate.

Better yet than multiplying by 262 is to not multiply at all! Just leave your timer in units of 26.2ms clock ticks. (You still need to account for missed packets.)

OK. this post is getting too long. If you need more details, just ask. (Or search. I'm pretty sure this has all been discussed before. )

It wasn't working right, and didn't seem to be a good way of doing it, so I didn't. That was just supposed to be until my team got the sensors I needed mounted.
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
Multiple Auton's Joe Clohessy Programming 17 11-02-2005 18:46
Building a Clock with Periodic Motion IMDWalrus Math and Science 15 24-03-2004 16:48
What do you wish you knew about the new control system? Joe Ross Control System 2 09-01-2004 21:47
control system worth more than $500 archiver 2001 8 24-06-2002 02:00
New Innovation FIRST control system and the dashboard archiver 2000 0 23-06-2002 22:15


All times are GMT -5. The time now is 13:04.

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