Go to Post We were the "oooh, neat, fire!" robot that year. - pfreivald [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 18-01-2007, 12:29
Mr. E Mr. E is offline
Its physics or stamp collecting
FRC #1883
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Las Vegas
Posts: 38
Mr. E is an unknown quantity at this point
programming a switch

I want to hit the microswitch and have a motor stop. I set the microswith to the digital input and I can make the light on the control board blink but thats about it.
I changed the line in the definitions from

digital_io_01 = digital_io_02 = digital_io_03 = digital_io_04 = INPUT;
to
digital_io_01 = digital_io_02 = digital_io_03 = digital_io_04 = 0;

I tried a simple if then statement but to no avail.

if (digital_io_01 = 1)
{
pwm01=127;
}

Any suggestions?
  #2   Spotlight this post!  
Unread 18-01-2007, 12:37
dcbrown dcbrown is offline
Registered User
AKA: Bud
no team
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Hollis,NH
Posts: 236
dcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud of
Re: programming a switch

I believe you just set the io pins to be outputs rather than inputs.

See ifi_aliases.h

Quote:
#define INPUT 1
#define OUTPUT 0

/* Used in User_Initialization routine in user_routines.c file. */
/* Used to set pins as digital INPUTS or digital OUTPUTS. */
#define digital_io_01 TRISBbits.TRISB2
#define digital_io_02 TRISBbits.TRISB3
...
Set them back to inputs and use the io pin name to read the values.

Again, ifi_aliases.h has these defined.

Quote:
/* Aliases used to read the pins when used as INPUTS. */
#define rc_dig_in01 PORTBbits.RB2 /* external interrupt RB2/INT2 */
#define rc_dig_in02 PORTBbits.RB3 /* external interrupt RB3/INT3 */
#define rc_dig_in03 PORTBbits.RB4 /* external interrupt-on-change RB4 */
#define rc_dig_in04 PORTBbits.RB5 /* external interrupt-on-change RB5 */
#define rc_dig_in05 PORTBbits.RB6 /* external interrupt-on-change RB6 */
#define rc_dig_in06 PORTBbits.RB7 /* external interrupt-on-change RB7 */
So change the code to test rc_dig_in01.


Bud
  #3   Spotlight this post!  
Unread 18-01-2007, 12:41
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: programming a switch

The digital_io_## names are for determining whether a digital pin is an input or an output. You want an input, so leave their setup the way it is in the default code.

To read the value of the input, use rc_dig_in##.
Code:
if (rc_dig_in01 == 1)
  {
  pwm01=127;
  }
Note the double equals sign == for making the comparison. You had a single =, which assigns a value.
  #4   Spotlight this post!  
Unread 18-01-2007, 12:43
Imajie Imajie is offline
Registered User
AKA: James Letendre
FRC #0195 (Cyber Knights)
Team Role: Electrical
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Southington
Posts: 114
Imajie has much to be proud ofImajie has much to be proud ofImajie has much to be proud ofImajie has much to be proud ofImajie has much to be proud ofImajie has much to be proud ofImajie has much to be proud ofImajie has much to be proud of
Send a message via AIM to Imajie
Re: programming a switch

Quote:
Originally Posted by Mr. E View Post
if (digital_io_01 = 1)
{
pwm01=127;
}

Any suggestions?
First make sure if you want to see if something is equal to something else you use ==. In C = means assign and == means compare.
__________________
2007 UTC Champions Thanks 1124 and 558
2007 UTC GM Industrial Design Award

2006 Newton Divisional Winners
2006 Championship finalists
(Thanks 25 and 968 for picking us!)
2006 National Innovation in Control
  #5   Spotlight this post!  
Unread 18-01-2007, 13:18
Mr. E Mr. E is offline
Its physics or stamp collecting
FRC #1883
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Las Vegas
Posts: 38
Mr. E is an unknown quantity at this point
Re: programming a switch

I love the solution Alan, I can't wait for the end of school to try it out. The solution also allows me to do a variety of tasks whenever I hit a switch.
thanks.
  #6   Spotlight this post!  
Unread 19-01-2007, 12:53
Mr. E Mr. E is offline
Its physics or stamp collecting
FRC #1883
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Las Vegas
Posts: 38
Mr. E is an unknown quantity at this point
Re: programming a switch

Didn't work.
I double/triple checked my wiring, made sure I was downloading the right code to the robot, and a myriad of other possibilities. I never had a compiling problem, the motor would simply not stop. Is this a possibility?
turn on a motor when the swith is hit? Does an if statement require an "else"?

if (rc_dig_in01 == 1)
{
pwm01=155;
}

I tried the above and it didn't work either...
  #7   Spotlight this post!  
Unread 19-01-2007, 13:05
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,795
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: programming a switch

I assume you set pwm01 by joystick or something else before making your micro switch "stop" check?
It doesn't accidentally get set again after your check?

Does your switch allow it to be wired by default open or closed?
You may be checking it backwards. A regular on/off switch (on the RC) will be read as "1" when open and "0" when closed. (An OI button is the opposite by the way).

E.g.,
Code:
pwm01=200;
if (rc_dig_in01 == 0)     // Act when the RC switch is closed
{
  pwm01=127;
}
The limit switches that come in the KOP have three terminals that give you the option of wiring them either way and can be wired incorrectly. Have you tested your switch with a multi-meter to see if it's open or closed when you expect?
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 19-01-2007 at 13:19.
  #8   Spotlight this post!  
Unread 19-01-2007, 13:24
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: programming a switch

Quote:
Originally Posted by Mr. E View Post
...I double/triple checked my wiring,...
You've never told us how you wired the switch. If you misunderstand how to do it, then no matter how many times you check it you'll just be ensuring that you did it wrong.

How do you have the switch connected? Be as detailed as you can, so we can make sure you're doing it the way the RC needs it to be.
  #9   Spotlight this post!  
Unread 19-01-2007, 14:55
Mr. E Mr. E is offline
Its physics or stamp collecting
FRC #1883
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Las Vegas
Posts: 38
Mr. E is an unknown quantity at this point
Re: programming a switch

The PWM cable is going to the digital in/out 1 on the board. the cable is going to the microswitch. the white is connected to the ground (com1), the red is going to the NC 2 and the black to NO 3. I've also ran it with black and red swapped. I get feedback on the interface ( green light turn on when I hit the switch--or turn off when I hit the switch--depending how I've wired it. )
I connect a motor to Pwm01. I know it functions correctly since I can control it with the joystick just fine.
TESTING METHOD:
load the code.
"turn on the motor" by pushing forward on the stick--the motor spins
hit the microswitch--motor keeps spinning.

