This is something i'm working on

Go down

This is something i'm working on

Post  hun on Thu Jan 14, 2010 9:01 pm

Dont mind this post everyone, this is just something for me i'm working on and dont want to lose this info. Promise it's gonna be tite once it's up and running. Thanks!

Going to re-write this to accept more targets. Looking to use RJ45 as the medium between units.

#include

// The Arduino is ready to receive a new command
boolean bReadyForCommand = true;

// PWM command margin - this is to handle the variablility of the PWM reading
int pwmInputVariabilityMargin = 10;

// Servos
Servo leftServo; // Left Target Servo
Servo centerServo; // Center Target Servo
Servo rightServo; // Right Targer Servo

// Command Values for PWM
int commandProgram1Value = 540; // Test program
int commandProgram2Value = 640; // Succession Program (left for 2
sec, center for 2 sec, right for 2 sec)
int commandProgram3Value = 740; // Random for 2 seconds
int commandProgram4Value = 840; // Random 10 second delay between targets

int HIDE_TARGET_SERVO_POS = 0;
int SHOW_TARGET_SERVO_POS = 90;

/*
* PIN CONFIGURATION
*/
// This is the output pin communicating to the ioBridge that the
command has been processed
int debugioBridgeInputReadyPin = 13;
int ioBridgeInputReadyPin = 12;

// PWM Servo Outputs - These are the pinouts
int targetLeftPWMOutput= 11;
int targetCenterPWMOutput= 10;
int targetRightPWMOutput= 9;

// This is the analog input from which the Arduino will receive
commands from the IO Bridge
int ioBridgeCommandInputPin = 8;

// This is the input pin informing the arduino that there is a command
ready to be processed
int ioBridgeCommandReady = 7;

// Peizo Speaker Pin
int peizoOutputPin = 2;

/*
* initializePinouts()
* Initialize the Arduino pins that will be used for communication with
the IO Bridge
*
* @param none
* @return none
*/
void initializePinouts()
{
// Inputs
pinMode(ioBridgeCommandInputPin, INPUT);
pinMode(ioBridgeCommandReady, INPUT);

// Outputs
pinMode(ioBridgeInputReadyPin, OUTPUT);
pinMode(debugioBridgeInputReadyPin, OUTPUT);
pinMode(targetLeftPWMOutput, OUTPUT);
pinMode(targetCenterPWMOutput, OUTPUT);
pinMode(targetRightPWMOutput, OUTPUT);
pinMode(peizoOutputPin, OUTPUT);
}

/*
* initializeCommands()
* Initialize the commands that the Arduino will support coming in from
the IO Bridge
*
* @param none
* @return none
*/
void initializeCommands()
{

}

/*
* initializeServoPositions()
* Start the servos in a known state
*
* @param None
* @return None
*/
void initializeServoPositions()
{
// Set servos to hide state
leftServo.write(HIDE_TARGET_SERVO_POS);
centerServo.write(HIDE_TARGET_SERVO_POS);
rightServo.write(HIDE_TARGET_SERVO_POS);

pulseDelay(1000);
}

/*
* pulseDelay()
* This method will effectively be a delay method, but should be used
instead of the delay because the
* software servo library requires a refesh every 50 ms. This should
be in increments of 25ms
* @param duration - The time in ms to wait for
*/
void pulseDelay(int duration)
{
// Calculate how many iterations we need
int iterations = duration / 25;

// Call refesh every 25 ms (2x what is needed just in case)
for (int x=0; x {
delay(25);
Servo::refresh();
}
}

/* soundSpeaker(int program)
* This method will sound the peizo speaker for second to indicate to
the shooter to get ready
*
* @param program - this is the program number to be executed
* @return None
*/
void soundSpeaker(int program)
{
for (int x=1; x < program; x++)
{
digitalWrite(peizoOutputPin, HIGH);
delay(300);
digitalWrite(peizoOutputPin, LOW);
delay(300);
}

digitalWrite(peizoOutputPin, HIGH);
delay(1000);
digitalWrite(peizoOutputPin, LOW);
}

/*
* commandProgram1
* Diagnostics
* @param none
* @return none
*/
void commandProgram1()
{
Serial.println("Processing: Diagnostics program");

soundSpeaker(1);

// Show all targets
leftServo.write(SHOW_TARGET_SERVO_POS);
centerServo.write(SHOW_TARGET_SERVO_POS);
rightServo.write(SHOW_TARGET_SERVO_POS);
Servo::refresh();

// Delay while pulsing every 25ms
pulseDelay(2000);

// Return to default state
initializeServoPositions();

}

/*
* commandProgram2()
* Left to right target faces
* @param none
* @return none
*/
void commandProgram2()
{
Serial.println("Processing: Left to right");

soundSpeaker(2);

// Show the left target
leftServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(2000);

// Show the center target
leftServo.write(HIDE_TARGET_SERVO_POS);
centerServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(2000);

// Show the right target
centerServo.write(HIDE_TARGET_SERVO_POS);
rightServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(2000);

rightServo.write(HIDE_TARGET_SERVO_POS);

// Return to default state
initializeServoPositions();

}

/*
* commandProgram3()
* Random 5 targets
* @param none
* @return none
*/
void commandProgram3()
{
soundSpeaker(3);

for (int x=0; x<5; x++)
{
int target = random(1, 4); // pick a target between 1 and 3

if (target == 1)
{
leftServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(2000);
leftServo.write(HIDE_TARGET_SERVO_POS);
pulseDelay(500); // Allow to return
}
else if (target == 2)
{
centerServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(2000);
centerServo.write(HIDE_TARGET_SERVO_POS);
pulseDelay(500); // Allow to return
}
else if (target == 3)
{
rightServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(2000);
rightServo.write(HIDE_TARGET_SERVO_POS);
pulseDelay(500); // Allow to return
}
}

// Return to default state
initializeServoPositions();
}

/*
* commandProgram4()
* Random delay (20 seconds), then a quick 10 targets
* @param none
* @return none
*/
void commandProgram4()
{
soundSpeaker(4);

// Get a delay between 2 and 20 seconds
int delayInSeconds = random (2, 20);

// Delay for the time needed
pulseDelay(delayInSeconds * 1000);

for (int x=0; x<10; x++)
{
int target = random(1, 4); // pick a target between 1 and 3

if (target == 1)
{
leftServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(1000);
leftServo.write(HIDE_TARGET_SERVO_POS);
pulseDelay(500); // Allow to return
}
else if (target == 2)
{
centerServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(1000);
centerServo.write(HIDE_TARGET_SERVO_POS);
pulseDelay(500); // Allow to return
}
else if (target == 3)
{
rightServo.write(SHOW_TARGET_SERVO_POS);
pulseDelay(1000);
rightServo.write(HIDE_TARGET_SERVO_POS);
pulseDelay(500); // Allow to return
}
}

// Return to default state
initializeServoPositions();
}


/*
* decodeCommand()
* Lookup the value of the command that is recieved on the input
line(s) and determine if valid.
*
* @param none
* @return TRUE if successful, FALSE otherwise
*/
bool decodeCommand(int commandValue)
{
// @TODO use function pointers by lookup rather than if statements
//cmdPointer[0] = testCommand;

// Check for command value 1
if ((commandValue >= (commandProgram1Value - pwmInputVariabilityMargin)) &&
(commandValue <= (commandProgram1Value + pwmInputVariabilityMargin)))
{
commandProgram1();
}
// Check for command value 2
else if ((commandValue >= (commandProgram2Value -
pwmInputVariabilityMargin)) &&
(commandValue <= (commandProgram2Value + pwmInputVariabilityMargin)))
{
commandProgram2();
}
// Check for command value 3
else if ((commandValue >= (commandProgram3Value -
pwmInputVariabilityMargin)) &&
(commandValue <= (commandProgram3Value + pwmInputVariabilityMargin)))
{
commandProgram3();
}
// Check for command value 4
else if ((commandValue >= (commandProgram4Value -
pwmInputVariabilityMargin)) &&
(commandValue <= (commandProgram4Value + pwmInputVariabilityMargin)))
{
commandProgram4();
}
else
{
// @DEBUG
Serial.print("Unknown command: ");
Serial.println(commandValue);
}
}


/*
* setup()
* Setup function for the Arduino board (mandatory function)
*
* @param none
* @return none
*/
void setup()
{
// @DEBUG
Serial.begin(9600);

// Seed the random number generator
randomSeed(analogRead(1));

// initialize necessary pins
initializePinouts();

// set initial state to "ready for command"
updateArduinoReadyState(true);

// Set the positions for the servos (550 = 0 degrees, 1450 = center,
2300 = 180)
leftServo.attach(targetLeftPWMOutput);
http://leftServo.setMaximumPulse(2300);
http://leftServo.setMinimumPulse(550);

centerServo.attach(targetCenterPWMOutput);
http://centerServo.setMaximumPulse(2300);
http://centerServo.setMinimumPulse(550);

rightServo.attach(targetRightPWMOutput);
http://rightServo.setMaximumPulse(2300);
http://rightServo.setMinimumPulse(550);

// put the Servos in a "not shown" state
initializeServoPositions();
} // end setup()

/*
* updateArduinoReadyState()
* Update the value of the Input Ready pin
*
* @param commandReady - True to set pin high, False to set pin low
* @return none
*/
void updateArduinoReadyState(boolean inputReady)
{
// Update the arduino state
bReadyForCommand = inputReady;

if (bReadyForCommand == true)
{
// @DEBUG
Serial.println("Arduino: Ready for command...");
digitalWrite(debugioBridgeInputReadyPin, HIGH);

digitalWrite(ioBridgeInputReadyPin, HIGH);

}
else
{
// @DEBUG
Serial.println("Arduino: Processing command...");
digitalWrite(debugioBridgeInputReadyPin, LOW);

digitalWrite(ioBridgeInputReadyPin, LOW);
}
} // end updateInputReadyPin()



/*
* loop()
* Main execution loop of the Arduino processor (mandatory function)
*
* @param none
* @return none
*/
void loop()
{
int commandValue = 0;
int commandWaiting;
boolean bCommandWaiting = false;

Servo::refresh();

// We are ready to process a new command
if (bReadyForCommand == true)
{
// Read the command pin from the ioBridge to see if it wants
to send a command
if (digitalRead(ioBridgeCommandReady) == HIGH)
{
// @DEBUG
Serial.println("Command waiting...");
bCommandWaiting = true;
} // end if
else
{
bCommandWaiting = false;
} // end else

// There was a command waiting for processing
if (bCommandWaiting == true)
{
// The arduino is processing a command, we will not process another
updateArduinoReadyState(false);

// Read the command from the input lines from the IO Bridge
// @DEBUG
Serial.println("Reading PWM value from ioBridge...");
commandValue = pulseIn(ioBridgeCommandInputPin, HIGH);

// @DEBUG
Serial.print("Read Value from ioBridge: ");
Serial.println(commandValue);

// Lookup and process the command from the ioBridge
decodeCommand(commandValue);
} // end if
} // end if
else
{
// We will not declare that the arduino is ready for another
command until the ioBridge pulls its command bit low
if (digitalRead(ioBridgeCommandReady) == LOW)
{
// Ok, we are ready to receive a new command
updateArduinoReadyState(true);
}
}
} // end loop()
avatar
hun
Admin

Posts : 372
Join date : 2008-05-06
Age : 36
Location : See - arson

View user profile http://pasados.realbb.net

Back to top Go down

Re: This is something i'm working on

Post  nanya on Fri Jan 15, 2010 11:14 am

Nice program ................................................................................................................................................................................................................
.........................................................................................................................................................................................................................................
..........
..............................................................................................................................................................................................................................................
..............................................................................................................................................................................................................................................
............................................................................................................................................................................................................................................
...........................................................................................................................................................................................................................................
...........................................................................................................................................................................................................................................
.....
.........................................................................................................................................................................................................................................
.........................................................................................................................................................................................................................................
..........................................................................................................................................................................................................................................
..........................................................................................................................................................................................................................................
....
..........................................................................................................................................................................................................................................
.........................................................................................................................................................................................................................................
........................................................................................................................................................................................................................................
..........................................................................................................................................................................................................................................
...............................................................................................................................................................................................................................................

........................................................................................................................................................................................................................................
................................................................................................NERD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
avatar
nanya
Moderator

Posts : 133
Join date : 2008-05-06

View user profile

Back to top Go down

Re: This is something i'm working on

Post  hun on Fri Jan 15, 2010 8:17 pm

avatar
hun
Admin

Posts : 372
Join date : 2008-05-06
Age : 36
Location : See - arson

View user profile http://pasados.realbb.net

Back to top Go down

Re: This is something i'm working on

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum