ROBOTC.net forums
http://www.robotc.net/forums/

Trying to get Sonar Sensor to work on Autonomous
http://www.robotc.net/forums/viewtopic.php?f=52&t=3666
Page 4 of 6

Author:  dtrain34555 [ Tue Dec 06, 2011 10:24 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

I am not sure why he would just want it so you could bring it up and stop it. I will talk to the mentor and see if he will mind if i change it from joy stick to button. But He might just want it the way it is too xD he is hard to understand.. xD

Author:  dtrain34555 [ Sat Dec 10, 2011 3:51 pm ]
Post subject:  Sevro button isssue

We are trying to get magnetservo to work and when we push a button nothing happens we notice none of the other buttons work on the 2nd contorl. here is the code

Code:
   //
   // -- Inter gates
   //
   if (joy2Btn(8) == 1)
    {
      servo[gate1] = 220;
    }
   if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
   //
   // If the magnetSenor is Inactive gate1 stays closed. If magnetSenor Is Active gate2 opens, and gate1 closes
   //
   if(SensorValue[magnetSensor] == 1)
    {
      servo[gate2] = 220;
    }
    //
    // -- Outer Gates
    //
     if (joy2Btn(5) == 1)
    {
      servo[magnetExit] = 255;
     }
     if (joy2Btn(6) == 1)
     {
       servo[servo1] = 255;
     }
    }


The full code is this
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTServo,  HTMotor)
#pragma config(Sensor, S2,     magnetSensor,        sensorHiTechnicMagnetic)
#pragma config(Sensor, S3,     touchSensor,         sensorTouch)
#pragma config(Sensor, S4,     IRSensor,            sensorHiTechnicIRSeeker1200)
#pragma config(Motor,  motorA,          gate1,         tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          gate2,         tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorNormal, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorNormal, openLoop)
#pragma config(Motor,  mtr_S1_C2_1,     conveyorMotor, tmotorNormal, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C2_2,     sweeperMotor,  tmotorNormal, openLoop)
#pragma config(Motor,  mtr_S1_C4_1,     motorH,        tmotorNormal, openLoop)
#pragma config(Motor,  mtr_S1_C4_2,     motorI,        tmotorNormal, openLoop)
#pragma config(Servo,  srvo_S1_C3_1,    magnetExit,           tServoStandard)
#pragma config(Servo,  srvo_S1_C3_2,    servo2,               tServoStandard)
#pragma config(Servo,  srvo_S1_C3_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C3_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C3_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C3_6,    servo6,               tServoNone)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"

//Deviation/Proximity of the Magnet Determines this value
#define MAGNET_THRESHOLD     5

//Value of the sensor when nothing is detected
#define NO_MAGNET_VALUE 654

//Value of the sensor when somthing is detected
#define MAGNET_VALUE 660

task main()
{
    const int threshold = 20; /* Int 'threshold' will allow us to ignore low */
   bool bConveyorBtnPressed = false;
   bool bConveyorOn = false;
   bool bSweeperBtnPressed = false;
   bool bSweeperOn = false;

    /* readings that keep our robot in perpetual motion. */
    while(true)
    {
    getJoystickSettings(joystick);
    // -- Control Motor D

    if(abs(joystick.joy1_y1) > threshold)   // If the left analog stick's Y-axis readings are either above or below the threshold:
    {
      motor[motorD] = joystick.joy1_y1;         // Motor D is assigned a power level equal to the left analog stick's Y-axis reading.
    }
    else                                    // Else if the readings are within the threshold:
    {
      motor[motorD] = 0;                        // Motor D is stopped with a power level of 0.
    }

    // -- Control Motor E

    if(abs(joystick.joy1_y2) > threshold)   // If the right analog stick's Y-axis readings are either above or below the threshold:
    {
      motor[motorE] = joystick.joy1_y2;         // Motor E is assigned a power level equal to the right analog stick's Y-axis reading.
    }
    else                                    // Else if the readings are within the threshold:
    {
      motor[motorE] = 0;                        // Motor E is stopped with a power level of 0.
    }

    // -- Control Motor F/conveyorMotor

   if (!bConveyorBtnPressed && (joy1Btn(5) == 1))
    {
      //
      // detected a button pressed event, toggle the conveyor ON or OFF.
      //
      bConveyorBtnPressed = true;
      bConveyorOn = !bConveyorOn;
    }
    else if (bConveyorBtnPressed && (joy1Btn(5) == 0))
    {
      //
      // detected a button release event.
      //
      bConveyorBtnPressed = false;
    }

    if (bConveyorOn)
    {
      motor[conveyorMotor] = 70;
    }
    else
    {
      motor[conveyorMotor] = 0;
    }

    //-- Control Motor G/Sweeper

    if (!bSweeperBtnPressed && (joy1Btn(6) == 1))
       {
    //
    // detected a button pressed event, toggle the sweeper ON or OFF.
    //
    bSweeperBtnPressed = true;
    bSweeperOn = !bSweeperOn;
    }
      else if (bSweeperBtnPressed && (joy1Btn(6) == 0))
    {
    //
    // detected a button release event.
    //
    bSweeperBtnPressed = false;
    }

    if (bSweeperOn)
    {
      motor[sweeperMotor] = 80;
    }
    else
    {
      motor[sweeperMotor] = 0;
    }

    // -- Motor H
    if (joy1Btn(8) == 1)
    {
      motor[motorH] = -5;
    }

    /////////////////////////////////////////////////////////////////
    // -- Control Magnet Sensor
    /////////////////////////////////////////////////////////////////
    nxtDisplayTextLine(1, "Magnet: %d", SensorValue[magnetSensor]);   // display magnet sensor value

    if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD))
    {
      nxtDisplayTextLine(2, "Magnet(NorthTop)");
    }
    else if(SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
    {
      nxtDisplayTextLine(2, "Magnet(SouthTop)");
    }
    else
    {
      nxtDisplayTextLine(2, "Nothing Detected");
    }
    wait1Msec(3);

   //
   // -- Inter gates
   //
   if (joy2Btn(8) == 1)
    {
      servo[gate1] = 220;
    }
   if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
   //
   // If the magnetSenor is Inactive gate1 stays closed. If magnetSenor Is Active gate2 opens, and gate1 closes
   //
   if(SensorValue[magnetSensor] == 1)
    {
      servo[gate2] = 220;
    }
    //
    // -- Outer Gates
    //
     if (joy2Btn(5) == 1)
    {
      servo[magnetExit] = 255;
     }
     if (joy2Btn(6) == 1)
     {
       servo[servo1] = 255;
     }
    }
return;}

everything works just not the 2nd contorler buttons for the servos won't work.

Author:  MHTS [ Sat Dec 10, 2011 5:29 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

Did you enable dual joystick?

Author:  dtrain34555 [ Sun Dec 11, 2011 1:27 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

We even tried that and still nothing happened with the servo's.. :(

Author:  MHTS [ Sun Dec 11, 2011 2:44 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

If dual joystick was enabled, when you press the buttons on either joysticks, did you see the corresponding joystick buttons responding in the joystick dialog picture? If it doesn't, that's the problem. You need to figure out why the joystick dialog doesn't receive button events from that joystick. One thing to check is that if the two joysticks are associated correctly. In the dual joystick dialog, each joystick should have a drop-down menu let you pick the joystick association. Make sure each joystick is associated with a different physical joystick. If you associated both joysticks to the same physical joystick, pressing the buttons on the associated physical joystick will get responses from both joysticks in the dialog while pressing buttons on the other physical joystick will get nothing. If everything works correctly in the dual joystick dialog, then you should add the following lines in your code to display debug info to your NXT LCD screen:
Code:
 //
   // -- Inter gates
   //
   if (joy2Btn(8) == 1)
    {
      servo[gate1] = 220;
      nxtDisplayTextLine(3, "Btn8 pressed");
    }
    else
    {
      nxtDisplayTextLine(3, "Btn8 released");
    }
   if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
   //
   // If the magnetSenor is Inactive gate1 stays closed. If magnetSenor Is Active gate2 opens, and gate1 closes
   //
   if(SensorValue[magnetSensor] == 1)
    {
      servo[gate2] = 220;
    }
    //
    // -- Outer Gates
    //
     if (joy2Btn(5) == 1)
    {
      servo[magnetExit] = 255;
     }
     if (joy2Btn(6) == 1)
     {
       servo[servo1] = 255;
     }
    }

If the LCD screen does show "Btn8 pressed" or "Btn8 released" when you press and release button 8 then you have a problem with your servo. Either there is a problem with the wiring of the servo or some components in the path have become bad such as the servo controller or the servo motors themselves. Also make sure the pragmas declaring the servo motors match the actual wiring. For example, according to your pragmas, the NXT sensor port 1 is connected to the first motor controller which daisy chained to a second motor controller which daisy chained to the servo controller which daisy chained to a third motor controller. Make sure the daisy chain seqence is correct. Also make sure the red power LEDs on all the controllers lit up when the main power switch is ON.

Author:  dtrain34555 [ Sun Dec 11, 2011 2:58 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

The contorl is on both now but i am still having troble with this

Code:
 //nMotorEncoder[gate1] = 0;  //clear the LEGO encoders in motors B and C+

  while (nMotorEncoder[motorC] < 70) //while the encoder wheel turns one revolution
  {
    motor[gate1] = 30; //turn both motors on at 30 percent power
  }
  motor[gate1] = 0; //turn both motors off

  wait1Msec(3000); // wait 3 seconds to see feedback from the debugger screens
                    // open the "NXT Devices" window to see the distance the encoder
                    //spins. The robot will coast a little bit.
}
else if (joy2Btn(8) == 1)
{
{

  //nMotorEncoder[motorC] = 0;  //clear the LEGO encoders in motors B and C+


  while (nMotorEncoder[gate1] < 0) //while the encoder wheel turns one revolution
  {
    motor[gate1] = -30; //turn both motors on at 30 percent power
  }
  motor[gate1] = 0; //turn both motors off
  wait1Msec(3000); // wait 3 seconds to see feedback from the debugger screens
                    // open the "NXT Devices" window to see the distance the encoder
                    //spins. The robot will coast a little bit.
}

Author:  dtrain34555 [ Sun Dec 11, 2011 3:04 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

What we are trying to do is open so much for a ball to go through it.so we want it to open then close when button is not prsssed we tried changing it to 7 and 8 as you can see but still it will not close.

Author:  MHTS [ Sun Dec 11, 2011 3:10 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

Sorry, I don't understand your replies and I also don't understand the last code you posted. Can you be more specific on what's not working? Also, I have suggested a lot of things to try in my last post. What are the results? I need to know the results so I can rule out the possibilities.

Author:  dtrain34555 [ Sun Dec 11, 2011 3:28 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

That other code that you said had alot of problems were a code for a mentor.... not for the robot that i am useing. This code i am showing you now is the code i been staying with while i been showing you it xD haha but thye logo motor moves now but still not the servo will not move.... this is the code for the servo

Code:

   if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
   //
   // If the magnetSenor is Inactive gate2 stays closed. If magnetSenor Is Active gate2 opens, and if gate1 is not closed it will close.
   //
   if(SensorValue[magnetSensor] == 1)
    {
      motor[gate2] = 50;
    }
    //
    // -- Outer Gates
    //
     if (joy2Btn(5) == 1)
    {
      servo[magnetExit] = 0;
     }
     else
   {
     servo[magnetExit] = 127;
   }
     if (joy2Btn(6) == 1)
     {
       servo[servo1] = 230;
     }
     //else
     //{
     //close servo1
     //}
    }


Servo 1 will not move

Author:  MHTS [ Sun Dec 11, 2011 3:36 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

Like I said, add the following debug code and see if pressing button 6 on the joystick will show anything on the LCD screen:
Code:
     if (joy2Btn(6) == 1)
     {
       servo[servo1] = 230;
       nxtDisplayTextLine(3, "Btn6 pressed");
     }
     //else
     //{
       nxtDisplayTextLine(3, "Btn6 released");
     //close servo1
     //}

If it doesn't, you have a joystick problem. If it does, you have a servo problem.

Author:  dtrain34555 [ Sun Dec 11, 2011 3:51 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

And it never says it is pressed when i press it down

Author:  dtrain34555 [ Sun Dec 11, 2011 4:04 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

I wi;look into it and see what is happening

Author:  dtrain34555 [ Sun Dec 11, 2011 4:46 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

it's all goood all fixed i found the problem thx

Author:  MHTS [ Sun Dec 11, 2011 4:52 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

dtrain34555 wrote:
And it never says it is pressed when i press it down

So it's a joystick problem. I have suggested some steps to diagnose the problem in my previous post. Also make sure you did call getJoystickSettings somewhere in your robot main loop.

Author:  dtrain34555 [ Sun Dec 11, 2011 10:31 pm ]
Post subject:  Re: Trying to get Sonar Sensor to work on Autonomous

The only thing that now has some issues that keep acting up is this part of the program. What i want it to do is have it when it sees a higher than the Magnet value... It will open gate2 then when not close it.
Code:
 if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
   //
   // If the magnetSenor is Inactive gate1 stays closed. If magnetSenor Is Active gate2 opens, and gate1 closes if not closed
   //
 if(SensorValue[magnetSensor] == 1)
  {
  motor[gate2] = 50;
  wait1Msec(125);
  }
else
{
  motor[gate2] = 0;
}


We also have been seeing it going on and on after the magnet goes so we want it to have a stop button point or something like that so it doesn't try to keep going. But also while it is looking for the magnet value to go up we want to make sure the button only works if the gate2 keeps running.

Page 4 of 6 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/