Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
- #pragma config(Sensor, S1, , sensorI2CMuxController)
- #pragma config(Sensor, S2, HTIRS2, sensorHiTechnicIRSeeker1200)
- #pragma config(Sensor, S3, nxtSonar, sensorSONAR)
- #pragma config(Sensor, S4, nxtColor, sensorCOLORFULL)
- #pragma config(Motor, motorA, tubeArm, tmotorNXT, PIDControl, encoder)
- #pragma config(Motor, mtr_S1_C1_1, leftMotor, tmotorTetrix, openLoop, encoder)
- #pragma config(Motor, mtr_S1_C1_2, rightMotor, tmotorTetrix, openLoop, reversed, encoder)
- #pragma config(Motor, mtr_S1_C2_1, tennisArm, tmotorTetrix, openLoop, encoder)
- #pragma config(Motor, mtr_S1_C2_2, motorG, tmotorTetrix, openLoop, encoder)
- #pragma config(Servo, srvo_S1_C3_1, servo1, tServoNone)
- #pragma config(Servo, srvo_S1_C3_2, servo2, tServoNone)
- #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)
- #define btnpressed() (nNxtButtonPressed != kNoButton) //Checking if any button is pressed or none
- #define btncheck(btn) (nNxtButtonPressed == btn) //Check if a certain button is pressed
- #include "hitechnic-irseeker-v2.h"
- int getbtn(void) { //Wait for a button press, read id, wait for up then return id
- while(!btnpressed()) {/* Sleep(1); */} //wait for button press
- int btn=nNxtButtonPressed; //read which id
- while(btnpressed() ) {/* Sleep(1); */} // wait for button up
- return btn; //return button id5
- }
- int chooser(void) {
- int runno = 1, runmax = 10;
- nxtDisplayCenteredBigTextLine(1, "Chooser");
- while(true)
- {
- nxtDisplayCenteredBigTextLine(4, "Run %d", runno);
- if(getbtn() == 1)//1 = Right Arrow
- {
- runno++;
- if(runno > runmax) runno = 1;
- }
- else if(getbtn() == 2) //2 = Left Arrow
- {
- runno--;
- if(runno < 1) runno = runmax;
- }
- else if(getbtn() == 3) //3 = Orange Button
- {
- nxtDisplayCenteredBigTextLine(6, "Using %d", runno);
- return runno;
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // initializeRobot
- //
- // Prior to the start of autonomous mode, you may want to perform some initialization on your robot.
- // Things that might be performed during initialization include:
- // 1. Move motors and servos to a preset position.
- // 2. Some sensor types take a short while to reach stable values during which time it is best that
- // robot is not moving. For example, gyro sensor needs a few seconds to obtain the background
- // "bias" value.
- //
- // In many cases, you may not have to add any code to this function and it will remain "empty".
- //
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- void initializeRobot()
- {
- nMotorEncoder[leftMotor] = 0;
- nMotorEncoder[rightMotor] = 0;
- nMotorEncoder[tennisArm] = 0;
- return;
- }
- void resetMotor()
- {
- nMotorEncoder[leftMotor] = 0;
- nMotorEncoder[rightMotor] = 0;
- }
- void wait4Inches(int inches) {
- int LeftMotorTarget, RightMotorTarget;
- int EncoderValue;
- bool LeftMotorDone, RightMotorDone;
- // 2 * pi * r = 2*3.14*2 = 12.57in per 360 deg
- // 360/12.57 = 28.65 deg per in
- // 1440 ticks per encoder revolution, 4 ticks per degree
- EncoderValue = inches * 28.65 * 4; //Now in ticks
- nMotorEncoderTarget[leftMotor] = LeftMotorTarget = nMotorEncoder[leftMotor] + EncoderValue;
- nMotorEncoderTarget[rightMotor] = RightMotorTarget = nMotorEncoder[rightMotor] + EncoderValue;
- // doesn't account for overflow of Encoder value
- // wait for 1 motor to reach Target and then wait for the other
- // can also return after first motor reaches Target
- //while(nMotorRunState[motorB] != runStateIdle && nMotorRunState[motorC] != runStateIdle)
- //// while Motor B AND Motor C are still running (haven't yet reached their target):
- //{
- // // do not continue
- //}
- //motor[motorB] = 0; // motor B is stopped at a power level of 0
- //motor[motorC] = 0; // motor C is stopped at a power level of 0
- LeftMotorDone = nMotorEncoder[leftMotor] > LeftMotorTarget;
- RightMotorDone = nMotorEncoder[rightMotor] > RightMotorTarget;
- while(!LeftMotorDone && !RightMotorDone) {
- LeftMotorDone = nMotorEncoder[leftMotor] > LeftMotorTarget;
- RightMotorDone = nMotorEncoder[rightMotor] > RightMotorTarget;
- }
- // can also return after first motor reaches Target
- if (LeftMotorDone) { // Left done, wait for Right
- RightMotorDone = nMotorEncoder[rightMotor] > RightMotorTarget;
- while (!RightMotorDone) {
- RightMotorDone = nMotorEncoder[rightMotor] > RightMotorTarget;
- }
- } else { // Right done, wait for Left
- LeftMotorDone = nMotorEncoder[leftMotor] > LeftMotorTarget;
- while (!LeftMotorDone) {
- LeftMotorDone = nMotorEncoder[leftMotor] > LeftMotorTarget;
- }
- }
- }
- void wait4BackInches(int inches) {
- int LeftMotorTarget, RightMotorTarget;
- int EncoderValue;
- bool LeftMotorDone, RightMotorDone;
- // 2 * pi * r = 2*3.14*2 = 12.57in per 360 deg
- // 360/12.57 = 28.65 deg per in
- // 1440 ticks per encoder revolution, 4 ticks per degree
- EncoderValue = inches * 28.65 * 4; //Now in ticks
- nMotorEncoderTarget[leftMotor] = LeftMotorTarget = nMotorEncoder[leftMotor] - EncoderValue;
- nMotorEncoderTarget[rightMotor] = RightMotorTarget = nMotorEncoder[rightMotor] - EncoderValue;
- // doesn't account for overflow of Encoder value
- // wait for 1 motor to reach Target and then wait for the other
- LeftMotorDone = nMotorEncoder[leftMotor] < LeftMotorTarget;
- RightMotorDone = nMotorEncoder[rightMotor] < RightMotorTarget;
- while(!LeftMotorDone && !RightMotorDone) {
- LeftMotorDone = nMotorEncoder[leftMotor] < LeftMotorTarget;
- RightMotorDone = nMotorEncoder[rightMotor] < RightMotorTarget;
- }
- // can also return after first motor reaches Target
- if (LeftMotorDone) { // Left done, wait for Right
- RightMotorDone = nMotorEncoder[rightMotor] < RightMotorTarget;
- while (!RightMotorDone) {
- RightMotorDone = nMotorEncoder[rightMotor] < RightMotorTarget;
- }
- } else { // Right done, wait for Left
- LeftMotorDone = nMotorEncoder[leftMotor] < LeftMotorTarget;
- while (!LeftMotorDone) {
- LeftMotorDone = nMotorEncoder[leftMotor] < LeftMotorTarget;
- }
- }
- }
- void Forward(int inches, int power)
- {
- float scale=1.0; // scale master power level for slave
- resetMotor();
- // ramp up
- motor[leftMotor] = (int) power/2 * scale;
- motor[rightMotor] = power/2;
- motor[leftMotor] = (int) power * scale;
- motor[rightMotor] = power;
- wait4Inches(inches);
- // ramp down
- motor[leftMotor] = (int) power/2 * scale;
- motor[rightMotor] = power/2;
- motor[leftMotor] = 0;
- motor[rightMotor] = 0;
- }
- void Backward(int inches, int power)
- {
- float scale=1.0; // scale master power level for slave
- resetMotor();
- // ramp up
- motor[leftMotor] = -1 * (int) power/2 * scale;
- motor[rightMotor] = -1 * power/2;
- motor[leftMotor] = -1 * (int) power * scale;
- motor[rightMotor] = -1 * power;
- wait4BackInches(inches);
- // ramp down
- motor[leftMotor] = -1 * (int) power/2 * scale;
- motor[rightMotor] = -1 * power/2;
- motor[leftMotor] = 0;
- motor[rightMotor] = 0;
- }
- void turnLeft(int angle, int power)
- {
- int LeftMotorTarget, RightMotorTarget;
- int EncoderValue;
- bool LeftMotorDone, RightMotorDone;
- resetMotor();
- // 2*pi*r=C, robot wheel to wheel basis 15.5", small wheel 2"
- // ratio = 15.5/2 = 7.75 turns of wheel to equal 1 turn of robot.
- // 4 ticks per degree
- EncoderValue = 4 * angle * 7.75; // New Tick Value Difference
- if(angle > 0) {
- motor[rightMotor] = power;
- nMotorEncoderTarget[rightMotor] = RightMotorTarget = nMotorEncoder[rightMotor] + EncoderValue;
- RightMotorDone = nMotorEncoder[rightMotor] > RightMotorTarget;
- while (!RightMotorDone) {
- RightMotorDone = nMotorEncoder[rightMotor] > RightMotorTarget;
- }
- motor[rightMotor] = 0;
- } else {
- motor[leftMotor] = - power;
- nMotorEncoderTarget[leftMotor] = LeftMotorTarget = nMotorEncoder[leftMotor] + EncoderValue;
- LeftMotorDone = nMotorEncoder[leftMotor] < LeftMotorTarget;
- while (!LeftMotorDone) {
- LeftMotorDone = nMotorEncoder[leftMotor] < LeftMotorTarget;
- }
- motor[leftMotor] = 0;
- }
- }
- void turnRight(int angle, int power)
- {
- int LeftMotorTarget, RightMotorTarget;
- int EncoderValue;
- bool LeftMotorDone, RightMotorDone;
- resetMotor();
- // 2*pi*r=C, robot wheel to wheel basis 15.5", small wheel 2"
- // ratio = 15.5/2 = 7.75 turns of wheel to equal 1 turn of robot.
- // 4 ticks per degree
- EncoderValue = 4 * angle * 7.75; // New Tick Value Difference
- if(angle > 0) {
- motor[leftMotor] = power;
- nMotorEncoderTarget[leftMotor] = LeftMotorTarget = nMotorEncoder[leftMotor] + EncoderValue;
- LeftMotorDone = nMotorEncoder[leftMotor] > LeftMotorTarget;
- while (!LeftMotorDone) {
- LeftMotorDone = nMotorEncoder[leftMotor] > LeftMotorTarget;
- }
- motor[leftMotor] = 0;
- } else {
- motor[rightMotor] = - power;
- nMotorEncoderTarget[rightMotor] = RightMotorTarget = nMotorEncoder[rightMotor] + EncoderValue;
- RightMotorDone = nMotorEncoder[rightMotor] < RightMotorTarget;
- while (!RightMotorDone) {
- RightMotorDone = nMotorEncoder[rightMotor] < RightMotorTarget;
- }
- motor[rightMotor] = 0;
- }
- }
- void spinLeft(int angle, int power)
- {
- int LeftMotorTarget, RightMotorTarget;
- int EncoderValue;
- bool LeftMotorDone, RightMotorDone;
- // only allow positive numbers
- if (angle <= 0 || power <= 0) return;
- resetMotor();
- // 2*pi*r=C, robot wheel to wheel basis 15.5", small wheel 2"
- // both wheels spin in opposite direction so radius is half or 7.75
- // ratio = 7.75/2 = 3.88 turns of wheel to equal 1 spin of robot.
- // 4 ticks per degree
- EncoderValue = 4 * angle * 3.88; // New Tick Value Difference
- nMotorEncoderTarget[leftMotor] = LeftMotorTarget = nMotorEncoder[leftMotor] - EncoderValue;
- nMotorEncoderTarget[rightMotor] = RightMotorTarget = nMotorEncoder[rightMotor] + EncoderValue;
- motor[leftMotor] = - power;
- motor[rightMotor] = power;
- // wait for 1 motor to reach Target and then return
- LeftMotorDone = nMotorEncoder[leftMotor] < LeftMotorTarget;
- RightMotorDone = nMotorEncoder[rightMotor] > RightMotorTarget;
- while(!LeftMotorDone && !RightMotorDone) {
- LeftMotorDone = nMotorEncoder[leftMotor] < LeftMotorTarget;
- RightMotorDone = nMotorEncoder[rightMotor] > RightMotorTarget;
- }
- motor[leftMotor] = 0;
- motor[rightMotor] = 0;
- }
- void spinRight(int angle, int power)
- {
- int LeftMotorTarget, RightMotorTarget;
- int EncoderValue;
- bool LeftMotorDone, RightMotorDone;
- // only allow positive numbers
- if (angle <= 0 || power <= 0) return;
- resetMotor();
- // 2*pi*r=C, robot wheel to wheel basis 15.5", small wheel 2"
- // both wheels spin in opposite direction so radius is half or 7.75
- // ratio = 7.75/2 = 3.88 turns of wheel to equal 1 spin of robot.
- // 4 ticks per degree
- EncoderValue = 4 * angle * 3.88; // New Tick Value Difference
- nMotorEncoderTarget[leftMotor] = LeftMotorTarget = nMotorEncoder[leftMotor] + EncoderValue;
- nMotorEncoderTarget[rightMotor] = RightMotorTarget = nMotorEncoder[rightMotor] - EncoderValue;
- motor[leftMotor] = power;
- motor[rightMotor] = - power;
- // wait for 1 motor to reach Target and then return
- LeftMotorDone = nMotorEncoder[leftMotor] > LeftMotorTarget;
- RightMotorDone = nMotorEncoder[rightMotor] < RightMotorTarget;
- while(!LeftMotorDone && !RightMotorDone) {
- LeftMotorDone = nMotorEncoder[leftMotor] > LeftMotorTarget;
- RightMotorDone = nMotorEncoder[rightMotor] < RightMotorTarget;
- }
- motor[leftMotor] = 0;
- motor[rightMotor] = 0;
- }
- //---------------------------------------------------------------------------------------------------------------------------------
- task main()
- {
- int runseq;
- //runseq = chooser();
- initializeRobot();
- /*
- motor[leftMotor] = 60;
- motor[rightMotor] = 20;
- wait4Inches(12);
- motor[leftMotor] = 0;
- motor[rightMotor] = 0;
- */
- nxtDisplayCenteredBigTextLine(3, "Test");
- return;
- //waitForStart();
- //Autonomous Code
- if(runseq == 1) { //Program 1: Knocking the Pole Down & Starting at edge of ramp Pos. 1
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- int maxSig = 0;
- int x = 0;
- eraseDisplay();
- nxtDisplayCenteredBigTextLine(3, "Running");
- wait1Msec(1000);
- eraseDisplay();
- _dirDC = HTIRS2readDCDir(HTIRS2);
- if (_dirDC < 0) x = 1;
- _dirAC = HTIRS2readACDir(HTIRS2);
- if (_dirAC < 0) x = 2;
- if (!HTIRS2readAllDCStrength(HTIRS2, dcS1, dcS2, dcS3, dcS4, dcS5)) x = 3;
- if (!HTIRS2readEnhanced(HTIRS2, _dirEnh, _strEnh)) x = 4;
- if (!HTIRS2readAllACStrength(HTIRS2, acS1, acS2, acS3, acS4, acS5 )) {
- x = 5;
- } else {
- x = 0;
- }
- maxSig = (acS1 > acS2) ? acS1 : acS2;
- maxSig = (maxSig > acS3) ? maxSig : acS3;
- maxSig = (maxSig > acS4) ? maxSig : acS4;
- maxSig = (maxSig > acS5) ? maxSig : acS5;
- nxtDisplayCenteredBigTextLine(1, "Dir=%d", _dirAC);
- nxtDisplayCenteredBigTextLine(4, "Sig=%d", maxSig);
- }
- /*
- if(runseq == 2) { //Program 2: Knocking the Pole Down & Starting on edge of ramp Pos. 2
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- }
- if(runseq == 3) { //Program 3: Knocking the Pole Down & Starting at edge of ramp Pos. 3
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- }
- //---------------------------------------------------------------------------------------------------------------------------------
- if(runseq == 4) { //Program 4: Knocking the Pole Down & Starting in base Pos. 1
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- }
- if(runseq == 5) { //Program 5: Knocking the Pole Down & Starting in base Pos. 2
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- }
- if(runseq == 6) { //Program 6: Knocking the Pole Down & Starting in base Pos. 3
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- }
- //---------------------------------------------------------------------------------------------------------------------------------
- if(runseq == 7) { //Program 7: Going to Tubes & Starting at base Pos. 1
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- }
- if(runseq == 8) { //Program 8: Going to Tubes & Starting at base Pos. 2
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- }
- if(runseq == 9) { //Program 9: Going to Tubes & Starting at base Pos. 3
- int _dirDC = 0;
- int _dirAC = 0;
- int dcS1, dcS2, dcS3, dcS4, dcS5 = 0;
- int acS1, acS2, acS3, acS4, acS5 = 0;
- int _dirEnh, _strEnh;
- }
- //---------------------------------------------------------------------------------------------------------------------------------
- */
- if(runseq == 10) { //Program 10: Going towards Tubes & Starting at edge of ramp any Pos?
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement