Ethan
Created March 30, 2017

Senior Design: VR Flight Simulator

Creating the ultimate VR flying experience using an arduino, a raspberry pi, two actuators, and an Oculus Rift

IntermediateShowcase (no instructions)115
Senior Design: VR Flight Simulator

Things used in this project

Story

Read more

Code

Simtool_Mega_Machine.ino

C/C++
const int pin_actuator_pitch_front = 4;
const int pin_actuator_pitch_back = 5;
const int pin_actuator_roll_right = 3;
const int pin_actuator_roll_left = 2;

const int MaxCharCount = 24;                //how many datapoints in the array 
const int valueCharCount = 12;              //each axis has 3 numbers + the axis char --> 4*3 = 12
int count = 0;
int Rcount = 0;
int Pcount = 0;
int Ycount = 0;
char tmp;
char tmpArray[MaxCharCount];
char tmpArrayR[5];          //character //what are the brackets for?
char tmpArrayP[5];
char tmpArrayY[5];
int valueR;
int valueP;
int valueY;
int check1 = 0;
int check2 = 0;
int check3 = 0;
int checkA = 0;
int checkB = 0;
int checkC = 0;

const int pin_sensor_roll = A9;     //*
const int pin_sensor_pitch = A8;    //*
const int pin_control_gain = A15;  //Blue potentiomter *

// Variables
float control_gain = 1.0;                    //*
float roll;                                  //*
float pitch;                                 //*
float roll_desired;                         //*
float pitch_desired;                        //*
float roll_control;                         //*
float pitch_control;                        //*

void setup() {
  Serial.begin(115200);
  pinMode(pin_actuator_roll_right, OUTPUT); //set pin 3 as output
  pinMode(pin_actuator_roll_left, OUTPUT);
  pinMode(pin_actuator_pitch_front, OUTPUT);
  pinMode(pin_actuator_pitch_back, OUTPUT);
}

void loop() {
  for (int i = 0; i < MaxCharCount && Serial.available(); i++) {
    tmpArray[count] = Serial.read();
      if (isAscii(tmpArray[count])) {               
        if (tmpArray[count] == 'R' && check1 == 0) {
          Rcount = count;
          check1++;
        }
        if (tmpArray[count] == 'P' && check2 == 0) {
          Pcount = count;
          check2++;
        }
        if (tmpArray[count] == 'Y' && check3 == 0) {
          Ycount = count;
          check3++;
        }
        count++;
      }     
  }
  
    
  //populate R array
  for (int a = 0; a < 3 && tmpArray[Rcount + a] != 'P'; a++) {
    tmpArrayR[a] = tmpArray[Rcount + a + 1];
  }

  //populate P array
  for (int b = 0; b < 3 && tmpArray[Pcount + b] != 'Y'; b++) {
    tmpArrayP[b] = tmpArray[Pcount + b + 1];
  }

  //populate Y array
  for (int c = 0; c < 3 && tmpArray[Ycount + c] != '~'; c++) {
    tmpArrayY[c] = tmpArray[Ycount + c + 1];
  }
                                          //isAscii what is this??
  valueR = atoi(tmpArrayR);               
  valueP = atoi(tmpArrayP);               //data conversion:  atof()=converts an ascii character array to a float
  valueY = atoi(tmpArrayY);               //atoi()=converts an ascii character array to an integer
                                          //itoa()=converts an integer to a character array


  valueP = map(valueP, 0, 255, -80,80 );       //*                            -80~80
  valueR = map(valueR, 0,255, -100,100);       //* change it to xplane data    -100~100
 
  //reset variables
  check1 = 0;
  check2 = 0;
  check3 = 0;
  count = 0;

  memset(tmpArrayR,0,sizeof(tmpArrayR));        //clear exisiting array when getting new serial command
  memset(tmpArrayP,0,sizeof(tmpArrayP));
  memset(tmpArrayY,0,sizeof(tmpArrayY));
delay(50);


roll =100- (analogRead(pin_sensor_roll))/1024.0 * 100;    //A9 * took out "100-"
pitch =analogRead(pin_sensor_pitch)/1024.0 * 100;         //A8 *
roll_desired = valueR; //A14 Used to be joystick values but now I have replaced it with simTool data (-100~100)****
pitch_desired = valueP; //A13 ****
roll_desired=map(constrain(-roll_desired,-45,45),-45,45,0,100);  //****
pitch_desired=map(constrain(pitch_desired,-45,45),-45,45,0,100); //****
control_gain = (analogRead(pin_control_gain)/1024.0) * 7.9 + 0.1;  //*

 roll_control = control_gain*2.55*(roll_desired-roll);  //*the pid?
 roll_control = min(max(-255, roll_control), 255);     //*     it is max at 255
 pitch_control = control_gain*2.55*(pitch_desired-pitch);
 pitch_control = min(max(-255, pitch_control), 255);         //it is max at 255



 if(roll_control < 0)
  {
    analogWrite(pin_actuator_roll_left, 0);            //goes to the left
    analogWrite(pin_actuator_roll_right, -roll_control);
  }
  else
  {
    analogWrite(pin_actuator_roll_right, 0);           //goes to the left
    analogWrite(pin_actuator_roll_left, roll_control);
  }
  if(pitch_control < 0)
  {
    analogWrite(pin_actuator_pitch_back, 0);
    analogWrite(pin_actuator_pitch_front, -pitch_control);
  }
  else
  {
    analogWrite(pin_actuator_pitch_front, 0);
    analogWrite(pin_actuator_pitch_back, pitch_control);
  }


/*
analogWrite( pin_actuator_roll_right, valueR );  //100 to the right 
//Having the right and left and getting the difference is how much the actuator will move it EVERY TIME
//Greater the difference, faster the speed
analogWrite(pin_actuator_roll_left, 50);  
analogWrite( pin_actuator_pitch_front, 50);
analogWrite( pin_actuator_pitch_back, valueP);
*/  
}

Credits

Ethan
12 projects • 2 followers
Welcome to my page! I use this site to record my personal projects. Email me at ethan.peng20@gmail.com if you have any questions.
Contact

Comments

Please log in or sign up to comment.