Go to Post If you smack the bear with a stick, you'd better be ready for a mauling. - sciguy125 [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 22-03-2004, 01:31
CharlieWilken CharlieWilken is offline
Registered User
#0634 (Robowolves)
 
Join Date: Jan 2002
Location: Van Nuys, CA
Posts: 46
CharlieWilken is an unknown quantity at this point
counting digital input

How do I count digital input ticks?

Im trying to get our autonomous mode to work before our regional this Thursday. Im using the banner optical switches to count ticks on my wheel that I painted on with reflective paint. We have tank style right and left side steering. I estimate I want to move 43 ticks forwared and turn say right with 4 ticks on one side and 1 tick on the other. I then want to move both sides forward 3 ticks. I figure from read chief delphi I can do this using 3 "states" Forward_long, turn and forward_short. However I dont know much C just enough to cut and paste or substitute.

1, How do I make a counter that keeps track of the number of ticks the digital input gets.
2. Where do I put the code for the timer? Where do I put the code for the states?

thanks in advance!
  #2   Spotlight this post!  
Unread 22-03-2004, 07:05
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: counting digital input

Either use polling, meaning you check for a change in state of the digital input every loop, or use interrupts. As you don't have much time, I would recommend the polling, as it can be done easily with no bugs.

For the polling you would have something like this:
Code:
// This is the output that anyone can read. Rename it to something better and more descriptive
int ticks = 0;

void checkDigitalIn(void)
{
    // This is used to tell us when we change states
    static char lastState = 0;

    // This does the actual checking. Make the dig_in_01 whatever input you are working with.
    if(lastState != dig_in_01)
    {
         ticks++;
         // This marks the lastState as the opposite of itself, meaning it now equals the digital input
         lastState = !lastState;
    }
}
That's all there is to it. Just call that as often as you can in your and when it sees the input cange from high to low or low to high, it will increment the global variable.

--EDIT--
If you want it to only catch one type of state change (IE low to high), then have it check if it has reached the state you want it to before it increments.
__________________

  #3   Spotlight this post!  
Unread 22-03-2004, 09:28
CharlieWilken CharlieWilken is offline
Registered User
#0634 (Robowolves)
 
Join Date: Jan 2002
Location: Van Nuys, CA
Posts: 46
CharlieWilken is an unknown quantity at this point
Re: counting digital input

Thanks Texan

And which file.c do I put this code in??
And how could I do say a case for autonomous_1 where I go left and autonomous_2 where I go right useing the digital input. And once again which file.c do I put it in?
  #4   Spotlight this post!  
Unread 22-03-2004, 09:43
CrashZero's Avatar
CrashZero CrashZero is offline
Computer Nerd
#1352 (Huskie Robotics)
Team Role: Programmer
 
Join Date: Mar 2004
Location: Stratford Northwestern
Posts: 44
CrashZero will become famous soon enoughCrashZero will become famous soon enough
Lightbulb Re: counting digital input

All Autonomous Code goes in user_routines_fast.c
__________________
This is our world now... the world of the electron and the switch, the beauty of the baud. We make use of a service already existing without paying for what could be dirt-cheap if it wasn't run by profiteering gluttons, and you call us criminals. We explore... and you call us criminals. We seek after knowledge... and you call us criminals. We exist without skin color, without nationality, without religious bias... and you call us criminals. You build atomic bombs, you wage wars, you murder, cheat, and lie to us and try to make us believe it's for our own good, yet we're the criminals. Yes, I am a criminal. My crime is that of curiosity. My crime is that of judging people by what they say and think, not what they look like. My crime is that of outsmarting you, something that you will never forgive me for. I am a hacker, and this is my manifesto. You may stop me, but you can't stop us all...

quote from:

+++ The Mentor +++
  #5   Spotlight this post!  
Unread 22-03-2004, 14:13
CharlieWilken CharlieWilken is offline
Registered User
#0634 (Robowolves)
 
