FSG / Battery_Linear_Actuator

Dependents:   7_21_17_FSG 7_26_17_FSG

Fork of Battery_Linear_Actuator_ by FSG

Battery_Linear_Actuator.cpp

Committer:
mdavis30
Date:
2017-06-09
Revision:
0:645ad86abcba
Child:
1:ca2454ef80d9

File content as of revision 0:645ad86abcba:

#include "Battery_Linear_Actuator.h"

#include "MODSERIAL.h"
Serial MC(p28,p27);         //tx, rx (pins verified)

//CONSTRUCTOR
Battery_Linear_Actuator::Battery_Linear_Actuator()
{
    MC.baud(9600);  //motor controller to MBED connection speed
    motor_absolute_position = 0;            //initialize variables here
    
    linear_actuator_step_size = 1000;   //default confirmed with Dan Edwards
    linear_actuator_motor_speed = 500;  //5 seconds of full travel between 0 and 175,000 (1000 is about 3 seconds)
    
    map_check = 0;
}

//KEYBOARD FUNCTIONS
int Battery_Linear_Actuator::MC_readable()
{
    return MC.readable();
}

string Battery_Linear_Actuator::Keyboard_O()
{      
    //PC.printf("MBED_m_a_p: %d \n", motor_absolute_position); //linear actuator position
    
    MC.printf("pos\r"); //get position from motor controller...maybe rewrite this
                        //get this from the Faulhaber Motor Controller in MATLAB?
    
    char char_buffer [60];
    string Battery_Position_String;
    sprintf(char_buffer,"MBED_m_a_p: %d \n", motor_absolute_position);      //linear actuator position
    Battery_Position_String = char_buffer;
    return Battery_Position_String;
}

string Battery_Linear_Actuator::Keyboard_H()    //Home linear actuator
{
    //reset position in program:
    motor_absolute_position = 0;
    
    MC.printf("hosp-100\r"); wait(0.1); //homing speed -100 (verified 
    MC.printf("ghosp\r");   wait(0.1);//check homing speed
    MC.printf("SHL1\r");    wait(0.1);//setup fault and AnIn
    MC.printf("gohoseq\r"); wait(0.1);//enable the sequence
    
    char homing_string[80];
    sprintf(homing_string,"\nH received! (Homing Sequence In Progress)\nMBED_m_a_p: %d \n", motor_absolute_position);                
    return homing_string;
}

string Battery_Linear_Actuator::Keyboard_E() //enable linear actuator
{
    MC.printf("en\r");
    return "\nE received!\nMOTOR ENABLED!";
}

string Battery_Linear_Actuator::Keyboard_Q() //disable linear actuator
{
    MC.printf("di\r");  
    return "\nQ received!\nMOTOR DISABLED!";
} 

string Battery_Linear_Actuator::Keyboard_EQUAL_KEY()    //INCREASE L.A. step size
{
    if (linear_actuator_step_size <= 9800) //arbitrary stop at 10000
        linear_actuator_step_size += 200;
    char bla_string[80];
    sprintf(bla_string, "\n(MBED) Linear Actuator step size INCREASED: %d\nLA_SZ: %d", linear_actuator_step_size, linear_actuator_step_size);
    
    return bla_string;
}

string Battery_Linear_Actuator::Keyboard_DASH_KEY()    //DECREASE L.A. step size
{
    if (linear_actuator_step_size >= 200)
        linear_actuator_step_size -= 200;
    char bla_string[80];
    sprintf(bla_string, "\n(MBED) Linear Actuator step size DECREASED\nLA_SZ: %d", linear_actuator_step_size, linear_actuator_step_size);
    
    return bla_string;
}

string Battery_Linear_Actuator::Keyboard_LEFT_BRACKET()
{                                                   //100 to 30000, DECREASE (100 increments)
    if (linear_actuator_motor_speed >= 100)
        linear_actuator_motor_speed -= 100;
    //PC.printf("\n(MBED) Linear Actuator Motor Speed INCREASED: sp%d\n", linear_actuator_motor_speed);
    MC.printf("sp%d\r", linear_actuator_motor_speed); //retesting linear actuator motor speed 5/11/17
    //PC.printf("\nLAMSP: %d\n", linear_actuator_motor_speed);
    
    char str[80];
    sprintf(str,"(MBED) Linear Actuator Motor Speed DECREASED\nLA_SP: %d", linear_actuator_motor_speed);
    
    return str;
}