I cut and pasted your code to where I map the joystick to the pwms in the user_routines.c
  #10   Spotlight this post!  
Unread 19-01-2007, 15:29
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: programming a switch

Quote:
Originally Posted by Mr. E View Post
The PWM cable is going to the digital in/out 1 on the board. the cable is going to the microswitch. the white is connected to the ground (com1), the red is going to the NC 2 and the black to NO 3. I've also ran it with black and red swapped.
That'll work, but it's a very unusual way to do it. The RC digital inputs just want to see a contact closure to ground. They already have a +5v pullup built in, so there's no need to connect the red wire.

The typical way to connect a switch is to connect the black ground wire to the COM pin and the white signal wire to either the NC or NO pin, depending on whether you want the switch to be normally closed or normally open.

Since the OI feedback light is responding as you expect, the problem is not with the switch. It has to be with the way you're controlling the motor. Make doubly sure that the code that tests the switch and sets the pwm to neutral is executing after anything that sets the pwm to reflect the joystick position. Perhaps you could post the relevant part of your code for us to look over?
  #11   Spotlight this post!  
Unread 28-01-2007, 22:55
dwc309 dwc309 is offline
Registered User
FRC #1552
 
Join Date: Jan 2006
Location: Niwot High School
Posts: 16
dwc309 is an unknown quantity at this point
Re: programming a switch

Since your LED is lighting it sounds like your circuit is working.

Switches bounce when they switches for milliseconds. This can lead to timing issues reading the switch state.

Try putting a 6.8 microfarad electrolytic capacitor and a .01 microfarad capacitor from the input pin to ground and see if it works better. The positive electrolytic pin should go on the input.
  #12   Spotlight this post!  
