Go to Post You won't find anything in school that says you have to share ideas or strategy with your football rivals. - Al Skierkiewicz [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 03-05-2008, 06:06
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
Question Automatic counter-Encoder

Hi everyone, I dont know much about programming so please help me out. Thanks!

Im looking for a way to make a counter that counts from 1-30 or 0-29. A total of 30 units. I have a clicker hooked up to a wheel and everytime the wheel moves 12 degrees i get a steady 1 or a steady 0 at the digital input. A total of 15x 0's and 15x1's willl be observed. I need a code that adds 1 everytime the input changes.

Starting with 0 , 1 , 0, 1, 0 , 1 , 0, 1, 0 , 1 ...etc
The counter: 1 +1 + 1+1+1+1 +1 +1 +1 +1 each time...and then everytime a 1 has been added, it keeps that value, untill the next 1 gets added.

Example: I can retrieve a number from the counter and if the number is 23, i make the PWM = 127, when the number is 24, i make the pwm = 135....etc

When the number reaches 29 or 30 depending if you started with 0 or 1, the next digit 30 or 31 does not exist and automatically resets to 0 and starts all over again....

Please point me in the right direction. My knowledge of C is limited to what I've been able to understand from examples and IFI mini RC programming guides.

Yes, I am using the Mini RC, EDU controller.
  #2   Spotlight this post!  
Unread 03-05-2008, 11:14
Uberbots's Avatar
Uberbots Uberbots is offline
Mad Programmer
AKA: Billy Sisson
FRC #1124 (ÜberBots)
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Avon
Posts: 739
Uberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond repute
Re: Automatic counter-Encoder

id reccomend setting up an interrupt and attatching your [s]encoder[/s] clicker to the interrupt ports on the edu-RC.

from there, you can add the signal to a stored variable, which will cause the integration you are looking for.


as for resetting after 30... you can do something like:

if (clicks % 30 == 0) {
clicks = 1;
}else{
clicks += input;
}
__________________
A few of my favorite numbers:
175 176 177 195 230 558 716 1024 1071 1592 1784 1816
RPI 2012
BREAKAWAY
  #3   Spotlight this post!  
Unread 03-05-2008, 11:52
Nikhil Bajaj Nikhil Bajaj is offline
MATLAB Fan
FRC #0461 (Westside Boiler Invasion)
Team Role: Mentor
 
Join Date: Feb 2003
Rookie Year: 2002
Location: West Lafayette, Indiana
Posts: 101
Nikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond repute
Send a message via AIM to Nikhil Bajaj
Re: Automatic counter-Encoder

I have a feeling that you may have to connect the same clicker output to two interrupt pins, and have one trigger on the rising edge and one trigger on the falling edge in order to do what you want...don't the PIC interrupt pins only trigger on either rising or falling, and not both? I guess I'm not quite sure about this...it depends on how the microcontroller is configured in the RC...

Last edited by Nikhil Bajaj : 03-05-2008 at 11:57.
  #4   Spotlight this post!  
Unread 03-05-2008, 12:35
Phalanx's Avatar
Phalanx Phalanx is offline
Formerly Team 1089 (Mercury)
AKA: Michael Reffler
FRC #5431 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 1999
Location: Lewisville, TX (previously NJ)
Posts: 384
Phalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond repute
Re: Automatic counter-Encoder

For starters look at Kevin Watson's excellent repository of FRC code and FAQ's.
http://www.kevin.org/frc/

Specifically for the EDU Controller read the encoder_readme.txt within the zip on how to make the code work with the EDU controller.
http://www.kevin.org/frc/frc_encoder.zip

Now if I remember correctly digital ports 3 through 6 are designed to interrupt on both the rising and falling edge. So if you connect your clicker to one of those it will trigger an interrupt on 0,1,0,1,0 and so on. Simply remove the "Phase B" logic and it should work as you need.
__________________
Don't just ask the experts, become one!
Leadership is not about ability. It's about responsibility!
Diagonally Parked in a Parallel Universe. It's okay we do Quantum Physics



Last edited by Phalanx : 03-05-2008 at 12:38.
  #5   Spotlight this post!  
Unread 03-05-2008, 12:41
Nikhil Bajaj Nikhil Bajaj is offline
MATLAB Fan
FRC #0461 (Westside Boiler Invasion)
Team Role: Mentor
 
Join Date: Feb 2003
Rookie Year: 2002
Location: West Lafayette, Indiana
Posts: 101
Nikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond reputeNikhil Bajaj has a reputation beyond repute
Send a message via AIM to Nikhil Bajaj
Re: Automatic counter-Encoder

Ah, I see. I just looked up the internal wiring diagram for the RC, and yeah, digital inputs/outputs 3-6 on the RC appear to be connected to port B on the PIC, which has that functionality.

Thanks, Phalanx.
  #6   Spotlight this post!  