string Battery_Linear_Actuator::Keyboard_RIGHT_BRACKET() //disable linear actuator
{                                                   //100 to 30000, INCREASE (100 increments)
    if (linear_actuator_motor_speed <= 29900)
        linear_actuator_motor_speed += 100;
    //PC.printf("\n(MBED) Linear Actuator Motor Speed INCREASED: sp%d\n", linear_actuator_motor_speed);
    MC.printf("sp%d\r", linear_actuator_motor_speed); //retesting linear actuator motor speed 5/11/17
    //PC.printf("\nLAMSP: %d\n", linear_actuator_motor_speed);
    
    char str[80];
    sprintf(str,"(MBED) Linear Actuator Motor Speed INCREASED\nLA_SP: %d", linear_actuator_motor_speed);
    
    return str;
}

//http://stackoverflow.com/questions/8011700/how-do-i-extract-specific-n-bits-of-a-32-bit-unsigned-integer-in-c
unsigned createMask(unsigned a, unsigned b) {
   unsigned r = 0;
   for (unsigned i=a; i<=b; i++)
       r |= 1 << i;

   return r;
}

string Battery_Linear_Actuator::Keyboard_A()
{
    if (motor_absolute_position >= 0)
    {
        //explicitly written, check if this will not go past 175000
        map_check = motor_absolute_position - linear_actuator_step_size;
        if (map_check >= 0) 
            motor_absolute_position -= linear_actuator_step_size;
    }
                    
    MC.printf("la%d\r", motor_absolute_position);
    MC.printf("m\r");
    
    char position[80];
    sprintf(position, "\nA received! (MBED) motor_absolute_position\nMBED_m_a_p: %d\n", motor_absolute_position);
    return position;
}

string Battery_Linear_Actuator::Keyboard_D()
{
    if (motor_absolute_position <= 175000)
    {
        //explicitly written, check if this will not go past 175000
        map_check = motor_absolute_position + linear_actuator_step_size;
        if (map_check <= 175000) 
            motor_absolute_position += linear_actuator_step_size;
    }
                    
    MC.printf("la%d\r", motor_absolute_position);
    MC.printf("m\r");
    
    char position[80];
    sprintf(position, "\nA received! (MBED) motor_absolute_position\nMBED_m_a_p: %d\n", motor_absolute_position);
    return position;
}

//void Keyboard_P()
//{
//    //Buoyancy Engine gets about 14.5 inches of travel before it hits end of screw (inner piston)
//    //max voltage on potentiometer is 3.06 volts
//    //min voltage on potentiometer is 0.70 volts
//    PC.printf("\nP received!\n");
//    float pot_check = potentiometer_analog_in.read() * 3.3;
//    float pot_check_inches = 6.144 * pot_check - 4.3;
//    float percent_travel_check = (pot_check_inches / 14.5) * 100;               
//    //PC.printf("(MBED) Potentiometer Reading: %f volts %f inches\n", pot_check, pot_check_inches); //  Read the input voltage, represented as a float in the range [0.0, 1.0].
////    PC.printf("Potentiometer Reading: %f volts %f inches %f%c\n ", pot_check, pot_check_inches, percent_travel_check, 37); 
////    PC.printf("\n(MBED) Linear Actuator position: %d\n", motor_absolute_position); //wait(0.1); //Linear Actuator position
//    
//    PC.printf("B_E_POS: %d\n", pot_check); //wait(0.1); //Linear Actuator position
//    PC.printf("MBED_m_a_p: %d \n", motor_absolute_position);
//    
//    MC.printf("pos\r");
//    //MC.printf("pos\r"); wait(0.1); //position
//    //get the next line from the Faulhaber Motor Controller in MATLAB?
//}

//void BE_POSITION()
//{
//    float pot_check = potentiometer_analog_in.read() * 3.3;
//    float pot_check_inches = 6.144 * pot_check - 4.3;
//    float percent_travel_check = (pot_check_inches / 14.5) * 100;               
//    PC.printf("B_E_POS: %d\n", pot_check); //wait(0.1); //Linear Actuator position
//}

//
//char ioc_char;          //linear actuator i/o configuration
//int ioc_array[28];      //http://stackoverflow.com/questions/439573/how-to-convert-a-single-char-into-an-int