Unread 04-02-2007, 17:17
duane's Avatar
duane duane is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2003
Location: Vacaville
Posts: 98
duane is an unknown quantity at this point
Send a message via AIM to duane
Re: programming a switch

Quote:
They already have a +5v pullup built in
I'm curious about this because it appears that this is not true for all of the pins.

According to the Analog/Digital IO schematic digital inputs 1-6 are internally pulled up. But digital IO 7-18 are not, they must be externally pulled up.

I hope I am missing something, but this seems like an extraordinarily unsafe design. The normal state of the digital inputs are high with the pull ups, however, if the connector is removed, the state will look low, which will look closed or active to the system.

It seems like these digital inputs would need a normally closed system to ensure that they are safe if the connector is removed. It's often difficult to locate normally closed switches for some items.

Am I missing something? How can these inputs be used in a safe manner?
__________________
Duane Murphy
Mentor - Software
Vanden Vikings FIRST Team 701
http://www.vandenrobotics.com
  #13   Spotlight this post!  
Unread 04-02-2007, 23:10
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: programming a switch

Quote:
Originally Posted by duane View Post
According to the Analog/Digital IO schematic digital inputs 1-6 are internally pulled up. But digital IO 7-18 are not, they must be externally pulled up.
Where is this "Analog/Digital IO schematic" you mention? According to the one on the IFI web site, there is a 100k resistor to +5v directly to each of digital I/O pins 7-18.
  #14   Spotlight this post!  
Unread 05-02-2007, 09:27
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,795
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: programming a switch

Quote:
Originally Posted by duane View Post
According to the Analog/Digital IO schematic digital inputs 1-6 are internally pulled up. But digital IO 7-18 are not, they must be externally pulled up.
Perhaps there is a misinterpretation of the use of "internal" on that schematic?
They are all pulled up within the confines of the Robot Controller enclosure (internal from our perspective), however, some are pulled up internal to the PIC itself while others are pulled up by IFI designed "external" circuitry.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
  #15   Spotlight this post!  
Unread 05-02-2007, 21:16
kmcclary's Avatar
kmcclary kmcclary is offline
Founder 830/1015;Mentor 66/470/1502
FRC #0470 (Alpha Omega Robotics)
Team Role: Engineer
 
Join Date: Aug 2001
Rookie Year: 1994
Location: Ann Arbor, MI
Posts: 491
kmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond reputekmcclary has a reputation beyond repute
Re: programming a limit switch

One big problem I noticed in your code:

You don't want to FREEZE the motor when a limit switch is hit. If you do that, your device dies as soon as you hit it forever, and you'll have to manually pull it off!

Instead, you want to prevent further motion in that direction. IOW, you override a motion command ONLY when that direction's limit is hit AND you're asking for MORE motion in the SAME direction.

Here's a code sample, that implements both forward and backwards motion limiting.

BTW, notice I've also included the practice of "#define"-ing the hardware assignments, logical states, and all control values. This separates the device assignments and device control values from the logical control code. (I'm also using the convention of #define'd values being "all caps", to hint at where to look for them.)

This separation (when combined with proper grouping of the defines), allows for far better device assigment management, AND more readable logical code. The last thing you want to do is to search through code for hard coded values and device assignments when you want to change one thing. That's a recipe for bugs!

