Friday, December 15, 2017

Lab 22 - MPU9255 Sensor Module

CLASSWORK:
Task: get YPR data off of the MPU:
  1. Download the Sparkfun MPU-9250 9 DOF IMU Breakout Library from the Arduino IDE's Library Manager. Make sure the author is Sparkfun Electronics and that it says Version 1.0.0.
  2. Go to Examples and open up the MPU9250BasicsAHRS code.
  3. Wire up the sensor to the Arduino: VCC to 3.3v, GND to GND, SDA to A4, SCL to A5.
  4. Upload the sample code to the Arduino.
  5. Open up the Serial Monitor to see the data streams.
Simplified MPU 9250 sample code (w/o YPR data)
#include "MPU9250.h"
MPU9250 myIMU;
void setup()
{
 Wire.begin();
 Serial.begin(38400);
 myIMU.MPU9250SelfTest(myIMU.SelfTest); // Start by performing self test and reporting values
 myIMU.calibrateMPU9250(myIMU.gyroBias, myIMU.accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
 myIMU.initMPU9250(); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
 myIMU.initAK8963(myIMU.magCalibration); // Initialize device for active mode read of magnetometer
}
void loop()
{
 if (myIMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
 {
 myIMU.readAccelData(myIMU.accelCount); // Read the x/y/z adc values
 myIMU.getAres();
 // Now we'll calculate the accleration value into actual g's
 // This depends on scale being set
 myIMU.ax = (float)myIMU.accelCount[0]*myIMU.aRes; // - accelBias[0];
 myIMU.ay = (float)myIMU.accelCount[1]*myIMU.aRes; // - accelBias[1];
 myIMU.az = (float)myIMU.accelCount[2]*myIMU.aRes; // - accelBias[2];
 myIMU.readGyroData(myIMU.gyroCount); // Read the x/y/z adc values
 myIMU.getGres();
 // Calculate the gyro value into actual degrees per second
 // This depends on scale being set
 myIMU.gx = (float)myIMU.gyroCount[0]*myIMU.gRes;
 myIMU.gy = (float)myIMU.gyroCount[1]*myIMU.gRes;
 myIMU.gz = (float)myIMU.gyroCount[2]*myIMU.gRes;
 myIMU.readMagData(myIMU.magCount); // Read the x/y/z adc values
 myIMU.getMres();
 myIMU.magbias[0] = +470.;
 // User environmental x-axis correction in milliGauss TODO axis??
 myIMU.magbias[1] = +120.;
 // User environmental x-axis correction in milliGauss
 myIMU.magbias[2] = +125.;
 myIMU.mx = (float)myIMU.magCount[0]*myIMU.mRes*myIMU.magCalibration[0] -
 myIMU.magbias[0];
 myIMU.my = (float)myIMU.magCount[1]*myIMU.mRes*myIMU.magCalibration[1] -
 myIMU.magbias[1];
 myIMU.mz = (float)myIMU.magCount[2]*myIMU.mRes*myIMU.magCalibration[2] -
 myIMU.magbias[2];
 } // if (readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
 // Must be called before updating quaternions!
 myIMU.updateTime();
 myIMU.delt_t = millis() - myIMU.count;
 if (myIMU.delt_t > 500)
 {
 // Print acceleration values in milligs!
 Serial.print("X-acceleration: "); Serial.print(1000*myIMU.ax);
 Serial.print(" mg ");
 Serial.print("Y-acceleration: "); Serial.print(1000*myIMU.ay);
 Serial.print(" mg ");
 Serial.print("Z-acceleration: "); Serial.print(1000*myIMU.az);
 Serial.println(" mg ");
 // Print gyro values in degree/sec
 Serial.print("X-gyro rate: "); Serial.print(myIMU.gx, 3);
 Serial.print(" degrees/sec ");
 Serial.print("Y-gyro rate: "); Serial.print(myIMU.gy, 3);
 Serial.print(" degrees/sec ");
 Serial.print("Z-gyro rate: "); Serial.print(myIMU.gz, 3);
 Serial.println(" degrees/sec");
 // Print mag values in degree/sec
 Serial.print("X-mag field: "); Serial.print(myIMU.mx);
 Serial.print(" mG ");
 Serial.print("Y-mag field: "); Serial.print(myIMU.my);
 Serial.print(" mG ");
 Serial.print("Z-mag field: "); Serial.print(myIMU.mz);
 Serial.println(" mG");
 //myIMU.tempCount = myIMU.readTempData(); // Read the adc values
 // Temperature in degrees Centigrade
 //myIMU.temperature = ((float) myIMU.tempCount) / 333.87 + 21.0;
 // Print temperature in degrees Centigrade
 //Serial.print("Temperature is "); Serial.print(myIMU.temperature, 1);
 //Serial.println(" degrees C");
 myIMU.count = millis();
 }
}

HOMEWORK:
Currently, the header file for the MPU 9250 is written such that the maximum acceleration that can be read by the accelerometer is 2g's. Modify the header file so that it allows the MPU 9250 to read a maximum acceleration of 16g's.


Lab 25 - Real Time Telemetry

CLASSWORK:
Sample Arduino program to test out with the telemetry:
int k=0;
void setup() {
  Serial.begin(57600);
}

void loop() {

  Serial.print(k);
  Serial.println("Testing Connection");
  k++;
  delay(50);
}


Note: This lab was done in tandem with our third project. We wanted to have real-time data retrieval from our MPU-9250 sensor module using the telemetry setup. Hence, we have auxiliary wiring for those components.
Pinout for RT telemetry

Output to PuTTy for RT telemetry

Sunday, November 5, 2017

Lab 4 - RGB LED

CLASSWORK:
RGB LED Control with Debounced Button
const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int BUTTON=2; //The Button is connected to pin 2
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
void setup()
{
 pinMode (BLED, OUTPUT); //Set Blue LED as Output
 pinMode (GLED, OUTPUT); //Set Green LED as Output
 pinMode (RLED, OUTPUT); //Set Red LED as Output
 pinMode (BUTTON, INPUT); //Set button as input (not required)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON); //Read the button state
 if (last != current) //if it's different...
 {
 delay(5); //wait 5ms
 current = digitalRead(BUTTON); //read it again
 }
 return current; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
 //RED
 if (mode == 1)
 {
 digitalWrite(RLED, HIGH);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, LOW);
 }
 //GREEN
 else if (mode == 2)
 {
 digitalWrite(RLED, LOW);
 digitalWrite(GLED, HIGH);
 digitalWrite(BLED, LOW);
 }
 //BLUE
 else if (mode == 3)
 {
 digitalWrite(RLED, LOW);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, HIGH);
 }
 //PURPLE (RED+BLUE)
 if (mode == 4)
 {
 digitalWrite(RLED, HIGH);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, HIGH);
 }
 //WHITE (RED+GREEN+BLUE)
 if (mode == 5)
 {
  digitalWrite(RLED, HIGH);
  digitalWrite(GLED, HIGH);
  digitalWrite(BLED, HIGH);
 }
 //ORANGE
 if (mode == 6)
 {
  analogWrite(RLED, 127);
  analogWrite(GLED, 127);
  analogWrite(BLED, 0);
 }
 //TEAL
 else if (mode == 7)
 {
  analogWrite(RLED, 0);
  analogWrite(GLED, 127);
  analogWrite(BLED, 127);
 }
 //OFF (mode = 0)
 else
 {
 digitalWrite(RLED, LOW);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, LOW);
 }
}
void loop()
{
 currentButton = debounce(lastButton); //read debounced state
 if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
 {
 ledMode++; //increment the LED value
 }
 lastButton = currentButton; //reset button value
 //if you've cycled through the different options,
 //reset the counter to 0
 if (ledMode == 8) ledMode = 0;
 setMode(ledMode); //change the LED state

}

