Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   encoder vs. motor (http://www.chiefdelphi.com/forums/showthread.php?t=31681)

seanwitte 04-01-2005 10:17

Re: encoder vs. motor
 
Quote:

Originally Posted by stephenthe1
I've been using Kevin Watson's interrupt template code. I want to have a variable increment every time the interrupt fires. that's not a problem. however, is there a static variable type available that I can use between files? I'm not aware of such a thing. also, does the code in interrupts.c execute first, or the code in user_routines_fast.c go first? this affects whether I use the interrupts.c file to handle the motor speed or the user_routines_fast.c file to handle the speed of the motor. if the user routines file comes second, that would be the case where I need the variable. I'm willing to bet there's an easier way, but this is what I've come up with so far. I would use Kevin's template for encoders but it supports two encoders for the wheels, when I want to use the one we already have set up on digitals 1 and 2 with one motor. we'll probably have to use his eventually on the wheels in addition to this encoder.
thanks,
Stephen.

Declare the variable you want to share between files in a header file with the "extern" storage specifier. For example:

extern unsigned char MyVar;

In a source file that includes the header, define the actual variable:

unsigned char MyVar;

Any source files that use the header now have access to MyVar. I would declare all of your shared variables in one header file and define them all in one source file. To use them you just include the header in the source files where needed. Something like "sharedvars.h" and "sharedvars.c".

Astronouth7303 04-01-2005 16:52

Re: encoder vs. motor
 
Quote:

Originally Posted by seanwitte
Declare the variable you want to share between files in a header file with the "extern" storage specifier. For example:

extern unsigned char MyVar;

In a source file that includes the header, define the actual variable:

unsigned char MyVar;

If the interupt fire enough, you will want to declare it volatile. ie,
Code:

/*** Variables.h ***/
volatile unsigned char MyVar = 0;

/*** MyCode.c ***/

extern volatile unsigned char MyVar;

This will cause it to not copy the value to a temporary storage location.

seanwitte 04-01-2005 17:00

Re: encoder vs. motor
 
Quote:

Originally Posted by Astronouth7303
If the interupt fire enough, you will want to declare it volatile. ie,
Code:

/*** Variables.h ***/
volatile unsigned char MyVar = 0;

/*** MyCode.c ***/

extern volatile unsigned char MyVar;

This will cause it to not copy the value to a temporary storage location.

Correct, I forgot about about the volatile keyword. You need to swap the declarations in the two files though. The extern statement goes in .h and the declaration goes in .c.

Code:

/*** Variables.h ***/
extern volatile unsigned char MyVar;

/*** MyCode.c ***/
volatile unsigned char MyVar = 0;


stephenthe1 04-01-2005 22:21

Re: encoder vs. motor
 
thanks guys!!! that clears that up.
do you know about the order the files are executed though? I need to know, because if I use interrupts in one file to control the speed of a motor, it needs to be read before the user_routines_fast.c file so that the variable's values (the extern one I was asking for help earlier) can be sent to that file.
do you see what I mean?
also, can I use the
volatile unsigned int my_int;
then assign its value to a static variable inside a method in another file?

(back to the question obout the file execution order) it would be really cool to be able to tell the controller to rescan the file (or whatever part of the .hex file it is) of course, it's better just to know which ones are done first and which are done last.
by the way, I feel dumb for asking this, but what's the difference between frc_rc and edu_rc.

Mike Betts 04-01-2005 22:59

Re: encoder vs. motor
 
Quote:

Originally Posted by stephenthe1
thanks guys!!! that clears that up.
do you know about the order the files are executed though? I need to know, because if I use interrupts in one file to control the speed of a motor, it needs to be read before the user_routines_fast.c file so that the variable's values (the extern one I was asking for help earlier) can be sent to that file.
do you see what I mean?
also, can I use the
volatile unsigned int my_int;
then assign its value to a static variable inside a method in another file?

(back to the question obout the file execution order) it would be really cool to be able to tell the controller to rescan the file (or whatever part of the .hex file it is) of course, it's better just to know which ones are done first and which are done last.
by the way, I feel dumb for asking this, but what's the difference between frc_rc and edu_rc.

1. Interrupts happen whenever interrupts happen. The only condition is that they must be enabled, This is typically performed in User_Initialization().

2. Your non-interrupt code executes from main () in main.c and follows whatever tortuous path you lead it on.

3. frc_rc is the full size controller that goes on our robots. edu_rc is the smaller controller for the Edubot.

stephenthe1 05-01-2005 15:23

Re: encoder vs. motor
 
as I was looking at Kevin's encoder template, I was wandering how these particular variables are available to all files, as they aren't declared with the "extern" keyword.
examples:

long Get_Left_Encoder_Count(void); // call this to get the current left
// wheel's encoder count

long Get_Right_Encoder_Count(void); // call this to get the current right
// wheel's encoder count

void Set_Left_Encoder_Count(long); // call this to set the left wheel's
// encoder count

void Set_Right_Encoder_Count(long); // call this to set the right wheel's
// encoder count

void Left_Encoder_Int_Handler(void); // left wheel encoder interrupt handler

void Right_Encoder_Int_Handler(void); // right wheel encoder interrupt handler

do you see what I mean? this is pretty much the only thing keeping me from coding our encoder is understanding why this is. thanks. also, does anyone know an easy way to set up a third encoder easily with his code (attatched to dig_in_01 and dig_in_02 (plus setting them up as interrupts))? I'm semi-new, so I'm proned to mistakes. thanks again,
Stephen

Mike Betts 05-01-2005 15:36

Re: encoder vs. motor
 
Quote:

Originally Posted by stephenthe1
as I was looking at Kevin's encoder template, I was wandering how these particular variables are available to all files, as they aren't declared with the "extern" keyword.
examples:

long Get_Left_Encoder_Count(void); // call this to get the current left
// wheel's encoder count

long Get_Right_Encoder_Count(void); // call this to get the current right
// wheel's encoder count

void Set_Left_Encoder_Count(long); // call this to set the left wheel's
// encoder count

void Set_Right_Encoder_Count(long); // call this to set the right wheel's
// encoder count

void Left_Encoder_Int_Handler(void); // left wheel encoder interrupt handler

void Right_Encoder_Int_Handler(void); // right wheel encoder interrupt handler

do you see what I mean? this is pretty much the only thing keeping me from coding our encoder is understanding why this is. thanks. also, does anyone know an easy way to set up a third encoder easily with his code (attatched to dig_in_01 and dig_in_02 (plus setting them up as interrupts))? I'm semi-new, so I'm proned to mistakes. thanks again,
Stephen

Stephen,

All of your examples are of functions not variables...

stephenthe1 05-01-2005 19:56

Re: encoder vs. motor
 
yeah, I soon realized that after posting. thanks for the tip though.

stephenthe1 06-01-2005 18:39

Re: encoder vs. motor
 
beware, long question...

ok, I have a couple questions.
1: in Kevin's encoder template, he first uses the Get_Left_Encoder_Count function, and assigns its value to the "count" variable. then he uses the Get_Right_Encoder_Count function and assigns its return value to the "count" variable also. Then he runs the Set_Left_Encoder_Count function, and uses the "count" variable to assign the value of "count" to Left_Encoder_Count. then he uses the "count" variable to assign the value of "count" to Right_Encoder_Count. I don't understand how he can do this without confusing the value of "count" between the left and right encoders. any help would be appreciated.

2:when setting up digitals as low priority interrupts, what do I use to reference the different pins (1-6, or interrupts 2-7). I know the first one is INTCON3bits.INT2IP = 0. but I need confirmation that that one is correct, and what the other five are. I know that interrupts 3-6 (4-7) are ganged together. I would prefer to know how to address each one individually
if that can be done easily, if not, I can set them all as low priority interrupts. I think it'd be better to do the other way though, because I need to learn how to address each one individually.

3: similarly to question two, I need to know the names of the pins to set the "edge select" option. also, the names to clear the interrupt flag and to enable the interrupts. I found some of the info in the "interrupts for dummies tutorial," but I don't know how to address each one specifically. I can't be in the dark or guessing on any of this, because interrupts are very finicky.

thank you,
Stephen

Mike Betts 06-01-2005 20:26

Re: encoder vs. motor
 
Stephen,

Find a good primer on C and read it. I highly recommend http://www.lysator.liu.se/c/bwk-tutor.html

Understand that I am not trying to be cruel... I just feel that you have not attempted to work things out for yourself. If Mr. Watson gave you all of the answers, what would you learn?

As it is, I feel he has given you too much.

I think back to my freshman days at college. A fellow student (I'll call him John) was hopelessly lost and in over his head. Another student gave him the program (it was a stack of punchcards for an IBM 360 mainframe). All John had to do was change the job card (the top card on the stack) and run his program...

John screwed it up and, after several hours, declared that he had changed all of the variable names and the program still didn't work!

John's problem was that he never read the material required by the professor and was too busy talking in class to pay attention.

Now to your situation. Your earlier post indicates that you have no real understanding of C. The variable count is a local variable inside of Get_Right_Encoder_Count and has nothing at all to do with the local variable count inside of Get_Left_Encoder_Count. Both functions return a number to the statement which called them.

Suppose I asked you to find out how many pennies Marsha has and then how many Bill has? You would go to Marsha and find out she has 3 pennies and report this to me. Then you would go to Bill hand find out he has 2 pennies and report it to me. The face that Marsha had 3 pennies does not influence the fact that Bill has two. I did not ask you to add then together... Just report.

The function Get_Right_Encoder_Count has no idea what other functions are available. It just reports the number of clicks that the right encoder has seen.

The last line of the function could have been written:

return(Right_Encoder_Count);

without any local variable... Does this make sense?

Bottom line: Mr. Watson's code works.

stephenthe1 06-01-2005 22:07

Re: encoder vs. motor
 
your right there. it's just frustrating right now, because I understand the code so well, save that little thing and methods. however, my main problem was not that little variable, but the interrupt names ( the whole TRISBbits. etc. thing ), mainly for accessing the 3-6 interrupts. yeah, I'm the type of guy, who, when I understand it 100%, I can work magic, untill then, I'm in a ditch.
2 days till kickoff :yikes: excitement is bubbling.


All times are GMT -5. The time now is 22:26.

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