View unanswered posts | View active topics It is currently Tue Jul 22, 2014 5:37 am






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: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
Xander's driver suite also has the IR Seeker support. You can take a look at that and the associated sample code. Also, there is some info on IR seeker accuracy in this thread.
viewtopic.php?f=52&t=2780&hilit=accuracy

We are attending a regional competition today. So I will not have time to look over your code this weekend.


Sat Nov 19, 2011 3:31 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
Hey, we have a revised code here that we'd like you to check:

#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Sensor, S1, , sensorI2CMuxController)
#pragma config(Sensor, S2, magnetSensor, sensorHiTechnicMagnetic)
#pragma config(Sensor, S3, touchSensor, sensorTouch)
#pragma config(Sensor, S4, IRSensor, sensorHiTechnicIRSeeker1200)
#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, motorF, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop)
#pragma config(Servo, srvo_S1_C3_1, servo1, tServoStandard)
#pragma config(Servo, srvo_S1_C3_2, servo2, tServoContinuousRotation)
#pragma config(Servo, srvo_S1_C3_3, servo3, tServoContinuousRotation)
#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 */
/* 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
if(abs(joystick.joy2_y1) > threshold) // If the left analog stick's Y-axis readings are either above or below the threshold:
{
motor[motorF] = joystick.joy2_y1; // Motor F 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[motorF] = 0; // Motor F is stopped with a power level of 0.
}

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

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

if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
{
servo[servo1] = 0;
}
if(SensorValue[touchSensor] == 1)
{
servo[servo1] = 90;
servo[servo2] = 190;
}

{
nxtDisplayTextLine(1,"test display");
}
if(joy2Btn(2) == 1 && joy2Btn(3) == 0)
{
servo[servo3] = 200;
nxtDisplayTextLine(2, "btn2==1");
}
else if (joy2Btn(3) == 1 && joy2Btn(2) == 0)
{
servo[servo3] = 80;
nxtDisplayTextLine(2, "btn3==1");
}
else
{
servo[servo3] = 127;
nxtDisplayTextLine(3, "no btn");
}
}
return;}


Our University of Iowa mentor was helping us a little with the program, we were beyond stuck. It always helps to have a second opinion, however. Thank you for all of your help here, we would've had a LOT more trouble if it wasn't for you guys! :lol:

Max


Tue Nov 22, 2011 5:29 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
Also, are which "I.R. Seeker" are we supposed to use in the programming?

Max


Tue Nov 22, 2011 6:30 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
First, you may want to rename your motors and servos to names that reflect what they are used for. motorD, motorE, ..., servo1, servo2 don't mean anything to me when reading the code. For example, leftMotor, rightMotor, clawServo, armServo are a lot more meaningful.
Secondly, joystick range is from -128 to 127 but motor range is from -100 to 100. Although reading the joystick and assign its value directly to the motor works, the range between 100 to 127 becomes a clip zone. So you may want to scale the joystick range to the motor range before the assignment. This comment applies to motorD, E and F.
Thirdly, don't know what servo1, servo2 and servo3 do, so can't comment on if they are correct.
It will help us to help you if you can add comment to your code on your intention so that we can check your code against the intention. Otherewise, there is not much I can say about its correctness. Also, you did not mention what problems you have except you are "beyond stuck". I don't know how to fix "beyond stuck". :roll:
Also, it would help if you put your code inside the "Code" box by clicking the "Code" button above such as the example below. This will keep the code indentation that will help tremendously when reading the code.
Code:
if (something)
{
    do something;
}
else
{
    do something else;
}


Tue Nov 22, 2011 8:29 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 am trying to Get a button control on a convor belt. It is runing on a motor i just want to Make it run when you push button 1


Code:

// -- Control Motor F
if(abs(joystick.joy2_y1) > threshold) // If the left analog stick's Y-axis readings are either above or below the threshold:
{
motor[motorF] = joystick.joy2_y1; // Motor F 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[motorF] = 0; // Motor F is stopped with a power level of 0.
}


Sat Dec 03, 2011 12:27 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
Are you trying to use button 1 to toggle the conveyor on and off. If so, try this:
Code:
bool bButton1Pressed = false;
bool bConveyorOn = false;
while (true)
{
    if (!bButton1Pressed && (Joy1Btn(1) == 1))
    {
        //
        // detected a button pressed event, toggle the conveyor ON or OFF.
        //
        bButton1Pressed = true;
        bConveyorOn = !bConveyorOn;
    }
    else if (bButton1Pressed && (Joy1Btn(1) == 0))
    {
        //
        // detected a button release event.
        //
        bButton1Pressed = false;
    }

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


Sat Dec 03, 2011 12:58 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
We tried adding it to this program

#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Sensor, S2, magnetSensor, sensorHiTechnicMagnetic)
#pragma config(Sensor, S3, touchSensor, sensorTouch)
#pragma config(Sensor, S4, IRSensor, sensorHiTechnicIRSeeker1200)
#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, motorF, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop)
#pragma config(Servo, srvo_S1_C3_1, servo1, tServoStandard)
#pragma config(Servo, srvo_S1_C3_2, servo2, tServoContinuousRotation)
#pragma config(Servo, srvo_S1_C3_3, servo3, tServoContinuousRotation)
#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 */
/* 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
bool bButton1Pressed = false;
bool bConveyorOn = false;
while (true)
{
if (!bButton1Pressed && (Joy1Btn(1) == 1))
{
//
// detected a button pressed event, toggle the conveyor ON or OFF.
//
bButton1Pressed = true;
bConveyorOn = !bConveyorOn;
}
else if (bButton1Pressed && (Joy1Btn(1) == 0))
{
//
// detected a button release event.
//
bButton1Pressed = false;
}

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

}
/////////////////////////////////////////////////////////////////
// -- 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(25);

if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
{
servo[servo1] = 0;
}
if(SensorValue[touchSensor] == 1)
{
servo[servo1] = 90;
servo[servo2] = 190;
}

// {
// nxtDisplayTextLine(1,"test display");
// }
if(joy2Btn(2) == 1 && joy2Btn(3) == 0)
{
servo[servo3] = 200;
nxtDisplayTextLine(3, "btn2 == 1");
}
else if (joy2Btn(3) == 1 && joy2Btn(2) == 0)
{
servo[servo3] = 80;
nxtDisplayTextLine(3, "btn3 == 1");
}
else
{
servo[servo3] = 127;
nxtDisplayTextLine(3, "no btn");
}
}
return;}

And the robot wouldn't do anything


Sat Dec 03, 2011 3:24 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
It looks like you just cut and pasted my code straight to your program without any modification. First, you already have a "while (true)" loop, so putting another forever loop inside the outer loop will make your program stuck in the inner while loop forever. Secondly, you didn't have the conveyorMotor defined. I am surpise if it even compiled. I have renamed your motors so it makes the code more understandable on what it's trying to do. I don't know what the servos do, so I couldn't rename them. But here is the modified version that you can try.
Code:
#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Sensor, S2, magnetSensor, sensorHiTechnicMagnetic)
#pragma config(Sensor, S3, touchSensor, sensorTouch)
#pragma config(Sensor, S4, IRSensor, sensorHiTechnicIRSeeker1200)
#pragma config(Motor, mtr_S1_C1_1, leftMotor, tmotorNormal, openLoop, reversed)
#pragma config(Motor, mtr_S1_C1_2, rightMotor, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_1, conveyorMotor, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C2_2, motorG, tmotorNormal, openLoop)
#pragma config(Servo, srvo_S1_C3_1, servo1, tServoStandard)
#pragma config(Servo, srvo_S1_C3_2, servo2, tServoContinuousRotation)
#pragma config(Servo, srvo_S1_C3_3, servo3, tServoContinuousRotation)
#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 bButton1Pressed = false;
    bool bConveyorOn = 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[leftMotor] = 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[leftMotor] = 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[rightMotor] = 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[rightMotor] = 0; // Motor E is stopped with a power level of 0.
        }

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

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

        /////////////////////////////////////////////////////////////////
        // -- 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(25);

        if(SensorValue[magnetSensor] > (NO_MAGNET_VALUE + MAGNET_THRESHOLD) || SensorValue[magnetSensor] < (NO_MAGNET_VALUE - MAGNET_THRESHOLD))
        {
            servo[servo1] = 0;
        }

        if(SensorValue[touchSensor] == 1)
        {
            servo[servo1] = 90;
            servo[servo2] = 190;
        }

// {
// nxtDisplayTextLine(1,"test display");
// }
        if(joy2Btn(2) == 1 && joy2Btn(3) == 0)
        {
            servo[servo3] = 200;
            nxtDisplayTextLine(3, "btn2 == 1");
        }
        else if (joy2Btn(3) == 1 && joy2Btn(2) == 0)
        {
            servo[servo3] = 80;
            nxtDisplayTextLine(3, "btn3 == 1");
        }
        else
        {
            servo[servo3] = 127;
            nxtDisplayTextLine(3, "no btn");
        }
    }
    return;
}


Last edited by MHTS on Sat Dec 03, 2011 5:28 pm, edited 1 time in total.



Sat Dec 03, 2011 4:41 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
Oh wait, since I removed the inner while loop, you need to put the two boolean variables outside of the while loop. I have edited the code above to correct it.


Sat Dec 03, 2011 5:27 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
Thx :lol: i will test it :D :bigthumb: :D :D :D :programmer: :programmer: :programmer: :programmer:


Sat Dec 03, 2011 6:11 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
Thank you once again. I re Modified some things on what we were trying to do thank you so much for helping ^_^

-Dtrain3455


Sun Dec 04, 2011 11:44 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
I am Having a Small Problem xD i bet it is really simple but I am Trying to Get a servo to work on the 2nd Joystick But it won't go xD I been trying to fix it but nothing helps it xD Also When I push Button's 5 and 6 servos won't move Outwards

Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTServo,  none)
#pragma config(Sensor, S1,     ,                    sensorI2CMuxController)
#pragma config(Sensor, S2,     magnetSensor,        sensorHiTechnicMagnetic)
#pragma config(Sensor, S3,     touchSensor,         sensorTouch)
#pragma config(Sensor, S4,     IRSensor,            sensorHiTechnicIRSeeker1200)
#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)
#pragma config(Motor,  mtr_S1_C2_2,     motorG,        tmotorNormal, openLoop)
#pragma config(Servo,  srvo_S1_C3_1,    servo1,               tServoStandard)
#pragma config(Servo,  srvo_S1_C3_2,    servo2,               tServoContinuousRotation)
#pragma config(Servo,  srvo_S1_C3_3,    servo3,               tServoContinuousRotation)
#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"

task main()
{
  const int threshold = 20;       /* Int 'threshold' will allow us to ignore low       */
  bool bButton1Pressed = false;
  bool bConveyorOn = 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 (!bButton1Pressed && (joy1Btn(5) == 1))
       {
    //
    // detected a button pressed event, toggle the conveyor ON or OFF.
    //
    bButton1Pressed = true;
    bConveyorOn = !bConveyorOn;
    }
      else if (bButton1Pressed && (joy1Btn(5) == 1))
    {
    //
    // detected a button release event.
    //
    bButton1Pressed = false;
    }

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

    // -- Contorl Motor G

    if(abs(joystick.joy2_y2) > threshold)   // If the right analog stick's Y-axis readings are either above or below the threshold:
    {
      motor[motorG] = joystick.joy2_y2;         // Motor G 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[motorG] = 0;                        // Motor G is stopped with a power level of 0.
    }

    //
    // Arm Contorl
    //

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

    if (joy2Btn(5) == 2)
    {
      servo[servo2] = 128;
    }
    if (joy2Btn(6) == 2)
    {
      servo[servo3] = 128;
    }
  }