HOMEWORK:
RGB LED Control with Switch Statements
const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int BUTTON1=2; //The Button is connected to pin 2
const int BUTTON2=3;
const int BUTTON3=4;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int mode = 0; //Cycle between LED states
void setup()
{
 pinMode (BLED, OUTPUT); //Set Blue LED as Output
 pinMode (GLED, OUTPUT); //Set Green LED as Output
 pinMode (RLED, OUTPUT); //Set Red LED as Output
 pinMode (BUTTON, INPUT); //Set button as input (not required)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON1); //Read the button state
 if (last != current) //if it's different...
 {
 delay(5); //wait 5ms
 current = digitalRead(BUTTON1); //read it again
 }
 return current; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
switch (mode)
{
case 1:
  digitalWrite(RLED, HIGH);
  digitalWrite(GLED, LOW);
  digitalWrite(BLED, LOW);
  break;
case 2: 
  digitalWrite(RLED, LOW);
  digitalWrite(GLED, HIGH);
  digitalWrite(BLED, LOW);
  break;
case 3: 
  digitalWrite(RLED, LOW);
  digitalWrite(GLED, LOW);
  digitalWrite(BLED, HIGH);
  break;
case 4: 
  digitalWrite(RLED, HIGH);
  digitalWrite(GLED, LOW);
  digitalWrite(BLED, HIGH);
break;
case 5: 
  digitalWrite(RLED, HIGH);
  digitalWrite(GLED, HIGH);
  digitalWrite(BLED, HIGH);
  break;
case 6: 
  analogWrite(RLED, 127);
  analogWrite(GLED, 127);
  analogWrite(BLED, 0);
  break;
case 7: 
  analogWrite(RLED, 0);
  analogWrite(GLED, 127);
  analogWrite(BLED, 127);
  break;
default:
  digitalWrite(RLED, LOW);
  digitalWrite(GLED, LOW);
  digitalWrite(BLED, LOW);
break;
}
}
void loop()
{
 currentButton = debounce(lastButton); //read debounced state
 if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
 {
 mode++; //increment the LED value
 }
 lastButton = currentButton; //reset button value
 //if you've cycled through the different options,
 //reset the counter to 0
 if (mode == 8) mode = 0;
 setMode(mode); //change the LED state
}


