Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Help! Programming question with triggers (http://www.chiefdelphi.com/forums/showthread.php?t=26863)

AsimC 17-03-2004 17:48

Help! Programming question with triggers
 
its the day before competition and i really need help on this. Our team decided to make a trigger box for our robot and one trigger controls 2 motors. What i what it to do is, when i hold the trigger forward, the motors go forward, and when i let go the motor stops. For some reason i cant get it to work. What happens is, i hold the trigger forward and the motors continuely move even if i let go. Ive tried IF loops as well as FOR loops. Any suggestions? (as quickly as possible :ahh: )

bstempi 17-03-2004 18:21

Re: Help! Programming question with triggers
 
how do you have it wired up? From what you posted, it sounds like some sort of toggle switch you're using. Do you know what variables that coresponds to in the code?

JoshJ 17-03-2004 18:27

Re: Help! Programming question with triggers
 
See if u can post a copy of your code. I would stay away from FOR loops, all u should need to do is set the motors to something when ur trigger is forward, and set them to 127 when it is not. Also, see if you have any other code that assigns values to these motors.

KenWittlief 17-03-2004 18:27

Re: Help! Programming question with triggers
 
you dont need an if statement

just something like

relay_fwd01 = p1_trig01; in your code

when the switch is on the relay is on, when its off, the relay goes off

Astronouth7303 17-03-2004 18:47

Re: Help! Programming question with triggers
 
Quote:

Originally Posted by KenWittlief
you dont need an if statement

just something like

relay_fwd01 = p1_trig01; in your code

when the switch is on the relay is on, when its off, the relay goes off

If you have a victor, try this:
Code:

pwm01=p1_sw_trig*127+127;

Ryan M. 17-03-2004 18:48

Re: Help! Programming question with triggers
 
If they are on PWMs, do something like:
Code:

if(switch == 1){
    pwm01 = pwm02 = 254;
}
else{
    pwm01 = pwm02 = 127;
}

Where pwm is the motor(s) and switch is the switch.

Xufer 17-03-2004 19:26

Re: Help! Programming question with triggers
 
i did mine like this
Code:

pwm03 = 127;  /*goal claw up and down*/
if (p2_sw_top)
{pwm03 = 195;}  /*half speed down*/
else if (p2_sw_trig)
{pwm03 = 59;} /*half speed up*/


AsimC 17-03-2004 20:21

Re: Help! Programming question with triggers
 
hmm this is how i originally did it...but it doesnt work. What happens is when i click the trigger, its constantly going forward even when i let go.

Code:

if (p3_sw_trig==1)
{
  pwm07=175;
  pwm08=175;
}
if (p3_sw_top==1)
{
  pwm07=90;
  pwm08=90;
}


Mr. Lim 17-03-2004 20:26

Re: Help! Programming question with triggers
 
Code:

if (p3_sw_trig==1)
{
  pwm07=175;
  pwm08=175;
}
else if (p3_sw_top==1)
{
  pwm07=90;
  pwm08=90;
}
else
{
  pwm07=127;
  pwm08=127;
}

try this... just beware if you push both the trigger and top button at the same time, it'll still go forward.

AsimC 17-03-2004 20:35

Re: Help! Programming question with triggers
 
i forgot to say...it is a self centering toggle switch. Toggle forward is wired up to "trigger", and toggle back is wired up to "top" in code.

Anyways... Slimbojones when i was debugging my code, i tried something like that... and it didnt work. What happens is the 1 from p3_sw_trig is held in the code, so it never goes back to 0 and that means itll never go to neutral.

WebWader125 17-03-2004 20:44

Re: Help! Programming question with triggers
 
Quote:

Originally Posted by AsimC
Anyways... Slimbojones when i was debugging my code, i tried something like that... and it didnt work. What happens is the 1 from p3_sw_trig is held in the code, so it never goes back to 0 and that means itll never go to neutral.

Did you try something like that, or did you try exactly what Slimbo posted? If you had two IF's but only one ELSE, it would probably behave how you've described.

One question I have: What happens when you push the toggle switch down? Do your motors actually run in reverse then? Or is forward the only direction that's working at all for you?

doy 17-03-2004 20:44

Re: Help! Programming question with triggers
 
Quote:

Originally Posted by AsimC
i forgot to say...it is a self centering toggle switch. Toggle forward is wired up to "trigger", and toggle back is wired up to "top" in code.

Anyways... Slimbojones when i was debugging my code, i tried something like that... and it didnt work. What happens is the 1 from p3_sw_trig is held in the code, so it never goes back to 0 and that means itll never go to neutral.

if at the rest state of the switch, both outputs are 0, then his code should work... if not, then i dont think the switch will work for what you want it to do, as there would be no way to tell whether or not the switch was being pushed in a direction or at rest.

AsimC 17-03-2004 20:50

Re: Help! Programming question with triggers
 
when i pull the toggle switch back...the motors do run in reverse...but again they do not stop even when i let go.

The Lucas 17-03-2004 21:09

Re: Help! Programming question with triggers
 
Quote:

Originally Posted by AsimC
when i pull the toggle switch back...the motors do run in reverse...but again they do not stop even when i let go.

Do your motors run at startup?

Greg 17-03-2004 21:17

Re: Help! Programming question with triggers
 
Quote:

Originally Posted by AsimC
i forgot to say...it is a self centering toggle switch. Toggle forward is wired up to "trigger", and toggle back is wired up to "top" in code.

Anyways... Slimbojones when i was debugging my code, i tried something like that... and it didnt work. What happens is the 1 from p3_sw_trig is held in the code, so it never goes back to 0 and that means itll never go to neutral.

Then its your switch. Just because it returns to center does not mean the switch is opened :)

AsimC 17-03-2004 21:18

Re: Help! Programming question with triggers
 
startup meaning when i turn the robot on?.... nope they dont move

The Lucas 17-03-2004 21:22

Re: Help! Programming question with triggers
 
if you push it in reverse and let go does it keep going in reverse? or go forward?

[EDIT] I also think it is a switch wiring problem [/EDIT]

AsimC 17-03-2004 21:28

Re: Help! Programming question with triggers
 
the thing is that....the SAME EXACT thing happens when i hooked up a joystick to it...so i doubt its an electrical problem.

*EDIT - using the triggers from a joystick

The Lucas 17-03-2004 21:33

Re: Help! Programming question with triggers
 
Could you post the file then? The algorithms on this thread are good so the problem is somewhere else. I am sure with all the CD programmers someone will find it real quick.

KenWittlief 17-03-2004 22:16

Re: Help! Programming question with triggers
 
Quote:

Originally Posted by AsimC
hmm this is how i originally did it...but it doesnt work. What happens is when i click the trigger, its constantly going forward even when i let go.

Code:

if (p3_sw_trig==1)
{
  pwm07=175;
  pwm08=175;
}
if (p3_sw_top==1)
{
  pwm07=90;
  pwm08=90;
}



the reason why your motors keep running is because there is no path in your code where you tell them to be = 127 and stop running

the way your code is written if neither switch is closed then the pwm outputs will still be what they were the last time the loop ran, and the time before that... your code leaves them at 175 or 90.

you could put

pwm07=127;
pwm08=127;

above your if statement - have them = 127 by default, then if either switch is closed they will be something else instead

this is a common mistake

Xufer 17-03-2004 22:48

Re: Help! Programming question with triggers
 
try this its the code i use to run my claw modified for your application
Code:

pwm07=127;
pwm08=127;

if (p3_sw_top)
{ pwm07 = 90;
  pwm08 = 90; }

else if (p3_sw_trig)
{ pwm07 = 175;
  pwm08 = 175;}


adventrx327 17-03-2004 23:00

Re: Help! Programming question with triggers
 
Here is how i would write it if i were you guys....

if ( 0 != p3_sw_trig)
{
pwm07 = 175;
pwm08 = 175;
}
else if (0 != p3_sw_top)
{
pwm07 = 90;
pwm08 = 90;
}
else
{ pwm07 = 127;
pwm08 = 127;
}


When writing my code i didnt do p3_sw_top == 1, my mentor sez its better to do 0 !=, and i have no idea why.

try this, if it works yay! if not sorry i couldnt be of more help

Joe Ross 17-03-2004 23:07

Re: Help! Programming question with triggers
 
Quote:

Originally Posted by adventrx327
When writing my code i didnt do p3_sw_top == 1, my mentor sez its better to do 0 !=, and i have no idea why.

Here is the reason: http://www.chiefdelphi.com/forums/sh...34&postcount=3 However, in the future don't be afraid to ask your mentor to explain things like that you don't understand.

Anthony Kesich 17-03-2004 23:20

Re: Help! Programming question with triggers
 
Code:

pwm07 = pwm08 = 127 + ((p3_sw_trig - p3_sw_top) * 40);
One line. I found it very useful this year. You can changew the numher at the end (40) to whatever scale you want. trig will make it go forward, top will make it go back, and either both or none will make it just sit there.

-Kesich

adventrx327 18-03-2004 00:30

Re: Help! Programming question with triggers
 
Oh my god!

So concise, so effective, so 1337!

I salute you Anthony, you truly embody the spirit of first.

KenWittlief 18-03-2004 07:01

Re: Help! Programming question with triggers
 
it is concise in C - but unless the CPU has a HW multiplyer then multiplyer that one line of code might compile into something that will take hundreds of machine cycles to execute in assembler

when you only need an '=' getting clever to make your C look concise is usually a big mistake.

The Lucas 18-03-2004 11:00

Re: Help! Programming question with triggers
 
Quote:

Originally Posted by KenWittlief
it is concise in C - but unless the CPU has a HW multiplyer then multiplyer that one line of code might compile into something that will take hundreds of machine cycles to execute in assembler

when you only need an '=' getting clever to make your C look concise is usually a big mistake.

Actually both routines, Anthony Kesich's one-liner and Xufer's selection structure, take exactly 22 bytes of program space, just check the list file (FrcCode.lst) after compiling both. Xufers routine was the most program space efficient of the selection structures I saw because it involved the least branching (i.e. no else to set neutral which only takes 2 bytes). Anthony’s routine could probably be optimized in assembler since only the sign bit and least significant bit of one of the factors is relevant (I don't program in assembler so I am not sure). One-liners are much better for relays because it is simply addition no multiplication.

Processor speed is a different story and depends on the processor architecture.


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

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