Go to Post What am I going to do with all of these torches and pitchforks now? - PayneTrain [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 10-03-2007, 01:35
qnetjoe qnetjoe is offline
Registered User
AKA: Joe Daily
no team
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2003
Location: Austin
Posts: 51
qnetjoe is on a distinguished road
Send a message via AIM to qnetjoe Send a message via MSN to qnetjoe Send a message via Yahoo to qnetjoe
Encoder Help with VEX and MPLAB

Greetings,

I am trying to set up some reference encoder for my team. We are using MPLAB with Vex Controller.

Is there a way that I can get the interrupt to be triggered on the raising or falling edge of the resulting encoder square wave?

Right now my code (below) just looks to see if the signal is high (0) or low (1), but if the encoder happens to stop when the signal is high , the encoder count just runs.

Code:
if(rc_dig_int1)
{
encoder1++;
PORTBbits.RB2=0; //Set Interrupt = 0
}
Thanks
  #2   Spotlight this post!  
Unread 10-03-2007, 01:45
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,187
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: Encoder Help with VEX and MPLAB

Kevin Waston provides some modular code for setting up encoders on the IFI hardware. The EDU hardware is closest to Vex, so I'd download that workspace. Any of the code can be easily ported to work with Vex, so I guess it doesnt matter.

That code will work as long as rc_dig_int1 is defined as the interrupt flag you want to look at and it is placed in the interrupt handler found in user_routines_fast.c. I'm guessing you are looking directly at the input pin, which is causing the code inside the If to execute every time.

Last edited by Tom Bottiglieri : 10-03-2007 at 01:47.
  #3   Spotlight this post!  
Unread 10-03-2007, 05:24
UnderDark's Avatar
UnderDark UnderDark is offline
Alumni
AKA: Matthew Korzeniowski
FRC #0862 (Lightning Robotics)
Team Role: Alumni
 
Join Date: Mar 2006
Rookie Year: 2004
Location: Canton, Michigan
Posts: 14
UnderDark will become famous soon enough
Re: Encoder Help with VEX and MPLAB

The VEX encoders send a binary string. You should only increment distance when the latest value is different from the previous value.

Code:
unsigned int distance = 0;
char encOld = 0;
.
.
.
if((char)rc_dig_int1 != encOld)
   distance++;
encOld = rc_dig_int1;
__________________
"My gaming is FIRST!"
-Joe Jagadics

"I am like an all powerful warloard"
-Brian Graham
  #4   Spotlight this post!  
Unread 10-03-2007, 16:37
qnetjoe qnetjoe is offline
Registered User
AKA: Joe Daily
no team
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2003
Location: Austin
Posts: 51
qnetjoe is on a distinguished road
Send a message via AIM to qnetjoe Send a message via MSN to qnetjoe Send a message via Yahoo to qnetjoe
Re: Encoder Help with VEX and MPLAB

Thank you that worked great
  #5   Spotlight this post!  
Unread 10-03-2007, 19:44
Tom Saxton's Avatar
Tom Saxton Tom Saxton is offline
Registered User
no team (Issaquah Robotics Society)
Team Role: Mentor
 
Join Date: Dec 2003
Rookie Year: 2003
Location: Sammamish, WA
Posts: 98
Tom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud ofTom Saxton has much to be proud of
Re: Encoder Help with VEX and MPLAB

Quote:
Originally Posted by UnderDark View Post
The VEX encoders send a binary string. You should only increment distance when the latest value is different from the previous value.

Code:
unsigned int distance = 0;
char encOld = 0;
.
.
.
if((char)rc_dig_int1 != encOld)
   distance++;
encOld = rc_dig_int1;
This code isn't using an interrupt handler, and so it can miss transitions. If you want to use an interrupt handler to count ticks as they come in, here are some instructions on how to modify the default code. The following assumes that you're hooking up two encoders, right side connected to interrupt pin 1 and the left side connected to interrupt pin 2. It should be clear how to modify things if your setup is different. Using the higher numbered interrupt pins is a bit more work since they used a share interrupt instead of the nice dedicated interrupts for pins 1 and 2.

To get the encoder values, just look at the two globals. It would be better to wrap that lookup in code that disable interrupts, so you don't risk having the value change in the middle of reading it. I can post that bit of code later, but I need to run right now.

user_routines.c
---------------

in the function "User_Initialization" add this line:

Code:
  EnableInterrupts();
user_routines.h
---------------

in the Function Prototypes section, add these lines:

Code:
extern volatile int g_distMotorLeft;
extern volatile int g_distMotorRight;
void EnableInterrupts(void);
user_routines_fast.c
--------------------

below this line:

/*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/

add these lines:

Code:
volatile int g_distMotorLeft = 0;
volatile int g_distMotorRight = 0;
replace the entire InterruptHandlerLow function with a new version and two more functions:

Code:
void InterruptHandlerLow ()     
{                               
	unsigned char int_byte;       
	if (INTCON3bits.INT2IF && INTCON3bits.INT2IE)       /* The INT2 pin is RB2/DIG I/O 1. */
	{ 
		INTCON3bits.INT2IF = 0;
		++g_distMotorRight;
	}
	else if (INTCON3bits.INT3IF && INTCON3bits.INT3IE)  /* The INT3 pin is RB3/DIG I/O 2. */
	{
		INTCON3bits.INT3IF = 0;
		++g_distMotorLeft;
	}
	else if (INTCONbits.RBIF && INTCONbits.RBIE)  /* DIG I/O 3-6 (RB4, RB5, RB6, or RB7) changed. */
	{
		int_byte = PORTB;          /* You must read or write to PORTB */
		INTCONbits.RBIF = 0;     /*     and clear the interrupt flag         */
	}                                        /*     to clear the interrupt condition.  */
}