return;}


Tue Dec 06, 2011 6:22 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Trying to get Sonar Sensor to work on Autonomous
There are multiple problems with your code.
First, you have changed my original button code. You code is now:
Code:
    if (!bButton1Pressed && (joy1Btn(5) == 1))
    {
      //
      // detected a button pressed event, toggle the conveyor ON or OFF.
      //
      bButton1Pressed = true;
      bConveyorOn = !bConveyorOn;
    }
    else if (bButton1Pressed && (joy1Btn(5) == 1))  <<<< This should be compared to 0, not 1.
    {
      //
      // detected a button release event.
      //
      bButton1Pressed = false;
    }

I think you misunderstood the function joy1Btn(5). This function returns the state of button 5 on joystick 1. It will either return a 1 (button pressed) or a 0 (button not pressed). The above code is basically saying:
Code:
if (button 5 was not pressed and button 5 is now pressed)
{
    set button 5 is in pressed state;
    toggle conveyor state to ON or OFF;
}
else if (button 5 was pressed and button 5 is now not pressed)
{
    set button 5 to unpressed state;
}

Also, since you are changing from button 1 to button 5, you should not name the variable bButton1Pressed. That will confuse you later on. You should change it to bConveyorBtnPressed, like the following:
Code:
  bool bConveyorBtnPressed = false;
  bool bConveyorOn = false;
  ...
  ...
    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;
    }