Unread 03-05-2008, 12:51
Kevin Sevcik's Avatar
Kevin Sevcik Kevin Sevcik is offline
(Insert witty comment here)
FRC #0057 (The Leopards)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Houston, Texas
Posts: 3,673
Kevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond repute
Send a message via AIM to Kevin Sevcik Send a message via Yahoo to Kevin Sevcik
Re: Automatic counter-Encoder

Oh jeeez. Look, how fast is this "clicker" going to be clicking? If it's slower than 38 times per second you could have the code to do this in your slow loop. If it's slower than about 300 times per second (your wheel is turning slower than 600 RPM) then you can almost certainly put the checking code in your fast loop and just not worry about the complication of interrupts. The code itself is simple.

count = (count + ((dig_in ^ old_dig_in)?1:0))%30;
old_dig_in = dig_in;

And you're done. ^ is the bitwise XOR operator, it's true if both arguments aren't the same. So if the current input isn't equal to the old input, you add 1. Otherwise you add 0. And then you modulo by 30.

Unless you absolutely have to not miss any of the clicks and it's turning at some ludicrous speed, just plop that in your code with dig_in replaced by the actual digital input and don't worry about interrupts.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.

Lone Star Regional Troubleshooter
  #7   Spotlight this post!  
Unread 03-05-2008, 13:03
Uberbots's Avatar
Uberbots Uberbots is offline
Mad Programmer
AKA: Billy Sisson
FRC #1124 (ÜberBots)
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Avon
Posts: 739
Uberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond repute
Re: Automatic counter-Encoder

Quote:
Originally Posted by Kevin Sevcik View Post
And then you modulo by 30.
THATS the operation i was looking for in my suggestion... i guess thats why i shouldnt be programming before noon.
__________________
A few of my favorite numbers:
175 176 177 195 230 558 716 1024 1071 1592 1784 1816
RPI 2012
BREAKAWAY
  #8   Spotlight this post!  
Unread 03-05-2008, 13:53
AustinSchuh AustinSchuh is offline
Registered User
FRC #0971 (Spartan Robotics) #254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Feb 2005
Rookie Year: 1999
Location: Los Altos, CA
Posts: 803
AustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond repute
Re: Automatic counter-Encoder

One thing to keep in mind when using interrupts, is that if you use a mechanical switch to trigger it, the mechanical switch doesn't cleanly fire just once. It tends to trigger the interrupt anywhere from 10 to 100 times. I learned this a couple of years ago with a mechanical mouse encoder hooked up to our robot. Optical switches don't have this problem.

I only bring this up because you don't say what type of switch you are using.
  #9   Spotlight this post!  
Unread 03-05-2008, 17:26
Phalanx's Avatar
Phalanx Phalanx is offline
Formerly Team 1089 (Mercury)
AKA: Michael Reffler
FRC #5431 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 1999
Location: Lewisville, TX (previously NJ)
Posts: 384
Phalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond repute
Re: Automatic counter-Encoder

If you want to avoid using interrupts, you could place Kevin Sevcik's code sample in Process_Data_From_Local_IO() within user_routines_fast(). It may give you more accurate readings if you really need that.

I've never had any issue with any mechanical switch generating spurious interrupts, but that has only been my experience. I find with higher quality components that becomes less an issue.
__________________
Don't just ask the experts, become one!
Leadership is not about ability. It's about responsibility!
Diagonally Parked in a Parallel Universe. It's okay we do Quantum Physics


  #10   Spotlight this post!  
Unread 03-05-2008, 18:43
Kevin Sevcik's Avatar
Kevin Sevcik Kevin Sevcik is offline
(Insert witty comment here)
FRC #0057 (The Leopards)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Houston, Texas
Posts: 3,673
Kevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond repute
Send a message via AIM to Kevin Sevcik Send a message via Yahoo to Kevin Sevcik
Re: Automatic counter-Encoder

Quote:
Originally Posted by Phalanx View Post
If you want to avoid using interrupts, you could place Kevin Sevcik's code sample in Process_Data_From_Local_IO() within user_routines_fast(). It may give you more accurate readings if you really need that.

I've never had any issue with any mechanical switch generating spurious interrupts, but that has only been my experience. I find with higher quality components that becomes less an issue.
Mechanical switches are pretty notorious for bouncing, but typically under light switching force, so the contacts only slowly make contact and the resistance of the connection fluctuates a bit. You can account for this with some debounce logic in the fast loop as well, by checking to make sure the last two or three samples of the input are the same.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.

Lone Star Regional Troubleshooter
  #11   Spotlight this post!  
Unread 03-05-2008, 21:56
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
Re: Automatic counter-Encoder

Part I:

Woah there! Plenty of help, thanks!

As for Kevins code: count = (count + ((dig_in ^ old_dig_in)?1:0))%30;
old_dig_in = dig_in;

