Program for the water play project for the course Software Testing Practical 2016 given at the VU University

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

SalinityController.cpp

Committer:
joran
Date:
2016-06-13
Revision:
26:966bad4c6365
Parent:
11:1a0a8fd74bc0
Child:
32:1e4919a44196

File content as of revision 26:966bad4c6365:

#include "SalinityController.h"

AnalogIn salinity_sensor(p19);

void SalinityController::update() {
    this->salinity = SalinityController::getAdjustedPPT();
}

float SalinityController::getValue() {
    return this->salinity;    
}

// Function reads sensor values and averages N sensor values
float SalinityController::readSensor()
{
    float analogin_value = 0;
    float ppt = 0;
    
    // Read 0-1.0 value
    for(int i = 0; i < N; i++)
    {
        analogin_value += salinity_sensor.read();
    }
    // Average 10 sensor values
    analogin_value /= (float) N;
    
    ppt = analoginToPPT(analogin_value);
    
    return ppt;
}

// Function converts average sensor value to ppt
float SalinityController::analoginToPPT(float analogin_value)
{
    float voltage = 0;
    float ppt = 0;
    
    // Convert average sensor value to value with range 0-5.0v
    voltage = analogin_value * 3.3f * (5.0f/3.0f);
    
  //  printf("Sensor Voltage: %f \t", voltage);
    
    // Convert voltage to ppt
//    ppt = voltage * 16.3;
    ppt = voltage * 15.1747217178 - 2.89491343498;
    
    return ppt;
}

float SalinityController::getPPT()
{
    float ppt = 0;
    
    ppt = readSensor();
    
    return ppt;
}

float SalinityController::getVoltage()
{
    float voltage = 0;
    
    float analogin_value = 0;
   
    
    // Read 0-1.0 value
    for(int i = 0; i < N; i++)
    {
        analogin_value += salinity_sensor.read();
    }
    // Average 10 sensor values
    analogin_value /= (float) N;    
    voltage = analogin_value * 3.3f * (5.0f/3.0f);
    
    return voltage;
}

float SalinityController::voltToSensor(float inputvolt)
{
    float slope = 0.7931723;
    float intercept = 0.0050561;
    
    float offset = (slope * inputvolt) - intercept;
    
    return inputvolt + offset;
}

float SalinityController::sensorToPPT(float inputvolt)
{
    float minimum = 0.03778499;
    if (inputvolt <= minimum) return 0.00;
    
    float slope = 11.53368;
    float intercept = 0.43580;
    
    float ppt = (slope * inputvolt) - intercept;
    
    return ppt;
}

float SalinityController::getAdjustedPPT()
{
  return sensorToPPT(voltToSensor(getVoltage()));   
}
// Function prints salinity sensor value in ppt
void SalinityController::displayPPT()
{
    float ppt = 0;
    
    ppt = readSensor();
    
    printf("Salinity: %f ppt\t", ppt);
}

std::string SalinityController::get_name() {
    return "SalinityController";
}

float SalinityController::getMl(float gramsinbody, float volumeinbody)
{
    
    float solvolume = 10; // 10ml
    
    float idealpptconstant = 0.007f; //7 ppt / 1000
    float x1 = volumeinbody * idealpptconstant;
    float x2 = solvolume * idealpptconstant;
    
    x1 = x1 - gramsinbody;
    x2 = 1 - x2;
    
    float outputml = (x1 / x2 * solvolume);
    
    return outputml; // amount in ml to get 7 ppt.
    
}