<edit>
(BTW, this is off the top of my head, based on what I've done before on other systems. I haven't tried compiling this on this year's C18 compiler. Let me know if it won't compile, this year's RC digital I/O switch assertion is backwards, etc... Thanks!)
</edit>

I hope this helps!

- Keith McClary
Chief Engineer, Team 1502, "Technical Difficulties"

Code:
//--------------- Header Snip follows --------------

//
// --- Device Assignment Definitions
//

// DIGITAL I/O Assignments  (Note: EVERY DIO pin has an entry...comment out unused)
#define WIDGET_FWD_LIMIT_SW    rc_dig_in01
#define WIDGET_REV_LIMIT_SW    rc_dig_in02
//#define ________        rc_dig_in03
//                (...etc...)

// PWM Assignments    (Note: EVERY PWM port has an entry...comment out unused)
#define WIDGET_MOTOR        pwm01
//#define ________        pwm02
//                (...etc...)

// (other devices follow, [analog in, relay out, serial ports, etc.]...)

//
// --- Victor Speed Defines
//
#define FULL_FORWARD     254
#define NEUTRAL        127
#define STOP        127
#define FULL_REVERSE    0

// Widget forward & reverse speeds - tweek values HERE, *NOT* inline!
//  This makes changes happen in only ONE PLACE...
#define WIDGET_FWD    200
#define WIDGET_FWD_SLOW 150
#define WIDGET_REV_SLOW 100
#define WIDGET_REV    25

//
// --- Logical Defines
//
#define FALSE   0
#define TRUE    1

// I/O Switch assertion Defines (RC varies by year! Uncomment this year's def)
//    Hardware:  switch pulls to ground against a resistor pullup

// #define SW_OFF    0
// #define SW_ON    1

#define SW_OFF    1
#define SW_ON    0


//--------------- Code snip follows --------------


//-----------------------------------------------------------------------
//  Sample widget limit switch test code follows.
//  Change speed *values defs* in headers, or add more options *there*.
//-----------------------------------------------------------------------

//--Choose one--

    WIDGET_MOTOR = WIDGET_FWD;
//  WIDGET_MOTOR = STOP;    // (or NEUTRAL)
//  WIDGET_MOTOR = WIDGET_REV;

//-----------------------------------------------------------------------
// Widget Limit Switch Handler (locate *after* normal widget control code)
// 
// Stops widget whenever a limit switch is closed *AND* still asking for 
// wrong dir.  It allows the widget to back off from the switch.
// (Assumes WIDGET_MOTOR values greater than NEUTRAL => forward motion)
//-----------------------------------------------------------------------

if (WIDGET_FWD_LIMIT_SW == SW_ON  &&  WIDGET_MOTOR > NEUTRAL )    // Hit Fwd Limit & want more?
      {
       WIDGET_MOTOR = STOP;                    // ..Y, stop widget
      }

if (WIDGET_REV_LIMIT_SW == SW_ON  &&  WIDGET_MOTOR < NEUTRAL )    // Hit Rev Limit & want more?
      {
       WIDGET_MOTOR = STOP;                    // ..Y, stop widget
      }
__________________
Keith McClary - Organizer/Mentor/Sponsor - Ann Arbor MI area FIRST teams
ACTI - Automation Computer Technologies, Inc. (Sponsoring FIRST teams since 2001!)
MI Robot Club (Trainer) / GO-Tech Maker's Club / RepRap-Michigan) / SEMI CNC Club
"Certifiably Insane": Started FIVE FRC teams & many robot clubs (so far)!
2002: 830 "Rat Pack" | 2003-5;14: 1015;1076 "Pi Hi Samurai" | 2005-6: 1549 "Washtenuts"/"Fire Traxx"
2005-(on): 1502 "Technical Difficulties" | 2006-(on): FIRST Volunteer!
2009-(on): 470 "Alpha Omega" | WAFL | Sponsor & "Floating Engineer" for MI Dist 13 (Washtenaw Cnty)
2011: 3638 "Tigertrons" | 2013-(on): 4395 "ViBots" | 2014-(on) 66 "Grizzlies"
"Home" Teams: 66, 470, 1076, 1502, 4395
Local FIRST alumni at or coming to Ann Arbor (UM/EMU/WCC/Cleary)?
...We Want YOU as a Mentor! Please email me for info!
Support CDF Reputation - If a posting helped, thank 'em with rep points!
"It must be FRC build season when your spouse and children become 'Action Items 8 & 9'..."

Last edited by kmcclary : 05-02-2007 at 21:23.
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
Momentary Switch Programming silv940 Programming 3 24-05-2006 02:19
problems programming knob and switch caderader Programming 4 16-02-2006 16:45
Problems programming a switch caderader Programming 2 18-02-2005 22:06
3 way toggle switch programming LeadRiccardoT Programming 3 17-02-2003 02:02
Error found in programming for the pump and pressure switch sjharobotics Programming 4 06-02-2002 17:46


All times are GMT -5. The time now is 10:34.

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