Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   GearTooth Sensor Destroying Hopes of Autonomous~!!!!!! (http://www.chiefdelphi.com/forums/showthread.php?t=44428)

Denz 25-02-2006 22:05

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
How could I modify Kevin's Encoder code so I could use it with Gear Tooth Sensors, I'm a programming rookie, and I'm really having a tough time with these! :( I don't really have a way to test, except I have a switch hooked up to the proper dig inputs, which is supposed to represent 1 gear being counted everytime I press it. My counter code works with the switch, but I'm afraid that the gears will be moving too fast for a simple counter code to work.

jgannon 26-02-2006 12:12

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Quote:

Originally Posted by Denz
How could I modify Kevin's Encoder code so I could use it with Gear Tooth Sensors, I'm a programming rookie, and I'm really having a tough time with these! :( I don't really have a way to test, except I have a switch hooked up to the proper dig inputs, which is supposed to represent 1 gear being counted everytime I press it. My counter code works with the switch, but I'm afraid that the gears will be moving too fast for a simple counter code to work.

Kevin Watson's encoder code will work with the gear tooth sensors with virtually no modification at all. If you drive the robot too fast, you will start to get weird readings, but if you drive at a reasonable pace, his code will work just fine.

Alan Anderson 26-02-2006 14:17

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Quote:

Originally Posted by Denz
How could I modify Kevin's Encoder code so I could use it with Gear Tooth Sensors,...

The necessary change is trivial. Just take out the piece of the encoder interrupt service routine that checks the "B" phase, so that it always increments the counter when it gets an interrupt. Or, if you like, you can get fancy and read the motor pwm value to decide whether to increment or decrement the couter.
Quote:

My counter code works with the switch, but I'm afraid that the gears will be moving too fast for a simple counter code to work.
It's not so much the speed you need to worry about, but the narrow pulse width from the GTS. It's only active for a few tens of microseconds each time a tooth goes by, so reading its value in the Process_Data_From_Master_uP() loop is not likely to see it reliably. You really need to have it trigger an interrupt.

Kevin Watson 26-02-2006 15:56

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Quote:

Originally Posted by Denz
How could I modify Kevin's Encoder code so I could use it with Gear Tooth Sensors, I'm a programming rookie, and I'm really having a tough time with these! :( I don't really have a way to test, except I have a switch hooked up to the proper dig inputs, which is supposed to represent 1 gear being counted everytime I press it. My counter code works with the switch, but I'm afraid that the gears will be moving too fast for a simple counter code to work.

Use encoder inputs one and two and change this code:

Code:

void Encoder_1_Int_Handler(void)
{
// Encoder 1's phase a signal just transitioned from zero to one, causing
// this interrupt service routine to be called. We know that the encoder
// just rotated one count or "tick" so now check the logical state of the
// phase b signal to determine which way the the encoder shaft rotated.
if(ENCODER_1_PHASE_B_PIN == 0)
{
Encoder_1_Count -= ENCODER_1_TICK_DELTA;
}
else
{
Encoder_1_Count += ENCODER_1_TICK_DELTA;
}
}

Into this:

Code:

void Encoder_1_Int_Handler(void)
{
Encoder_1_Count += ENCODER_1_TICK_DELTA;
}

Repeat the above for channel two.

If you want to change the direction the GTS counts, change the ENCODER_1_TICK_DELTA or ENCODER_2_TICK_DELTA value to -1 in encoder.h.

-Kevin

Denz 26-02-2006 22:22

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Thanks! I'll test it on my temporary rig, and report back :) hopefully it works on the robot aswell, lol, considering I'm using a processor and a switch hooked up to the dig inputs.

EDIT:
If the GTS is stuck on a gear, does it's value stay at 0?

EDIT:
I must have done something wrong. I can see that the switch is being activated, but the count stays at 0.

I called all functions in autonomous mode and I modified this part of the code:

void Encoder_1_Int_Handler(void)
{
if(ENCODER_1 == 0)
{
Encoder_1_Count++;
}
printf("Encoder: %d", Encoder_1_Count);
printf("Switch: %d", rc_dig_in06);
}
Encoder_1 is rc_dig_in06

I read the readme, and I assume I did everything correctly.

Please keep in mind that this is my first year programming, and I'm most probably overlooking something obvious...

ericand 27-02-2006 04:12

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
The default encoder code from Kevin is great for seeing the interrupts
work. I would run that code (unmodified) on a test system and look at
the way it responds to interrupts generated by hand using a switch.
The default encoder code will print out the counts for the enabled interrupts
on a regular interval.

We wired up a bunch of switches and put them on digital I/O 1-6 to see
how they worked.

What digital I/O pins do you have the GTS wired to?
Your printf seems to say that the switch is on rc_dig_in06 and
you are expecting encoder_1_count to increment. That will not work.

You should not be trying to print from the interrupt handler. Printing takes
way too long to be done in the interrupt context.

We also used a switch to simulate the GTS input when we were developing
code on a test bed, so you should be able to get it to work.

You need to remember that all the interrupts are on the digital I/O pins 1-6.
encoder_1_count in Kevin's code refers to the count of interrupts on pin 1.
I encourage you to use digital I/O 1 and 2 for your GTS. For reasons why,
search the forum for posts on the difference between edge triggered and
level triggered interrupts.

Quote:

Originally Posted by Denz
Thanks! I'll test it on my temporary rig, and report back :) hopefully it works on the robot aswell, lol, considering I'm using a processor and a switch hooked up to the dig inputs.

EDIT:
If the GTS is stuck on a gear, does it's value stay at 0?

EDIT:
I must have done something wrong. I can see that the switch is being activated, but the count stays at 0.

I called all functions in autonomous mode and I modified this part of the code:

void Encoder_1_Int_Handler(void)
{
if(ENCODER_1 == 0)
{
Encoder_1_Count++;
}
printf("Encoder: %d", Encoder_1_Count);
printf("Switch: %d", rc_dig_in06);
}
Encoder_1 is rc_dig_in06

I read the readme, and I assume I did everything correctly.

Please keep in mind that this is my first year programming, and I'm most probably overlooking something obvious...


Denz 27-02-2006 07:21

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
I had the encoder to rc_dig_in06 aswell. I defined it in encoder.h. Where should I print from? Because it's printing, just it's only printing 0, not counting like it should.

And how do you see how the interrupts trigger on the test code if you don't have printfs? My code is not really modified, I just removed Encoder_3-6 because I wont be using them. I kept Encoder_1 and_2 and have them to rc_dig_in06 and 07. I defined it properly in encoder.h, but maybe I'll swithc them back to rc_dig_in01 and 02.
Thanks! Anything else I'm missing?

Alan Anderson 27-02-2006 08:25

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Quote:

Originally Posted by Denz
Code:

void Encoder_1_Int_Handler(void)
{
        if(ENCODER_1 == 0)
        {
                Encoder_1_Count++;
        }
        printf("Encoder: %d", Encoder_1_Count);
        printf("Switch: %d", rc_dig_in06);
}

Encoder_1 is rc_dig_in06

Two big problems. First, the Encoder_1_Int_Handler() interrupt service routine will only be triggered by a transition on digital input 1. Move your gear tooth sensor to rc_dig_in_01. There's no interrupt connection to rc_dig_in06.

Second, you won't be incrementing the counter unless the code manages to execute quickly enough to read the value of the pin before it goes back high. Take out the if statement. The interrupt will occur only once per pulse of the input signal.

There's a small problem as well -- using printf() inside an interrupt routine is risky, as it can be a time-consuming function.

Denz 27-02-2006 16:19

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Quote:

Originally Posted by Alan Anderson
Two big problems. First, the Encoder_1_Int_Handler() interrupt service routine will only be triggered by a transition on digital input 1. Move your gear tooth sensor to rc_dig_in_01. There's no interrupt connection to rc_dig_in06.

Second, you won't be incrementing the counter unless the code manages to execute quickly enough to read the value of the pin before it goes back high. Take out the if statement. The interrupt will occur only once per pulse of the input signal.

There's a small problem as well -- using printf() inside an interrupt routine is risky, as it can be a time-consuming function.

I moved it to rc_dig_in01. What should I put in place of the if statement to increment the counter?

Also, I'm getting a code error sometimes when I run the code.

6600gt 27-02-2006 23:14

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Quote:

Originally Posted by Denz
I moved it to rc_dig_in01. What should I put in place of the if statement to increment the counter?

Also, I'm getting a code error sometimes when I run the code.

Nothing, just the encoder_1_count++ will work(everytime the interrupt occurs the count goes up by one)

Is this using Kevin's encoder code or you ported over the encoder.c and .h files over to your code?

If you did port over then look at his user_routines_fast.c file in Kevin's encoder code on how to call the interrupt handler.

print in the Process_Data_From_Master_uP() before putdata function.

Denz 27-02-2006 23:49

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Oh man, this just doesn't seem to want to work! I've been working on it since friday! Ummm, ok I changed some things, now I'm getting a code error. (the red light of doom). I don't know what to do! I guess I'll start from scratch again! :( We need more programmers on my team! lol

EDIT: Ok, no more red light of death, but sitll not working, would it help if I posted my code?

Denz 28-02-2006 00:15

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
3 Attachment(s)
Here it is! Thanks guys so far for all the help, I guess I'm just not getting it!


EDIT: I changed encoder.c, still doesn't count however.

Alan Anderson 28-02-2006 07:27

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Quote:

Originally Posted by Denz
...Ok, no more red light of death, but sitll not working, would it help if I posted my code?

It would help if you posted your code, and it would help even more if you clarified exactly what "not working" means. What are you expecting it to do, and what is it doing instead?

Looking at your code, I see a couple of small issues with a printf(). The encoder counts are defined as long, and you're not casting them to an int when printing their value. That could give unexpected results, as the %d format specifier expects a regular integer and might show you just the high two bytes of the value (which will be zero until you've counted a lot of teeth).

