Inverted Pendulum / Mbed 2 deprecated IP-Interface

Dependencies:   mbed QEI

main.cpp

Committer:
Snay22
Date:
2016-11-16
Revision:
18:5a1c351094d2
Parent:
15:a0b5c4306246
Child:
19:0e9bf6d61d0d

File content as of revision 18:5a1c351094d2:

#include "QEI.h"
#include "Motor.h"
#include "pot.h"
#include <time.h>

 
Serial pc(USBTX, USBRX);
//Use X4 encoding.
//QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING);
//Use X2 encoding by default.
QEI encoder (PTC16, PTC17, PTB9, 512);
Motor motor(PTC4, PTD0, PTC3, PTC12, PTB23, 2000);
Pot pend(PTC10,0); 
void test_distance(){
    int wait = 1000;
    int steps = 100;
    while (wait > 500){
        while (steps > 10){
            printf("steps: %i wait: %i \r\n", steps, wait);
            for( int i = 0; i < 3; i++){
                encoder.reset();
                motor.step_clockwise(steps, wait);
                int pulses_cw = encoder.getPulses();
                wait_ms(200);
                int coast_pulses_cw = encoder.getPulses();
                
                encoder.reset();
                motor.step_anticlockwise(steps, wait);
                int pulses_ccw = encoder.getPulses();
                wait_ms(200);
                int coast_pulses_ccw = encoder.getPulses();
                printf("trial: %i \tclockwise pluses: \t%i  \tanticlockwise pluses: \t%i \r\n", i + 1, pulses_cw, pulses_ccw);
                printf("\t\tpulses after coast: \t%i \tpulses after coast: \t%i\r\n", coast_pulses_cw, coast_pulses_ccw);
            }
            steps -=10;
            printf("\r\n");
        }
        
        wait-=100;
        steps=100;
    }       
}

void test_speed() {
    time_t start;
    double elapsed_time = 0;
    int steps = 10;
    int wait = 1000;
    printf("steps: %i\r\n", steps);
            for( int i = 0; i < 3; i++){
                encoder.reset();
                start = time(0);
                motor.step_clockwise(steps, wait);
                elapsed_time = difftime( time(0), start);
                printf("Round %i clockwise, Total duration: \t%f seconds\r\n", i, elapsed_time);
                wait_ms(200);
                
                encoder.reset();
                start = time(0);
                motor.step_anticlockwise(steps, wait);
                elapsed_time = difftime( time(0), start);
                printf("Round %i counter-clockwise, Total duration: \t%f seconds\r\n", i, elapsed_time);
                wait_ms(200);
            }
}


void test_pendulum(){
    while(1){
        printf("angle: %f \r\n", pend.get_angle());   
    }
}
void calibrate_pendulum(){
    encoder.reset();
    while(1){
        printf("voltage %%: %f  pulse: %i", pend.get_voltage(), encoder.getPulses());
    }
}
void move_pulses(int pulses){ // find number of pulses from the encoder going from one end to the other.
    int start = encoder.getPulses();
    if(pulses >=0){
        while(encoder.getPulses() < start + pulses){
            motor.clockwise();    
        } 
    }else{
        while(encoder.getPulses() > start + pulses)
            motor.anticlockwise();
        } 
}
void disttopulse(int distance) //find amount of pulses to distance
{
    
}
void set_speed(int speed) // wait time correspondence to speed
{
        
}

void test_pulsetodist() // have user input x amount of pulses to move motor a certain distance
{
    while(1)
    {
        char str[4];
        int steps = 0;
        int wait = 1000;
        time_t start = time(0);
        double elapsed;      
        printf("Enter the amount of steps: ");
        fgets(str,4,stdin);
        printf("\r\nSteps: %s", str);
        steps = atoi(str);
        
        if(steps > 0)
            motor.step_clockwise(steps, wait);
        else
            motor.step_anticlockwise(steps, wait);
        elapsed = difftime( time(0), start);
        printf("\r\nTook %i steps in time: %f \r\n",steps, elapsed);
    }
}
int main() {
    while(1)
        printf("Pulses: %i\r\n", encoder.getPulses());
    //motor.step_clockwise(100,1500);
    //motor.step_clockwise(100,1000);
    //motor.step_clockwise(100,700);
    
    
 
}