Also, there is a problem with your arm code. You code is:
Code:
    //
    // Arm Contorl
    //
    if(abs(joystick.joy2_y1) > threshold)   // If the left analog stick's Y-axis readings are either above or below the threshold:
    {
      servo[servo1] = joystick.joy2_y1;         // Sevro1 is assigned a power level equal to the left analog stick's Y-axis reading.
    }
    else                                    // Else if the readings are within the threshold:
    {
      servo[servo1] = 0;                        // Sevrvo1 is stopped with a power level of 0.
    }

According to your pragma, servo1 is a standard servo. Then the value you set to the servo motor should be the angle position (0 - 255). 0 is position 0-degree and 255 is 180-degree. It is not servo motor power. In other words, the above code will change the angle position of your arm according to your joystick 2 y1 postion. If you let go of the joystick, the arm will go back to zero position. It is not "stopping" at the last position you had it. Is this behavior you really want?
Finally, there is also a problem with the code below:
Code:
    if (joy2Btn(5) == 2)
    {
      servo[servo2] = 128;
    }
    if (joy2Btn(6) == 2)
    {
      servo[servo3] = 128;
    }

According to the pragmas, servo2 and servo 3 are continuous rotation servos. It means the value 128 is actually "stop", 255 means going forward at maximum speed. 0 will be going backward at maximum speed. So anything greater than 128 will go forward with increasing speed for bigger values. Anything below 128 will go backward with increasing speed for smaller values.
Like I said, you misunderstood how the function joy2Btn(5) works. joy2Btn(5) can only return either a 0 or a 1. It will never return a value 2. Since I don't really know what you are trying to do, I don't know how to correct this code.


Tue Dec 06, 2011 9:27 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
This is a diffent code for a mentor He needed one. everything worked Just not the servos.


Tue Dec 06, 2011 10:16 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
Also Thanks For making me understand haha The team wanted the number on the control to change and it worked fine i better change that too thank you :D


Tue Dec 06, 2011 10:17 pm
Profile WWW
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.