Also, if you're trying to read the value of the counter outside the interrupt service routine, you should not be accessing the variable directly. Instead, use the function provided to do it. That way you won't find your code being interrupted and changing the value in the middle of reading it, leaving you with an inconsistent result.

Denz 28-02-2006 14:46

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
I want the GTS to count everytime it senses a tooth. So it can count rotations of the wheels (say 90 teeth per rev). As of now, when I print the value, it stays at 0. That is what I mean by not working.
I uploaded a new version of encoder.c, it still doesn't count however. I made all the changes you suggested.

Alan Anderson 28-02-2006 16:23

Re: GearTooth Sensor Destroying Hopes of Autonomous~!!!!!!
 
Quote:

Originally Posted by Denz
I want the GTS to count everytime it senses a tooth. So it can count rotations of the wheels (say 90 teeth per rev). As of now, when I print the value, it stays at 0. That is what I mean by not working.
I uploaded a new version of encoder.c, it still doesn't count however. I made all the changes you suggested.

Your software looks like it should work. Do you trust your hardware?

Is the gear tooth sensor mounted as specified, in the correct orientation and at the correct distance from a ferrous gear? Does the board have 12v power on the proper pin? Does it have 5v and ground? Have you looked at the output with an oscilloscope to verify that it's providing pulses?


All times are GMT -5. The time now is 12:50.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi