Recent Changes - Search:

ENTER NEW DAILY DATA HERE:

PmWiki

pmwiki.org

edit SideBar

December30-LatestMaster-SlaveCodeInProgress

ElmoMasterTESTarray

(code-in-progress and documentation)

//ElmoMasterTESTarray
//Master Code for composing
//and sending commands
//December 30th, 2008


#define SPD 0
#define ARM 1
#define LEG 2

#define FAST  'A'
#define MED   'B'
#define SLOW  'C'

#define armBr 'B' //-20 deg
#define armRd 'R' //  0 deg
#define armYw 'Y' // 90 deg
#define armGn 'G' //150 deg

#define legBl 'B' //?
#define legGr 'G' //?
#define legYw 'Y' //?
#define legOr 'O' //?
#define legRd 'R' //?

#define HOLD  'H' //No movement (works for arm and legs)


//Variable definitions

int switchPin = 2;
int previousState = 1;
int nowState = 0;
int stateChange = 0;
int ledPin = 13;  //to double check that code is good
int Step = 1;
int seqNum = 1;
char movMsg[4];


void setup() {

  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH); //enable internal pull-up resistor
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);

}


void loop(){


  state();

  if (stateChange == 1 && nowState == 1) {

    //Serial.println('H');
    digitalWrite(ledPin,HIGH);
    previousState=nowState;
    state();

  }


// ** This is where the move choreography begins. ** //
// ** Each case switched through is a step in the ** //
// ** performance.  Change the speed value first  ** //
// ** (FAST, MED or SLOW), then the position for  ** //
// ** the arm (please see '#define's above), then ** //
// ** the position for the legs (also in the      ** //
// ** '#define's above).  The angular positions   ** //
// ** corresponding to each joint position are    ** //
// ** detailed in the comments above.             ** //


  else if (stateChange == 1 && nowState == 0) {       

    switch(Step) {
      case 1:
        multiPrint(makeMsg(FAST, armBr, legBl));
        break;
      case 2:
        multiPrint(makeMsg(FAST, armBr, legBl));
        break;
      case 3:
        multiPrint(makeMsg(FAST, armBr, legBl));
        break;
      case 4:
        multiPrint(makeMsg(FAST, armBr, legBl));
        break;
      case 5:
        multiPrint(makeMsg(FAST, armBr, legBl));
        break;
      case 6:
        multiPrint(makeMsg(FAST, armBr, legBl));
        break;
      case 7:
        multiPrint(makeMsg(FAST, armBr, legBl));
        break;
      default:
        multiPrint(makeMsg(FAST, armBr, legBl));
        break;
    }

    //push the action number forward     
    Step = Step + 1;

    digitalWrite(ledPin,LOW);
    previousState=nowState;
    state();

  }

}


void state() {

  nowState=digitalRead(switchPin);

  if (nowState != previousState){
    stateChange=1;
  }

  else {
    stateChange=0;
  }

}


void multiPrint(char moveMessage[]) {

  //ATTEMPT #1
  Serial.print(seqNum); 
  Serial.print(' ');
  Serial.println(moveMessage);
  delay(10);

  //ATTEMPT #2
  Serial.print(seqNum); 
  Serial.print(' ');
  Serial.println(moveMessage);
  delay(10);

  //ATTEMPT #3
  Serial.print(seqNum); 
  Serial.print(' ');
  Serial.println(moveMessage);
  delay(10);

  seqNum = seqNum + 1;

}


char* makeMsg(char spdPos, char armPos, char legPos) {

  movMsg[SPD] = spdPos;
  movMsg[ARM] = armPos;
  movMsg[LEG] = legPos;

  return movMsg;
}

ElmoSlaveTESTarray

(code-in-progress and documentation)

//ElmoSlaveTESTarray
//Slave Code for interpreting
//and executing Master commands
//December 30th, 2008


#define FAST 180
#define MED  120
#define SLOW 60


//Other variables

int seqNumLast = 0;
int seqNumCurrent;
char msgBuf[64];


//From the Master

char spdIn;
char armIn;
char legIn;


//To our functions

int spdOut;
int armOut;
int legOut;


//Motor Output Pins:

int armPwmPin = 10;      // L293 pin 1
int armMtrPosPin = 2;    // L293 pin 2
int armMtrNegPin = 3;    // L293 pin 7
int legPwmPin = 11;      // L293 pin 9
int legMtrPosPin = 4;    // L293 pin 15
int legMtrNegPin = 5;    // L293 pin 10