Lab 3 - Digital Inputs and Indexed Loops

CLASSWORK:
Blinking LED with For Loop (aka PWM)
const int LED=9; //define LED for Pin 9
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an output
}
void loop()
{
 for (int i=100; i<=1000; i=i+100)
 {
 digitalWrite(LED, HIGH);
 delay(i);
 digitalWrite(LED, LOW);
 delay(i);
 }

}

LED with Changing Brightness
const int LED=9; //define LED for Pin 9
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an output
}
void loop()
{
 for (int i=0; i<256; i++)
 {
 analogWrite(LED, i);
 delay(10);
}
 for (int i=255; i>=0; i--)
 {
 analogWrite(LED, i);
 delay(10);
 }

}

Basic Button Without Debounce
const int LED=9; //The LED is connected to pin 9
const int BUTTON=2; //The Button is connected to pin 2
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an output
 pinMode (BUTTON, INPUT); //Set button as input (not required)
}
void loop()
{
 if (digitalRead(BUTTON) == LOW)
 {
 digitalWrite(LED, LOW);
 }
 else
 {
 digitalWrite(LED, HIGH);
 }

}

HOMEWORK:
Basic Button With Debounce
const int LED=9; //The LED is connected to pin 9
const int BUTTON=2; //The Button is connected to pin 2
boolean lastButton = LOW; //Variable containing the previous button state
boolean currentButton = LOW; //Variable containing the current button state
boolean ledOn = false; //The present state of the LED (on/off)
void setup()
{
 pinMode (LED, OUTPUT); //Set the LED pin as an output
 pinMode (BUTTON, INPUT); //Set button as input (not required)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON); //Read the button state
 if (last != current) //if it's different…
 {
 delay(5); //wait 5ms
 current = digitalRead(BUTTON); //read it again

 return current; //return the current value
}
void loop()
{
 currentButton = debounce(lastButton); //read debounced state
 if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
 {
 ledOn = !ledOn; //toggle the LED value
 }
 lastButton = currentButton; //reset button value

 digitalWrite(LED, ledOn); //change the LED state

}



Day 3 - Numerical Technique: Linear Interpolation, Math Functions. Brinicles and Open Jet Engines

CLASSWORK/ HOMEWORK:
Freezing Temperature of Seawater
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/*  This program uses linear interpolation to                  */
/*  compute the freezing temperature of seawater.              */
#include <stdio.h> 
#include <math.h>
int main(void)
{
/*  Declare variables.  */
double salinity_a, freezpt_a, new_salinity_b, freezpt_b, salinity_c, freezpt_c;
/*  Get user input from the keyboard.  */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&salinity_a,&freezpt_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&salinity_c,&freezpt_c);
printf("Enter new salinity: \n");
scanf("%lf",&new_salinity_b);
//  Use linear interpolation to compute   new freezing temperature.            
freezpt_b = freezpt_a + (new_salinity_b - salinity_a)/(salinity_c - salinity_a)*(freezpt_c - freezpt_a);
/*  Print new freezing temperature.  */
printf("New freezing temperature in degrees F: %4.1f \n",freezpt_b);
return 0;  /*  Exit program.  */

}

Open Jet Engine Velocity and Acceleration
#include <math.h>
#include <stdio.h>

int main(void)
{
  double Velocity,acceleration, t_s;
  printf("Input time elapsed in seconds:\n");
  scanf("%lf",&t_s);
  Velocity = .00001*(t_s*t_s*t_s)-.00488*(t_s*t_s)+.75795*(t_s)+181.3566;
  acceleration = 3-.000062*(Velocity*Velocity);
  printf("Velocity:%lf m/s\n", Velocity);
  printf("Acceleration: %lf m/s/s\n",acceleration);
 return 0;

}

Logarithm of Base 'y'
#include <stdio.h>
#include <math.h>

int main (void)
{
 double x, y, log_y;
 printf("Logarithm of x to Base y: \n");
 printf("\nInput positive numbers for x & y: \n");
 scanf("%lf" "%lf",&x, &y);
 log_y = log(x)/log(y);
 if (x <= 0 || y <= 0)
 {
  printf("Sorry, please input a positive number. \n");
 } 
 else
 {
  printf("The logarithm of x to base y is %5.1f \n", log_y);
 }
 return 0;

}

Day 4 - Top Down Programming, Logical and Conditional statements

CLASSWORK:
If the denominator is close to zero
if (fabs(denominator) < 0.0001) 
printf("Denominator close to zero"); 
else { 
x = numerator/denominator; 
printf("x = %f \n",x); 
}

Example of an if/else statement
if (code == 10) 
  printf("Too hot - turn equipment off \n"); 
    else 
    { 
      if (code == 11) 
        printf("Caution - recheck in 5 minutes \n"); 
          else { 
            if (code == 13) 
              printf("Turn on circulating fan \n"); 
                else 
                  printf("Normal mode of operation \n"); 
            } 
} 

An equivalent statement is the following switch statement:

switch (code) 
{ 
case 10: 
printf("Too hot - turn equipment off \n"); 
break; 
case 11: 
printf("Caution - recheck in 5 minutes \n"); 
break; 
case 13: printf("Turn on circulating fan \n"); 
break; 
default: 
printf("Normal temperature range \n"); 
break; 
} 

HOMEWORK:
Give the value of a after the following set of statements is executed.
int a = 750; 
... 
if (a>0) //if a is greater than zero
if (a >= 1000) //check if a is greater than or equal to 1000
a = 0; //if the previous statements are true, set a equal to zero
else if (a < 500) //if not, check if a is less than 500
a *= 2; //if the previous statement is true, let a equal 2a
else 
a *= 10; //if a is greater than or equal to 500, let a equal 10a 
else 
a += 3; //if a is less than or equal to zero, let a equal 3+a

Since a = 750, a = a*10 => a = 7500



Day 5 - Loops

CLASSWORK:
Degrees to Radians Using While Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a while loop structure. */ 
#include <stdio.h>
#define PI 3.141593 

int main(void) {
 /* Declare and initialize variables. */ 
int degrees=0; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
while (degrees <= 360) { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 
degrees += 10; 
} 
/* Exit program. */ 
return 0; 
}

Degrees to Radians Using Do/While Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a do-while loop structure. */ 
#include <stdio.h> 
#define PI 3.141593

int main(void) { 
/* Declare and initialize variables. */ 
int degrees=0; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
do { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 
degrees += 10; 
} while (degrees <= 360); 
/* Exit program. */ 
return 0; 
} 

Degrees to Radians Using For Loop
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ 
/* This program prints a degree-to-radian table */ 
/* using a for loop structure. */ 
#include <stdio.h> 
#define PI 3.141593 

int main(void) { 
/* Declare variables. */ 
int degrees; 
double radians; 
/* Print radians and degrees in a loop. */ 
printf("Degrees to Radians \n"); 
for (degrees=0; degrees<=360; degrees+=10) { 
radians = degrees*PI/180; 
printf("%6i %9.6f \n",degrees,radians); 
} 
/* Exit program. */ 
return 0; 
} 

HOMEWORK:
Wave Generator Code
#include <stdio.h>
#include <math.h>
#define PI 3.141592

int main (void)
{
  double wl_1, wl_2, wl_3, wh_1, wh_2, per_1, per_2, per_c, time;
  double sum, wavemax,steps, w_1, w_2, w_3;
  printf("Input period and wave height:\n" );
  printf("Wave 1: \n");
  scanf("%f" "%f", &per_1, &wh_1);
  printf("Wave 2: \n");
  scanf("%f" "%f", &per_2, &wh_2);
  
  wl_1 = 5.13*per_1*per_1;
  wl_2 = 5.13*per_2*per_2;
  
  per_c = per_1*per_2;
  wl_3 = 5.13*per_c*per_c;
  
  w_1 = .5*wh_1*sin((2*PI/per_1)*time);
  w_2 = .5*wh_2*sin((2*PI/per_2)*time);
  w_3 = .5*(wh_1 + wh_2)*sin((2*PI/per_c)*time);
  sum = w_1 + w_2;
  
  steps = per_c/200;

/*set new period to the product of the wave periods
set time increment to new period/200
set wavemax to 0
set time to 0
set steps to 0
while steps <= 199
set sum to wave 1 + wave 2
if sum > wavemax
set wavemax to sum
add 1 to steps
print wavemax*/
printf("Individual Wavelengths:");
for (time=0, sum=0; time<=2; time += steps)
{
  printf("WL 1: %5.4f""WL 2: %5.4f""WL 3: %5.4f", wl_1, wl_2, wl_3);
  if (sum > wavemax)
  {
    wavemax = sum;
  }
continue;
}
printf("Max Wave Height: %f", wavemax);

  return 0;
}