View unanswered posts | View active topics It is currently Fri Apr 18, 2014 2:38 pm






Reply to topic  [ 77 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6  Next
Trying to get Sonar Sensor to work on Autonomous 
Author Message
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Trying to get Sonar Sensor to work on Autonomous
Does it mean your joystick problem is solved? If so, do you mind sharing what you found so other people may benefit from similar problems?
Regarding your magnet sensor problem. Your code doesn't look correct to me. First, the magnet sensor returns a non-zero value even though there is no magnet around. If there is a magnet around and it's the north pole, the value goes up and if it's the south pole, the value goes down (or the other way around, I don't remember). Your first line of code has the right idea:
Code:
if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))

But then the following lines of code doesn't make sense:
Code:
if(SensorValue[magnetSensor] == 1)
  {
  motor[gate2] = 50;
  wait1Msec(125);
  }
else
{
  motor[gate2] = 0;
}

This code pretty much said, "run the gate if the magnet sensor has a value 1, otherwise stop". I don't think the magnetSensor will ever give you a value of 1. So the gate motor should never run at all.
What you really want is the following code:
Code:
bool magnetPresent = abs(SensorValue[magnetSensor] - NO_MAGNET_VALUE) > MAGNET_THRESHOLD;
if (magnetPresent)
{
  motor[gate2] = 50;
  wait1Msec(125);
}
else
{
  motor[gate2] = 0;
}

Since you only posted a sniplet of the code, it is hard to say if this will work when you replace your code with the above code. It doesn't show the robot main loop so I can't tell what happened on the next loop. Also what happened when gate2 is opened and the magnet ball has pass through it. Will the magnet sensor register no magnet then? Is the gate2 motor an NXT motor with encoder? If so, would it be more appropriate to use the built-in encoder to check if the gate is opened fully instead of just running the motor 125 msec? Without the enocder telling you the position of the gate, your gate will eventually be misaligned (e.g. not close fully, open too far etc).


Sun Dec 11, 2011 11:45 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
What happened was That on the program i had the set up on the program as Motor, motor, servo, motor while it should of been motor contorl, motor contorl, motor contorl, then servo contorl. Make sure on the program that you have it set up as the same as the robot has set up. If you set it up wrong something will not run. So make sure all of the motor and servo contorls on the wireing is the same on the program if not. Then you will things that will not run...


As for the Motor gate2. it is a lego motor we are using. The frist one runs fine. The 2nd one we have trobles with i will see if we can use a encoder to check the gate. Thanks


- Dtrain3455
and here is the whole code if u wanna look at it
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTMotor,  HTServo)
#pragma config(Sensor, S1,     ,                    sensorI2CMuxController)
#pragma config(Sensor, S2,     magnetSensor,        sensorHiTechnicMagnetic)
#pragma config(Motor,  motorA,          gate1,         tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          gate2,         tmotorNormal, PIDControl, reversed, 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_C3_1,     motorH,        tmotorNone, openLoop)
#pragma config(Motor,  mtr_S1_C3_2,     motorI,        tmotorNone, openLoop)
#pragma config(Servo,  srvo_S1_C4_1,    servo1,               tServoStandard)
#pragma config(Servo,  srvo_S1_C4_2,    magnetExit,           tServoStandard)
#pragma config(Servo,  srvo_S1_C4_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_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 600

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

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(7) == 1)
{
  motor[gate1] = 30;
  wait1Msec(125);
}
else if (joy2Btn(8) == 1)
{
motor[gate1] = -30;
  wait1Msec(125);

}
else
{
motor[gate1] = 0;
}

 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;
}
    //
    // -- Outer Gates
    //
     if (joy2Btn(2) == 1)
    {
      servo[magnetExit] = 0;
     }
     else
   {
     servo[magnetExit] = 127;
   }
     if (joy2Btn(1) == 1)
     {
       servo[servo1] = 200;
     }
    else
     {
       servo[servo1] = 0;
     }
    }
return;}


Mon Dec 12, 2011 10:39 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Trying to get Sonar Sensor to work on Autonomous
I found several problems in your code.
First, what is motorH? It looks like you have it turned on when joystick 1 button 8 is pressed but there is no code to stop it. It could be fine but not knowing what motorH is for, I can't tell if it is a problem.
Code:
    // -- Motor H
    if (joy1Btn(8) == 1)
    {
      motor[motorH] = -5;
    }

Like I said in my previous post, the gate2 code is wrong and needs to be fixed possibly with encoder. While you are at it, gate1 should use the encoder too.


Mon Dec 12, 2011 12:32 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
i forogt the 0 my bad xD and ok i will use it :programmer: :bigthumb:


Mon Dec 12, 2011 4:53 pm
Profile WWW
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
the team dosen't want to use encoders so we are going to find a way around it some how


Mon Dec 12, 2011 4:59 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Trying to get Sonar Sensor to work on Autonomous
dtrain34555 wrote:
the team dosen't want to use encoders so we are going to find a way around it some how

Your gate motors are NXTs, right? Then, you already have encoders built-in. The only thing is to write code to use them.


Mon Dec 12, 2011 6:24 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
Yes, but when i used them or try to use them iut wonldn't work. The other team even had troble.


Tue Dec 13, 2011 5:25 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Trying to get Sonar Sensor to work on Autonomous
The code should be very simple. Instead of
Code:
bool magnetPresent = abs(SensorValue[magnetSensor] - NO_MAGNET_VALUE) > MAGNET_THRESHOLD;
if (magnetPresent)
{
  motor[gate2] = 50;
  wait1Msec(125);
}
else
{
  motor[gate2] = 0;
}