//Connections to drive motors:

// L293 pin 3 - Arm Motor + (green wire)
// L293 pin 6 - Arm Motor - (white wire)
// L293 pin 14 - Leg Motor + (yellow wire)
// L293 pin 11 - Leg Motor - (brown wire)


//Motor Feedback Pins:

int armSensorBrown = 6;    //red wire -20 180+
int armSensorRed = 7;      //white wire 0 90~180
int armSensorYellow = 8;   //yellow wire 90 90
int armSensorGreen = 9;    //grey wire 150 0
int legSensorBlue = 14;    //blue wire 135
int legSensorGreen = 15;   //green wire 90
int legSensorYellow = 16;  //yellow wire 30
int legSensorOrange = 17;  //orange wire 0
int legSensorRed = 18;     //red wire -30


//Gyroscope Feedback Pins:

int gyroGreen = 13;    // supine
int gyroYellow = 19;   // upright
int gyroRed = 12;      // prone
//brown is ground, and orange is not connected


//Variables:

int forward=1;
int reverse=0;
//int stepNumber=0; //keep track of which movement to do next
//int currentStep;


void setup() {


  Serial.begin(9600);


  //Motor Output Pins:

  pinMode(armPwmPin, OUTPUT);        //Set pin 3 as an output
  pinMode(armMtrPosPin, OUTPUT);     //Set pin 2 as an output
  pinMode(armMtrNegPin, OUTPUT);     //Set pin 4 as an output
  digitalWrite(armPwmPin, LOW);      //Disable motor on startup
  digitalWrite(armMtrPosPin, HIGH);  //Prepare for clockwise motor rotation
  digitalWrite(armMtrNegPin, LOW);   //Prepare for clockwise motor rotation

  pinMode(legPwmPin, OUTPUT);        //Set pin 3 as an output
  pinMode(legMtrPosPin, OUTPUT);     //Set pin 2 as an output
  pinMode(legMtrNegPin, OUTPUT);     //Set pin 4 as an output
  digitalWrite(legPwmPin, LOW);      //Disable motor on startup
  digitalWrite(legMtrPosPin, HIGH);  //Prepare for clockwise motor rotation
  digitalWrite(legMtrNegPin, LOW);   //Prepare for clockwise motor rotation


  //Motor Feedback Pins: for arm

  pinMode(armSensorRed, INPUT);      //Set pin 7 as an input
  digitalWrite(armSensorRed, HIGH);  //Enable internal pull-up resistor

  pinMode(armSensorBrown, INPUT);      //Set pin 8 as an input
  digitalWrite(armSensorBrown, HIGH);  //Enable internal pull-up resistor

  pinMode(armSensorYellow, INPUT);      //Set pin 9 as an input
  digitalWrite(armSensorYellow, HIGH);  //Enable internal pull-up resistor

  pinMode(armSensorGreen, INPUT);      //Set pin 10 as an input
  digitalWrite(armSensorGreen, HIGH);  //Enable internal pull-up resistor


  //Motor Feedback Pins: for legs

  pinMode(legSensorBlue, INPUT);      //Set pin 7 as an input
  digitalWrite(legSensorBlue, HIGH);  //Enable internal pull-up resistor

  pinMode(legSensorGreen, INPUT);      //Set pin 8 as an input
  digitalWrite(legSensorGreen, HIGH);  //Enable internal pull-up resistor

  pinMode(legSensorYellow, INPUT);      //Set pin 9 as an input
  digitalWrite(legSensorYellow, HIGH);  //Enable internal pull-up resistor

  pinMode(legSensorOrange, INPUT);      //Set pin 10 as an input
  digitalWrite(legSensorOrange, HIGH);  //Enable internal pull-up resistor

  pinMode(legSensorRed, INPUT);      //Set pin 10 as an input
  digitalWrite(legSensorRed, HIGH);  //Enable internal pull-up resistor

//  Serial.print("Step: ");
//  Serial.println(stepNumber);

}


