Go to Post We all need to remember to be careful at these competitions. Danger lies in the most unexpected (and comical) places. - Alexander McGee [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 17-01-2005, 17:32
pickle's Avatar
pickle pickle is offline
Talking vegtable
AKA: Jeremy
#1296 (Full Metal Jackets)
Team Role: Programmer
 
Join Date: Apr 2004
Rookie Year: 2004
Location: Rockwall,TX
Posts: 42
pickle is on a distinguished road
Send a message via AIM to pickle
Code violation using Hall effect sensor

I need some serious help here, so, I have been using the Hall effect sensors to count gear teeth and get an RPS for the wheels. It works pretty well. I was told to check the sensor every millisecond, so I set an interrupt to fire every millisecond and do a little math.

Code:
	// this function will be called when a timer 2 interrupt occurs

	static unsigned char lastHighL = 0; //The last value seen was a high
	static unsigned char lastLowL = 0; //The last value seen was a low
	static unsigned char lastHighR = 0; //The last value seen was a high
	static unsigned char lastLowR = 0; //The last value seen was a low
 	msClock++; // increment the millisecond clock
	if(msClock > 0 && msClock % 26 ==0)
		return;
	
	//Count the changed between highs and lows	

	if(Get_Analog_Value(rc_ana_in01) > 900 && lastLowL)
	{
		lastHighL=1;
		lastLowL=0;
		leftTicks++;
	}
	else if(Get_Analog_Value(rc_ana_in01) < 100 && lastHighL)
	{
		lastHighL=0;
		lastLowL=1;
	}
	else if(!lastHighL && !lastLowL && Get_Analog_Value(rc_ana_in01) > 900)
	{
		lastHighL=1;
		lastLowL=0;
	}
	else if(!lastHighL && !lastLowL && Get_Analog_Value(rc_ana_in01) < 100)
	{
		lastHighL=0;
		lastLowL=1;
	}



	if(Get_Analog_Value(rc_ana_in02) > 900 && lastLowR)
	{
		lastHighR=1;
		lastLowR=0;
		rightTicks++;
	}
	else if(Get_Analog_Value(rc_ana_in02) < 100 && lastHighR)
	{
		lastHighR=0;
		lastLowR=1;
	}
	else if(!lastHighR && !lastLowR && Get_Analog_Value(rc_ana_in02) > 900)
	{
		lastHighR=1;
		lastLowR=0;
	}
	else if(!lastHighR && !lastLowR && Get_Analog_Value(rc_ana_in02) < 100)
	{
		lastHighR=0;
		lastLowR=1;
	}	

	if(leftTicks == GEAR_TEETH)
	{
		leftTicks=0;
		leftRevo++;
	}
	if(rightTicks == GEAR_TEETH)
	{
		rightTicks=0;
		rightRevo++;
	}

	//Every second, increment secClock
	if(msClock%1000 == 0 && msClock>0)
	{
		secClock++;
	}

	//Calculate RPS every 4 seconds
	if(secClock>0 && secClock % 4 ==0)
	{
		leftRPS=leftRevo/secClock;
		rightRPS=rightRevo/secClock;
	}
This is what I have going right now in that interrupt, most of it is just incrementing variables and such, so it shouldn't be too much, though I am worried that the division is taking too long. Is there anything I can do to optimze this? Or am I overlooking something? I told the interrupt to return immediately after starting if the number of milliseconds is divisible by 26, so every 26ms, return without doing any processing (I'm concerned that all that code may take more than .2 ms, and I think the 1ms check should be more than enough to skip once without too many detrimental effects, though I could be wrong). I then plugged it into the dashboard to try and get more details, as the manual suggests, and it tells me "User violation: Invalid CONTROL or CURRENT_MODE byte". Any clue what that means? I sure have no clue....... Any help would be wonderful, or ideas, or better ideas. :'( :'( :'( :'( I'm going nuts, and I have an angry teacher glaring at me and breathing down my neck...... That would be grand if any of you can help
__________________
2004 Rookie Inspiration Award winner - Midwest Regional
2004 Autodesk Visualization Award winner - Lone Star Regional
2004 Quarterfinalist - Lone Star Regional
2005 Team Spirit Award - Lone Star Regional
  #2   Spotlight this post!  
Unread 17-01-2005, 18:08
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Code violation

Pickle,


Use the Hall sensor as a digital input and interrupt on the rising edge.

I strongly suggest that you use Kevin Watson's RC encoder example as a guide. Just eliminate the part where he looks at the quadrature input to determine direction.
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #3   Spotlight this post!  
Unread 17-01-2005, 18:28
pickle's Avatar
pickle pickle is offline
Talking vegtable
AKA: Jeremy
#1296 (Full Metal Jackets)
Team Role: Programmer
 
Join Date: Apr 2004
Rookie Year: 2004
Location: Rockwall,TX
Posts: 42
pickle is on a distinguished road
Send a message via AIM to pickle
Re: Code violation

Perhaps my problem is not having a good grasp on how to set up interrupts. I understand how they work, but I'm not entirely sure on setting them up.... I've read the innovationfirst documentation on them, and I've looked at kevin's code, but its still not very clear. Any places I can get help on them? This may help solve my problem
__________________
2004 Rookie Inspiration Award winner - Midwest Regional
2004 Autodesk Visualization Award winner - Lone Star Regional
2004 Quarterfinalist - Lone Star Regional
2005 Team Spirit Award - Lone Star Regional
  #4   Spotlight this post!  
Unread 17-01-2005, 20:01
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Code violation

Quote:
Originally Posted by pickle
Perhaps my problem is not having a good grasp on how to set up interrupts. I understand how they work, but I'm not entirely sure on setting them up.... I've read the innovationfirst documentation on them, and I've looked at kevin's code, but its still not very clear. Any places I can get help on them? This may help solve my problem
Jeremy,

Not an easy answer here... I have met quite a few degreed engineers who did not understand the intricacies of interrupt timing, latency, context saving, priorities, et cetera.

The important thing to remember is that an interrupt is like a separate program which runs on your processor. It can occur at any time, therefore it must have it's own variables, do its thing, come to completion quickly and give control back to your background process.

Any variables it uses which are shared with the background process must be treated very carefully. Read the timers white paper from the IFI site for a fairly good side by side comparison of implementing a timer with and without interrupts.

Meanwhile, feel free to post specific questions here and I (or other knowledgeable people) will try to help you.
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #5   Spotlight this post!  
Unread 17-01-2005, 21:56
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: Code violation

Quote:
Originally Posted by Mike Betts
Pickle,


Use the Hall sensor as a digital input and interrupt on the rising edge.

I strongly suggest that you use Kevin Watson's RC encoder example as a guide. Just eliminate the part where he looks at the quadrature input to determine direction.
Instead of looking at the encoder phase-b signal, you could look at the motor's commanded direction and increment/decrement the counters as necessary. One problem that comes to mind, though, is what is sometimes the robot won't have a commanded direction when the interrupt fires off. You may be able to get around this by keeping track of the last commanded direction in a state variable.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #6   Spotlight this post!  
Unread 17-01-2005, 22:17
pickle's Avatar
pickle pickle is offline
Talking vegtable
AKA: Jeremy
#1296 (Full Metal Jackets)
Team Role: Programmer
 
Join Date: Apr 2004
Rookie Year: 2004
Location: Rockwall,TX
Posts: 42
pickle is on a distinguished road
Send a message via AIM to pickle
Re: Code violation using Hall effect sensor

Thank you all so much, I'll try this out tomorrow and see if that helps. I really think I was doing too much during that interrupt handler. The part of interrupts I don't understand is the setup, I don't see any clear documentation on that. One of our software engineers explained how they work and everything, he specializes in integrated control systems and deals with PIC processors alot, it was just the syntax of setting one up that seems to get me. If perhaps someone could show the setup code, then expain what each line of it does , and how that changes for each interrupt, thats I think what I'm looking for. I mean, I can copy and paste code just fine, but I really want to learn it, not just smile and not know whats going on.

But I will try the digital thing, the thing is, I need both an accurate timer, and the counter, thats not too tolling on the processor is it? And what happens if an interrupt is raised while you're in an interrupt handler? I figure its possible, do I miss that tooth, or does it interrupt the interrupt handler? Ok, thats all for now.
__________________
2004 Rookie Inspiration Award winner - Midwest Regional
2004 Autodesk Visualization Award winner - Lone Star Regional
2004 Quarterfinalist - Lone Star Regional
2005 Team Spirit Award - Lone Star Regional
  #7   Spotlight this post!  
Unread 17-01-2005, 22:45
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Code violation using Hall effect sensor

Quote:
Originally Posted by pickle
...But I will try the digital thing, the thing is, I need both an accurate timer, and the counter, thats not too tolling on the processor is it? And what happens if an interrupt is raised while you're in an interrupt handler? I figure its possible, do I miss that tooth, or does it interrupt the interrupt handler?...
Jeremy,

Interrupts are nested. While you are servicing one, the next will wait for you to be done unless you are doing prioritized interrupts. In that case, the higher priority will interrupt the lower priority. This is why Kevin's code disables interrupts within the handler. Bottom line, you will not miss an interrupt unless you try doing too much within the interrupt OR try servicing too many interrupts.

As to your question on "tolling". This is very implementation specific. How often does the timer interrupt? how many teeth and how fast does the gear tooth sensor need to service? And lastly, how much code is implemented within the interrupt? We can not answer this for you. It needs to be derived numerically or verified experimentally.

If you have a competent mentor, ask him or her. I'm sure they will be glad to help (depending on how busy they are).
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #8   Spotlight this post!  
Unread 17-01-2005, 23:43
pickle's Avatar
pickle pickle is offline
Talking vegtable
AKA: Jeremy
#1296 (Full Metal Jackets)
Team Role: Programmer
 
Join Date: Apr 2004
Rookie Year: 2004
Location: Rockwall,TX
Posts: 42
pickle is on a distinguished road
Send a message via AIM to pickle
Re: Code violation using Hall effect sensor

Thanks so much, you have helped me immensely, again, I'll try this out tomorrow and see how this all goes.
__________________
2004 Rookie Inspiration Award winner - Midwest Regional
2004 Autodesk Visualization Award winner - Lone Star Regional
2004 Quarterfinalist - Lone Star Regional
2005 Team Spirit Award - Lone Star Regional
  #9   Spotlight this post!  
Unread 18-01-2005, 23:49
pickle's Avatar
pickle pickle is offline
Talking vegtable
AKA: Jeremy
#1296 (Full Metal Jackets)
Team Role: Programmer
 
Join Date: Apr 2004
Rookie Year: 2004
Location: Rockwall,TX
Posts: 42
pickle is on a distinguished road
Send a message via AIM to pickle
Re: Code violation using Hall effect sensor

It worked !!!!! Thanks so much! You guys rock!
__________________
2004 Rookie Inspiration Award winner - Midwest Regional
2004 Autodesk Visualization Award winner - Lone Star Regional
2004 Quarterfinalist - Lone Star Regional
2005 Team Spirit Award - Lone Star Regional
  #10   Spotlight this post!  
Unread 19-01-2005, 00:10
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: Code violation using Hall effect sensor

Quote:
Originally Posted by pickle
It worked !!!!! Thanks so much! You guys rock!
What works?

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
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
Hall Effect Sensor. Nixterrimus Electrical 77 29-01-2005 10:48
Coding / Style Standards for sharing C code Joe Johnson Programming 33 01-05-2004 15:15
heres the code. y this not working omega Programming 16 31-03-2004 15:18
Strange IR sensor movement with default tracker code wun Programming 5 19-03-2004 10:51
Infrared Beacon and Sensor Diagnostics Code Released Kevin Watson Programming 3 25-02-2004 17:50


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

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