Skip to main content

Day 27: Live Telemetry and MPU 9255 | C++ Classes with Overloaded Operators

Hardware: Live Telemetry and MPU 9255

Real Time Telemetry

Telemetry seemed complicated, but it was a simple matter of hooking up the sensors correctly and getting Putty to communicate with the serial port. This portion was done fairly quickly.

MPU 9255 + BMP280


Software: C++ Classes with Overloaded Operators

Complex Class with Overloaded Operators

  • Complex numbers needed to solve many problems in science and engineering, esp. physics and electrical engineering, therefore useful to define complex class in C++
Recall: complex number has form 
a+bi where i=sqrt(-1) and a and b are real numbers
Figure 1.1
Real and imaginary axis
X is real axis and Y is imaginary axis

Complex Class Definition

  • Class declaration must include private members for real and imaginary components of complex number
  • Public members include a function to input complex number from keyboard and function to print complex number on screen
  • Also need member functions to define arithmetic operators
Example 1.1.1
// These statements define a class for complex numbers.
// This declaration is stored in complex.h.
#include <iostream>
#include <cmath>
using namespace std;
class complex
{
// Declare function prototypes for public members.
public:
complex();
complex(double a, double b);
void print();
void input();
double magn(complex);
double angle(complex);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
// Declare private members.
private:
double real, imag;
};
//
// Class implementation
//
// These statements define implementation of a complex class.
// This function is the default constructor to initialize a
// complex number that is not given a value.
complex::complex()
{
real = 0;
imag = 0;
}
// This function is the constructor to initialize a complex
// number to a specified value.
complex::complex(double a, double b)
{
real = a;
imag = b;
}
// This function prints a complex number.
void complex::print()
{
if (imag > 0)
cout << real << "+" << imag << "i" << endl;
else
if (imag == 0)
cout << real << endl;
else
cout << real << imag << "i" << endl;
}
// This function reads two values for a complex number.
void complex::input()
{
cin >> real >> imag;
}
// This function defines the sum of complex numbers.
complex complex::operator+(complex c)
{
// Definition of complex addition.
complex temp;
temp.real = c.real + real;
temp.imag = c.imag + imag;
return temp;
}
// This function defines the difference of complex numbers.
complex complex::operator-(complex c)
{
// Definition of complex subtraction.
complex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
// This function defines the product of complex numbers.
complex complex::operator*(complex c)
{
// Definition of complex multiplication.
complex temp;
temp.real = (real*c.real - imag*c.imag);
temp.imag = (imag*c.real + real*c.imag);
return temp;
}
// This function defines the quotient of complex numbers.
complex complex::operator/(complex c)
{
// Definition of complex division.
complex temp;
temp.real = (real*c.real + imag*c.imag)/
(pow(c.real,2) + pow(c.imag,2));
temp.imag = (imag*c.real - real*c.imag)/
(pow(c.real,2) + pow(c.imag,2));
return temp;
}

Example 1.1.2
// This program demonstrates the use of
// the complex number class and its operations.
#include <iostream>
#include "complex.h"
using namespace std;
int main(void)
{
// Declare and initialize variables.
complex c1(4,1), c2(-3,-2), c3;
// Print initial values.
cout << "c1:";
c1.print();
cout << "c2:";
c2.print();
cout << "c3:";
c3.print();
// Compute and print new values.
c3 = c1 + c2;
cout << "c1+c2 = ";
c3.print();
c3 = c1 - c2;
cout << "c1-c2 = ";
c3.print();
c3 = c1*c2;
cout << "c1*c2 = ";
c3.print();
c3 = c1/c2;
cout << "c1/c2 = ";
c3.print();
system("pause"); //Add system pause
return 0; //Exit program
}
Figure 2.1
Output from example 1.1

Complex Roots for Quadratic Equations

The song to find the roots of a quadratic equation is permanently burned into my head from 7th grade Algebra 1. "X equals negative b, plus or minus square root, b squared minus four a c, divided by two a." In mathematical terms, x=[-b (+/-) sqrt(b^2-4ac)]/2a. The term under the square root symbol is called the discriminant. If it is greater than or equal to zero, the two roots are real, but if it is less than zero, the two roots are complex values. 

Example 2.1.1
I kept getting an error that stated that 'real' and 'imag' were private within the context. I tried changing real and imag to public members, which seemed to fix the problems, though I don't know if that will have any adverse effects on the program. My computer hasn't exploded, though, so that's a good sign. 

// These statements define a class for complex numbers.
// This declaration is stored in complex.h.
#include <iostream>
#include <cmath>
using namespace std;
class complex
{
// Declare function prototypes for public members.
public:
complex();
complex(double a, double b);
void print();
void input();
double magn(complex);
double angle(complex);
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
double real, imag;
};
//
// Class implementation
//
// These statements define implementation of a complex class.
// This function is the default constructor to initialize a
// complex number that is not given a value.
complex::complex()
{
real = 0;
imag = 0;
}
// This function is the constructor to initialize a complex
// number to a specified value.
complex::complex(double a, double b)
{
real = a;
imag = b;
}
// This function prints a complex number.
void complex::print()
{
if (imag > 0)
cout << real << "+" << imag << "i" << endl;
else
if (imag == 0)
cout << real << endl;
else
cout << real << imag << "i" << endl;
}
// This function reads two values for a complex number.
void complex::input()
{
cin >> real >> imag;
}
// This function defines the sum of complex numbers.
complex complex::operator+(complex c)
{
// Definition of complex addition.
complex temp;
temp.real = c.real + real;
temp.imag = c.imag + imag;
return temp;
}
// This function defines the difference of complex numbers.
complex complex::operator-(complex c)
{
// Definition of complex subtraction.
complex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
// This function defines the product of complex numbers.
complex complex::operator*(complex c)
{
// Definition of complex multiplication.
complex temp;
temp.real = (real*c.real - imag*c.imag);
temp.imag = (imag*c.real + real*c.imag);
return temp;
}
// This function defines the quotient of complex numbers.
complex complex::operator/(complex c)
{
// Definition of complex division.
complex temp;
temp.real = (real*c.real + imag*c.imag)/
(pow(c.real,2) + pow(c.imag,2));
temp.imag = (imag*c.real - real*c.imag)/
(pow(c.real,2) + pow(c.imag,2));
return temp;

}

Example 2.1.2
// This program computes and prints the roots of a quadratic
// equation. It uses the user-defined complex class.
#include <iostream>
//#include <cmath>
#include "complex.h"
using namespace std;
int main(void)
{
// Declare and initialize variables.
double a, b, c, term1, disc;
complex root1, root2;
// Read values for a, b, c from the keyboard.
cout << "Enter real values a, b, c:" << endl;
cin >> a >> b >> c;
// Compute roots of quadratic equation.
term1 = -b/(2*a);
disc = b*b - 4*a*c;
if (disc >= 0)
    {
        root1.real = term1 + sqrt(disc)/(2*a);
        root2.real = term1 - sqrt(disc)/(2*a);
    }
    else
    {
        root1.real = term1;
        root1.imag = sqrt(-disc)/(2*a);
        root2.real = term1;
        root2.imag = -sqrt(-disc)/(2*a);
    }
// Print roots.
cout << "Roots:" << endl;
root1.print();
root2.print();
//Add System Pause
system("pause");
// Exit program.
return 0;

}
Figure 3.1
Output from Example 2.1

Figure 3.2
Output from Example 2.1

Homework

Hardware

No homework

Software

1. Test the program with an equation that will give a double root. Does it handle that situation correctly?

Figure 4.1
Double root example

2. Modify the program so that it also prints one of these phrases: "real roots", "double roots", or "complex roots".

Code:
// This program computes and prints the roots of a quadratic
// equation. It uses the user-defined complex class.
#include <iostream>
//#include <cmath>
#include "complex.h"
using namespace std;
int main(void)
{
// Declare and initialize variables.
double a, b, c, term1, disc;
// Read values for a, b, c from the keyboard.
cout << "Enter real values a, b, c:" << endl;
cin >> a >> b >> c;
complex root1, root2;
// Compute roots of quadratic equation.
term1 = -b/(2*a);
disc = b*b - 4*a*c;
if (disc >= 0)
    {
        root1.real= term1 + sqrt(disc)/(2*a);
        root2.real = term1 - sqrt(disc)/(2*a);
        if(disc==0)
        {
        cout << "Double Roots" << endl;
        }
        else
        {
         cout << "Real Roots" << endl;
        }
    }
    else
    {
        root1.real = term1;
        root1.imag = sqrt(-disc)/(2*a);
        root2.real = term1;
        root2.imag = -sqrt(-disc)/(2*a);
        cout << "Complex Roots" << endl;
    }
// Print roots.
cout << "Roots:" << endl;
root1.print();
root2.print();
//Add System Pause
system("pause");
// Exit program.
return 0;
}

3. Modify the program so that it prints two decimal positions for the roots. 



Comments

Popular posts from this blog

Day 25: Python, Graphing, 3D Visualization and Inertial Measurement

Python Arduino Code int trigPin=13; //Sensor Trig pin connected to Arduino pin 13 int echoPin=11; //Sensor Echo pin connected to Arduino pin 11 float pingTime; //time for ping to travel from sensor to target and return float targetDistance; //Distance to Target in inches float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees. void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(trigPin, LOW); //Set trigger pin low delayMicroseconds(2000); //Let signal settle digitalWrite(trigPin, HIGH); //Set trigPin high delayMicroseconds(15); //Delay in high state digitalWrite(trigPin, LOW); //ping has now been sent delayMicroseconds(10); //Delay in low state pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds pingTime=pingTime/1000000; //convert pin...

Day 19: Project 2

Our project was a test stand for rocket motors Using an LCD screen, we could choose a display mode, and it would display the selected value. Unfortunately, we were unable to calibrate the pressure sensor, so all we ended up with was a load cell. We used the HX711 library for this. The LCD screen displayed the maximum force experienced during the test Code for MotorSensors.h #ifndef _MOTORSENSORS_ #define _MOTORSENSORS_ #include <LiquidCrystal.h> //Include Liquid Crystal Library #include <HX711.h> //Include HX711 Load Cell library #include "HX711.h" //Include Load Cell header file #include <math.h> //Include math library #define calibration_factor -8815 //set calibration factor #define DAT 3 //define DAT pin #define CLK 2 //define CLK pin #define pressurePin A1 #define tempPin A2 HX711 scale(DAT,CLK);  LiquidCrystal lcd(8,9,4,5,6,7); //Define LCD pins //Initialize variables float maxkg=0;  float currentval=0; float maxforce=0; char mo...