void loop() {

  //Wait for a command+speed message to be sent.
  //Check the sequence number of the message to weed out
  //repeated messages.
  //Execute desired move(s) at desired speed.


// ** From here, we have to take in the array, ** //
// ** parse out the speed, arm and leg moves   ** //
// ** and send that data into functions which  ** //
// ** move the arm and legs in the right       ** //
// ** directions based on where they are       ** //
// ** located when they receive those          ** //
// ** commands.  The code below for reading in ** //
// ** in the array of values is currently not  ** //
// ** working.                                 ** //


  if(Serial.available() > 0) {

     seqNumCurrent = Serial.read();
     //Serial.read(); //Get the 'space'

    if (seqNumCurrent != seqNumLast) {

      spdIn = Serial.read(); //speed
      armIn = Serial.read(); //arm move
      legIn = Serial.read(); //leg move

      //Prints for testing ***
      Serial.println("TEST");
      Serial.println(seqNumCurrent, DEC);
      Serial.println(spdIn, BYTE);
      Serial.println(armIn, BYTE);
      Serial.println(legIn, BYTE);

      Serial.flush(); //Flush any crap -- NECESSARY?

/*      
    //Switch cases for each appendage

      switch(spdIn) {
        case 'A':
          spdOut = FAST;
          break;
        case 'B':
          spdOut = MED;
          break;
        case 'C':
          spdOut = SLOW;
          break;
        case 'H':
          break;
        default:
          Serial.print("UNKNOWN VALUE!");
          break;
      }

      switch(armIn) {
        case 'B':

        case 'R':

        case 'Y':

        case 'G':

        case 'H':
          break;
        default:
          break;
      }

      switch(legIn) {
        case 'B':

        case 'G':

        case 'Y':

        case 'O':

        case 'R':

        case 'H':
          break;
        default:
          break;
      }
 */     

      seqNumLast = seqNumCurrent;
    }
  }
}



void blink(int whatPin, int howManyTimes, int milliSecs) {

  int i=0;

  for(i=0;i<howManyTimes;i++) {
    digitalWrite(whatPin, HIGH);
    delay(milliSecs/2);
    digitalWrite(whatPin, LOW);
    delay(milliSecs/2);
  }
}


void moveArm(int armSensorColor, int whichWay, int howFast) {

  //MOVE MOTOR IN DIRECTION OF "whichWay" (EITHER FORWARD OR REVERSE)
  int positivePin = whichWay;
  int negativePin = ~whichWay & 1;
  digitalWrite(armMtrPosPin, positivePin); 
  digitalWrite(armMtrNegPin, negativePin);
  analogWrite(armPwmPin,howFast); // Turn motor on at speed "howFast"

  //WATCH FOR WHEN THE MOTOR REACHES THE NEXT TARGET SENSOR
  int sensor = 1;

  while(sensor==HIGH) {
    sensor = digitalRead(armSensorColor);
  }

  //AND WHEN ELMO'S ARM REACHES TARGET SENSOR, STOP:
  analogWrite(armPwmPin,0);

}


void moveLeg(int legSensorColor, int whichWay, int howFast) {

  //MOVE MOTOR IN DIRECTION OF "whichWay" (EITHER FORWARD OR REVERSE)
  int positivePin = whichWay;
  int negativePin = ~whichWay & 1;
  digitalWrite(legMtrPosPin, positivePin); 
  digitalWrite(legMtrNegPin, negativePin);
  analogWrite(legPwmPin,howFast); // Turn motor on at speed "howFast"

  //WATCH FOR WHEN THE MOTOR REACHES THE NEXT TARGET SENSOR
  int sensor = 1;
  while(sensor==1) {
    sensor = digitalRead(legSensorColor);
  }

  //AND WHEN ELMO'S ARM REACHES TARGET SENSOR, STOP:
  analogWrite(legPwmPin,0);
}


/*   
 *   USE THESE FUNCTION CALLS AS EXAMPLES
 *   FOR WRITING THE MOVE COORDINATION ABOVE
 *   moveArm(armSensorRed, reverse, 150);
 *   moveArm(armSensorGreen, forward, 150);
 *   moveArm(armSensorRed, reverse, 150);
 *   moveArm(armSensorGreen, forward, 150);
 *   moveArm(armSensorYellow, reverse, 150);      
 *   moveLeg(legSensorGreen,forward, 200);
 *   moveLeg(legSensorOrange, reverse, 200);
 */
Edit - History - Print - Recent Changes - Search
Page last modified on December 30, 2008, at 08:38 PM