You just do:
Code:
bool magnetPresent = abs(SensorValue[magnetSensor] - NO_MAGNET_VALUE) > MAGNET_THRESHOLD;
if (magnetPresent)
{
  while (nMotorEncoder[gate2] < GATE2_OPEN_POS)
  {
    motor[gate2] = 50;
  }
  motor[gate2] = 0;
}
else
{
  while (nMotorEncoder[gate2] > GATE2_CLOSE_POS)
  {
    motor[gate2] = -50;
  }
  motor[gate2] = 0;
}


Tue Dec 13, 2011 5:39 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
What do you mean by GATE2_OPEN_POS and GATE2_close_POS?


Tue Dec 13, 2011 5:56 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Trying to get Sonar Sensor to work on Autonomous
You have to determine what are the encoder values when gate2 is opened and closed. If you reset the encoder value to zero before entering the task main loop when gate2 is closed, then GATE2_CLOSE_POS is probably zero. So for example:
Code:
#define GATE2_CLOSE_POS        0
#define GATE2_OPEN_POS        ???      //You have to determine this number     
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. */

   nMotorEncoder[gate2] = 0;

    while(true)
    {
        getJoystickSettings(joystick);
        ....
        ....
}



Tue Dec 13, 2011 6:09 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Trying to get Sonar Sensor to work on Autonomous
ok thabnnks :D and now on the samtha feild contorl it says we have no tele op set do you know what it means or what we have to do?


Tue Dec 13, 2011 6:42 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Trying to get Sonar Sensor to work on Autonomous
Assuming you have downloaded your teleop program to the NXT, you need to set the name of your teleop program in the NXT. The easiest way to do it is to bring up the joystick (FTC) dialog and there is a drop down box that let you select the program used for teleop.


Tue Dec 13, 2011 6:46 pm
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Lego motor to servo
We arte going to have servo3 for a place for the magnet senor.

Code:

#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTMotor,  HTServo)
#pragma config(Sensor, S1,     ,                    sensorI2CMuxController)
#pragma config(Sensor, S2,     magnetSensor,        sensorHiTechnicMagnetic)
#pragma config(Motor,  motorA,          gate1,         tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          gate2,         tmotorNormal, PIDControl, reversed, 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_C3_1,     motorH,        tmotorNone, openLoop)
#pragma config(Motor,  mtr_S1_C3_2,     motorI,        tmotorNone, openLoop)
#pragma config(Servo,  srvo_S1_C4_1,    servo1,               tServoStandard)
#pragma config(Servo,  srvo_S1_C4_2,    magnetExit,           tServoStandard)
#pragma config(Servo,  srvo_S1_C4_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_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 600

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

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(7) == 1)
{
  motor[gate1] = 30;
  wait1Msec(125);
}
else if (joy2Btn(8) == 1)
{
motor[gate1] = -30;
  wait1Msec(125);

}
else
{
motor[gate1] = 0;
}
 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 Servo3 opens, and gate1 closes
   //
   if(SensorValue[magnetSensor] == 1)

{
servo[servo3] = 127;
}
else
{
  servo[servo3] = 0;
}
    //
    // -- Outer Gates
    //
     if (joy2Btn(2) == 1)
    {
      servo[magnetExit] = 0;
     }
     else
   {
     servo[magnetExit] = 127;
   }
     if (joy2Btn(1) == 1)
     {
       servo[servo1] = 200;
     }
    else
     {
       servo[servo1] = 0;
     }
    }
return;}


For some reason it won''t even move a bit.


Wed Dec 14, 2011 4:33 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Trying to get Sonar Sensor to work on Autonomous
It looks like this is the same code you posted before, meaning I have already pointed out several problems and suggested fixes. You haven't put any of the suggested fixes in the code, so it remained not working. Please refer my previous posts for the fixes.


Last edited by MHTS on Wed Dec 14, 2011 4:54 pm, edited 1 time in total.



Wed Dec 14, 2011 4:44 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1343
Post Re: Trying to get Sonar Sensor to work on Autonomous
In case you can't find the post, here it is:
MHTS wrote:
Does it mean your joystick problem is solved? If so, do you mind sharing what you found so other people may benefit from similar problems?
Regarding your magnet sensor problem. Your code doesn't look correct to me. First, the magnet sensor returns a non-zero value even though there is no magnet around. If there is a magnet around and it's the north pole, the value goes up and if it's the south pole, the value goes down (or the other way around, I don't remember). Your first line of code has the right idea:
Code:
if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))

But then the following lines of code doesn't make sense:
Code:
if(SensorValue[magnetSensor] == 1)
  {
  motor[gate2] = 50;
  wait1Msec(125);
  }
else
{
  motor[gate2] = 0;
}

This code pretty much said, "run the gate if the magnet sensor has a value 1, otherwise stop". I don't think the magnetSensor will ever give you a value of 1. So the gate motor should never run at all.
What you really want is the following code:
Code:
bool magnetPresent = abs(SensorValue[magnetSensor] - NO_MAGNET_VALUE) > MAGNET_THRESHOLD;
if (magnetPresent)
{
  motor[gate2] = 50;
  wait1Msec(125);
}
else
{
  motor[gate2] = 0;
}

Since you only posted a sniplet of the code, it is hard to say if this will work when you replace your code with the above code. It doesn't show the robot main loop so I can't tell what happened on the next loop. Also what happened when gate2 is opened and the magnet ball has pass through it. Will the magnet sensor register no magnet then? Is the gate2 motor an NXT motor with encoder? If so, would it be more appropriate to use the built-in encoder to check if the gate is opened fully instead of just running the motor 125 msec? Without the enocder telling you the position of the gate, your gate will eventually be misaligned (e.g. not close fully, open too far etc).


Wed Dec 14, 2011 4:51 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 77 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.