Join Date: Jan 2002
Location: Van Nuys, CA
Posts: 46
CharlieWilken is an unknown quantity at this point
Re: counting digital input

Thanks CrashZero!!!

So my last question is how to I put a switch on my digital inputs so that I have one that turns left and one that turns right etc. I imagine it is some sort of If statement but I dont know how to put all the brackets, parenthesis etc


If digitalinput15=0 then autonomous_1
else digital input16=0 then autonomous_2
else end
something like this?

and do i put the counter in each automous module?
each of my autonomous modes has three states, forwardstate
turnstate and forwardshortstate. How do I group these together with regards to brackets, parenthesis into autonomous_1 and autonomous_2?

thanks again!!!
  #6   Spotlight this post!  
Unread 22-03-2004, 14:32
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: counting digital input

Quote:
Originally Posted by CharlieWilken
So my last question is how to I put a switch on my digital inputs so that I have one that turns left and one that turns right etc. I imagine it is some sort of If statement but I dont know how to put all the brackets, parenthesis etc
What do you mean by turns left and turns right? If you are saying that these switches are telling the robot which side of the field it is starting on/you want it to start off going straight if both switches are off (IE one switch on is right, other switch on is left, and both off is straight, do something like:
Code:
if(dig_in_10 == 1)
{
    // Right Mode
}
else if(dig_in_ 11 == 1)
{
    // Left mode
}
else
{
    // Straight mode
}
Quote:
Originally Posted by CharlieWilken
and do i put the counter in each automous module?
each of my autonomous modes has three states, forwardstate
turnstate and forwardshortstate. How do I group these together with regards to brackets, parenthesis into autonomous_1 and autonomous_2?

thanks again!!!
You just need to put it where it is being called no matter what autonomous mode you are in, so if that means calling it from multiple places, do it. Put it in whereever you will be sure it will be called often, no matter what mode you are in. It doesn't matter if it is called faster than the ticks are.
__________________

  #7   Spotlight this post!  
Unread 22-03-2004, 16:00
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
Exclamation Re: counting digital input

None of that is going to work if you're going at any resonable speed. It's going to miss ticks. Use interupts.

Here's some of my code:
Code:
//Variables
volatile char State = 0;
volatile long Distance = 0;
volatile signed char Dir = 0;
//...

//Call while initializing
void Initialize_Interrupts(void)
{
	// initialize external interrupt 1 (INT2 on user 18F8520)
	TRISBbits.TRISB2 = 1;		// make sure the RB2/INT2 pin is configured as an input [108]
								//
	INTCON3bits.INT2IP = 0;		// 0: interrupt 1 is low priority (leave at 0 for IFI controllers) [91]
								// 1: interrupt 1 is high priority
								//
	INTCON2bits.INTEDG2 = 0;	// 0: trigger on the falling-edge [90]
								// 1: trigger on the rising-edge
								//
	INTCON3bits.INT2IE = 0;		// 0: disable interrupt	1 [91]
								// 1: enable interrupt 1
	
	// initialize external interrupt 2 (INT3 on user 18F8520)
	TRISBbits.TRISB3 = 1;		// make sure the RB3/CCP2/INT3 pin is configured as an input [108]
								//
	INTCON2bits.INT3IP = 0;		// 0: interrupt 2 is low priority (leave at 0 for IFI controllers) [90]
								// 1: interrupt 2 is high priority
								//
	INTCON2bits.INTEDG3 = 0;	// 0: trigger on the falling-edge [90]
								// 1: trigger on the rising-edge
								//
	INTCON3bits.INT3IE = 0;		// 0: disable interrupt	2 [91]
								// 1: enable interrupt 2

	// initialize external interrupts 3-6 (KBI0 - KBI3 on user 18F8520)
	TRISBbits.TRISB4 = 1;		// make sure the RB4/HBI0 pin is configured as an input [108]
	TRISBbits.TRISB5 = 1;		// make sure the RB5/KBI1/PGM pin is configured as an input [108]
	TRISBbits.TRISB6 = 1;		// make sure the RB6/KBI2/PGC pin is configured as an input [108]
	TRISBbits.TRISB7 = 1;		// make sure the RB7/KBI3/PGD pin is configured as an input	[108]
								//
  	INTCON2bits.RBIP = 0;		// 0: interrupts 3 through 6 are low priority (leave at 0 for IFI controllers) [90]
								// 1: interrupts 3 through 6 are high priority
								//
	Old_Port_B = PORTB;			// initialize the Old_Port_B variable (in user_routines_fast.c)
								//
	INTCONbits.RBIE = 1;		// 0: disable interrupts 3 through 6 [89]
								// 1: enable interrupts 3 through 6
}						  
//...

//The ISR Routine
void Int_3_Handler(unsigned char RB4_State)
{
 // wheel encoder Phase A interrupts.
 if (!ShaftA==ShaftB)
 {
  Dir = 1;
 }
 if (ShaftA==ShaftB)
 {
  Dir = -1;
 }
 Distance+=Dir; // Increment

 switch (State) {
  case 0: // A=0, B=0
   State = 1;
   break;
  case 1: // A=1, B=0
   State = 0;
   break;
  case 2: // A=1, B=1
   State = 3;
   break;
  case 3: // A=0, B=1
  State = 2;
   break;
 } // switch (State)
}
It's based on Kevin's interupts stuff and the Quadrature whitepaper. Hope you can figure it out!
  #8   Spotlight this post!  
Unread 22-03-2004, 16:06
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: counting digital input

Quote:
Originally Posted by Astronouth7303
None of that is going to work if you're going at any resonable speed. It's going to miss ticks. Use interupts.
True, it may not be the most reliable thing in the world, but it is simple. Plus, if you do the polling from outside the 26.2ms part, you shouldn't have many problems. I went with polling after another kid on our controls team couldn't figure the interrupts out and we haven't had problems yet.

But, I agree with you. Use interrupts if you can.
__________________

  #9   Spotlight this post!  
Unread 22-03-2004, 16:36
seanwitte seanwitte is offline
Registered User
None #0116
Team Role: Engineer
 
Join Date: Nov 2002
Location: Herndon, VA
Posts: 378
seanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant future
Send a message via AIM to seanwitte
Re: counting digital input

Quote:
Originally Posted by Astronouth7303
None of that is going to work if you're going at any resonable speed. It's going to miss ticks. Use interupts.
If you use a quadrature encoder you don't need the state machine to decode its output. Connect channel A to an interrupt configured to fire on the rising edge. Connect channel B to a regular digital input (input 10 in the example). Your ISR becomes one line:
Code:
void Int_1_Handler()
{
     Distance += (rc_dig_in10) ? 1 : -1;
}
The outputs follow the pattern AB = 00-10-11-01-00... If you look at the rising edges for channel A, going from left to right, channel B is 0. Looking at the rising edge of A from the other direction and channel B is 1. If the direction is reversed you can swap the inputs or change the signs in the ISR.

Last edited by seanwitte : 22-03-2004 at 16:45.
  #10   Spotlight this post!  
Unread 22-03-2004, 19:56
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: counting digital input

Sorry, I just copied and pasted. I don't think I even use it.
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
digital inputs not valid just after initialization? WizardOfAz Programming 2 26-01-2004 13:21
Some EDURobotics Problems squide Robotics Education and Curriculum 9 20-01-2004 10:27
Help On Coding 2K1 Controller GregTheGreat Programming 9 05-12-2003 18:35
How do u hook up the digital input to the sensor's wires Fares Technical Discussion 2 14-02-2003 16:41
Robot Digital Input Mike Norton Technical Discussion 5 16-01-2003 14:31


All times are GMT -5. The time now is 02:33.

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