void ResetEncoders()
{
	g_distMotorLeft = 0;
	g_distMotorRight = 0;
}

void EnableInterrupts()
{
	// interrupt 2, which is pin 1
	TRISBbits.TRISB2 = 1; // make sure pin is configured as an input
	INTCON3bits.INT2IP = 0; // interrupt is low priority
	INTCON2bits.INTEDG2 = 1; // trigger on rising edge
	INTCON3bits.INT2IF = 0; // make sure interrupt flag is clear before enabling
	INTCON3bits.INT2IE = 1; // enable the interrupt

	// interrupt 3, which is pin 2
	TRISBbits.TRISB3 = 1; // make sure pin is configured as an input
	INTCON2bits.INT3IP = 0; // interrupt is low priority
	INTCON2bits.INTEDG3 = 1; // trigger on rising edge
	INTCON3bits.INT3IF = 0; // make sure interrupt flag is clear before enabling
	INTCON3bits.INT3IE = 1; // enable the interrupt
}
__________________
Tom Saxton
http://www.idleloop.com/
  #6   Spotlight this post!  
Unread 11-03-2007, 21:04
qnetjoe qnetjoe is offline
Registered User
AKA: Joe Daily
no team
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2003
Location: Austin
Posts: 51
qnetjoe is on a distinguished road
Send a message via AIM to qnetjoe Send a message via MSN to qnetjoe Send a message via Yahoo to qnetjoe
Re: Encoder Help with VEX and MPLAB

How would do you make encoders work with interrupt pins 3-6?

I plan on allowing the code to utilize 4 encoders and 2 ultra-sonics.

Thanks in advance
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
Help with MPLAB burningpig Programming 9 17-01-2007 13:10
Kevin Watson and programming VEX in MPLAB Doug Leppard Programming 4 24-08-2006 21:05
Vex Programming with MPLAB and IF loader Joohoo Programming 3 27-07-2006 18:18
VEX UltraSonic and Encoder SOLD OUT... Chris_Elston Electrical 5 09-01-2006 13:22
Need Help with Encoder - Won't Count Clicks Kingofl337 Programming 5 16-02-2005 18:30


All times are GMT -5. The time now is 21:24.

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