Do I simply replace dig_in with rc_dig_in05 for example if im using digital 05 as my input. Will I have to replace old_dig_in with anything or leave it as is....

So everytime I need to call back to find where the count is, I have to refer to using "count"? right? so in the case that count = 25 is true, do this etc etc...if count is 26 do this and so on. Would it be possible to start the count when the switch is either a 0 or a 1? For example, I always want the first number 1 to be when the switch is 1, and then when the switch is 0, that would become 2, if I had to reset, and the current switch is sitting at 0, it would not start the count unless the switch is at 1.

So can someone run this through with me? When the system boots up, what is it doing initially? It has no count to begin with? Does it start counting as soon as it detects that a change has occured?

Could it be made so that initially, if the switch is 0, it would count it as a 1, and when the switch starts with a 1, it would count is as an automatic 2. This way, each 0 that comes back from the switch becomes an odd number, while each 1, is an even number.

If you are wondering what this wheel is, it is actually a motor that I've been making in the shop. its made to track distance covered by the rotations.

Part II:

How do I print the current count in a printf format?

Last edited by Generalx5 : 03-05-2008 at 21:59. Reason: Added part II
  #12   Spotlight this post!  
Unread 03-05-2008, 22:32
Kevin Sevcik's Avatar
Kevin Sevcik Kevin Sevcik is offline
(Insert witty comment here)
FRC #0057 (The Leopards)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Houston, Texas
Posts: 3,673
Kevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond repute
Send a message via AIM to Kevin Sevcik Send a message via Yahoo to Kevin Sevcik
Re: Automatic counter-Encoder

Quote:
Originally Posted by Generalx5 View Post
Part I:

Woah there! Plenty of help, thanks!

As for Kevins code: count = (count + ((dig_in ^ old_dig_in)?1:0))%30;
old_dig_in = dig_in;

Do I simply replace dig_in with rc_dig_in05 for example if im using digital 05 as my input. Will I have to replace old_dig_in with anything or leave it as is....

So everytime I need to call back to find where the count is, I have to refer to using "count"? right? so in the case that count = 25 is true, do this etc etc...if count is 26 do this and so on. Would it be possible to start the count when the switch is either a 0 or a 1? For example, I always want the first number 1 to be when the switch is 1, and then when the switch is 0, that would become 2, if I had to reset, and the current switch is sitting at 0, it would not start the count unless the switch is at 1.

So can someone run this through with me? When the system boots up, what is it doing initially? It has no count to begin with? Does it start counting as soon as it detects that a change has occured?

Could it be made so that initially, if the switch is 0, it would count it as a 1, and when the switch starts with a 1, it would count is as an automatic 2. This way, each 0 that comes back from the switch becomes an odd number, while each 1, is an even number.

If you are wondering what this wheel is, it is actually a motor that I've been making in the shop. its made to track distance covered by the rotations.

Part II:

How do I print the current count in a printf format?
alright, so first, this is a 0-29 counter.

Second, yes you can leave old_dig_in as is. I'd recommend renaming it old_rc_dig_in05, though, in case you want to duplicate this code for another counter.

Third, as I said, ^ will be true and you'll increment your count whenever rc_dig_in05 and old_rc_dig_in05 aren't equal. So logically, it doesn't count when those two are equal. So, if you want count to start at 0 if rc_dig_in05 = 0 at the start, then what would you want to set old_rc_dig_in05 equal to to start out? If you get that right, then your second desire follows naturally.

You'll need to set count = 0 in your definition or in the initialization code or some such to have it start as 0. Or set it to whatever you like, but do recall that this is a 0-29 counter.

The printf would be printf("blah-ty-blah %d", (int) count); And the (int) IS important, as printf expects ints, and you can get some odd results giving it a char. (And you should be using a char for this.)


Finally, this only counts in one direction, if you want to change directions, it'll be a little different.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.

Lone Star Regional Troubleshooter
  #13   Spotlight this post!  
Unread 04-05-2008, 00:02
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
Re: Automatic counter-Encoder

There is a 0.625 - 0.635 Khz frequency producing only 0.9volts at the digital outputs. I've tried other digital outputs and they do the same thing. And the solenoids also give me that sort of frequency response. I measured it with a multimeter, FREQ% selection. What could cause this?

I will give the code a try, so far, im still trying to solve some other minor issues such as the one above.

Last edited by Generalx5 : 04-05-2008 at 00:05.
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
Automatic Something... synth3tk IT / Communications 8 08-03-2008 20:20
Automatic Transmission gnirts Programming 6 18-01-2007 20:44
Automatic Power Swampcoder2 Control System 6 03-02-2005 17:08
Automatic Power Swampcoder2 Electrical 6 03-02-2005 17:08
automatic archiving Joe Ross CD Forum Support 5 04-08-2003 13:27


All times are GMT -5. The time now is 01:00.

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