Runs a DC motor for 30 interations to evaluate transfer function of physical parameters

Dependencies:   mbed mbedWSEsbc

Committer:
DCamuti
Date:
Mon Nov 09 01:41:59 2015 +0000
Revision:
0:968adb203327
ES305 Lab 5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DCamuti 0:968adb203327 1 #include "mbed.h"
DCamuti 0:968adb203327 2 #include "mbedWSEsbc.h"
DCamuti 0:968adb203327 3 #define PI (3.14159)
DCamuti 0:968adb203327 4
DCamuti 0:968adb203327 5 // Declaring variables
DCamuti 0:968adb203327 6 int go; // variable passed from MATLAB to start data collection
DCamuti 0:968adb203327 7 float st = 1.0; // desired sampling time
DCamuti 0:968adb203327 8 float sp = 0.01; // sampling period
DCamuti 0:968adb203327 9 float dt; // loop time
DCamuti 0:968adb203327 10 long O; // oscillations measured
DCamuti 0:968adb203327 11 float OR;// oscillations in radians
DCamuti 0:968adb203327 12 float low;
DCamuti 0:968adb203327 13 float high;
DCamuti 0:968adb203327 14 float ang_pos_prev = 0.0;
DCamuti 0:968adb203327 15 float omega;
DCamuti 0:968adb203327 16 float dc;
DCamuti 0:968adb203327 17 float Time; // reset time variable
DCamuti 0:968adb203327 18
DCamuti 0:968adb203327 19 int main()
DCamuti 0:968adb203327 20 {
DCamuti 0:968adb203327 21 mbedWSEsbcInit(115200); // initialize ES305 breakout board
DCamuti 0:968adb203327 22 mot_en1.period(0.020);
DCamuti 0:968adb203327 23
DCamuti 0:968adb203327 24 // User initializes data collection w/o MATLAB
DCamuti 0:968adb203327 25 pc.printf("Press 1 to start data collection"); // prompts user
DCamuti 0:968adb203327 26 // pc.scanf("%.0f",&go); // records user input
DCamuti 0:968adb203327 27
DCamuti 0:968adb203327 28 // MATLAB initialized data collection
DCamuti 0:968adb203327 29 pc.scanf("%d,%f,%f",&go,&low,&high);
DCamuti 0:968adb203327 30
DCamuti 0:968adb203327 31 while(go == 1) {
DCamuti 0:968adb203327 32
DCamuti 0:968adb203327 33 Time = 0.0;
DCamuti 0:968adb203327 34 t.reset(); // reset timer object
DCamuti 0:968adb203327 35
DCamuti 0:968adb203327 36 while(Time <= st) {
DCamuti 0:968adb203327 37 t.start(); // starting timer for sample
DCamuti 0:968adb203327 38
DCamuti 0:968adb203327 39 // data collection and processing
DCamuti 0:968adb203327 40 O = LS7366_read_counter(1); // reads from encoder
DCamuti 0:968adb203327 41 OR = O*(2*PI/6500.0); // counts to radians
DCamuti 0:968adb203327 42 omega = (OR-ang_pos_prev)/sp;
DCamuti 0:968adb203327 43 ang_pos_prev = OR;
DCamuti 0:968adb203327 44
DCamuti 0:968adb203327 45 if(Time < 0.1){
DCamuti 0:968adb203327 46 dc = 0.0;
DCamuti 0:968adb203327 47 }
DCamuti 0:968adb203327 48 else if(Time >= 0.1 && Time < 0.55){
DCamuti 0:968adb203327 49 dc = low;
DCamuti 0:968adb203327 50 }
DCamuti 0:968adb203327 51 else{
DCamuti 0:968adb203327 52 dc=high;
DCamuti 0:968adb203327 53 }
DCamuti 0:968adb203327 54
DCamuti 0:968adb203327 55 mot_control(1,dc); // gives the motor a duty cycle
DCamuti 0:968adb203327 56
DCamuti 0:968adb203327 57 pc.printf("%f,%f,%f\n",Time,omega,dc); // sending values to MATLAB
DCamuti 0:968adb203327 58
DCamuti 0:968adb203327 59
DCamuti 0:968adb203327 60 Time = Time + sp; // updates time
DCamuti 0:968adb203327 61
DCamuti 0:968adb203327 62 // wait to achieve sampling rate
DCamuti 0:968adb203327 63 dt = sp - t.read();
DCamuti 0:968adb203327 64 wait(dt);
DCamuti 0:968adb203327 65
DCamuti 0:968adb203327 66 t.reset(); // resets timer for sampling loop
DCamuti 0:968adb203327 67 } // ends sampling loop
DCamuti 0:968adb203327 68 mot_control(1,0.0);
DCamuti 0:968adb203327 69 // repeat data collection if need exists
DCamuti 0:968adb203327 70 // pc.printf("Run data collection again? Press 1. If not, press 0.\n\r");
DCamuti 0:968adb203327 71 pc.scanf("%d,%f,%f",&go,&low,&high);
DCamuti 0:968adb203327 72
DCamuti 0:968adb203327 73 } // ends data collection trigger loop
DCamuti 0:968adb203327 74
DCamuti 0:968adb203327 75 } // ends main