Mirror actuator for RT2 lab

Dependencies:   FastPWM

Committer:
altb2
Date:
Sun May 02 08:55:44 2021 +0000
Revision:
16:28b6bb8a4b7f
Parent:
15:9f32f64eee5b
Final commit 4 students

Who changed what in which revision?

UserRevisionLine numberNew contents of line
altb2 0:a56e9c932891 1 #include "mbed.h"
altb2 0:a56e9c932891 2 #include "math.h"
altb2 15:9f32f64eee5b 3 #include <stdio.h>
altb2 0:a56e9c932891 4 //------------------------------------------
altb2 0:a56e9c932891 5 #define PI 3.1415927f
altb2 0:a56e9c932891 6 //------------------------------------------
altb2 0:a56e9c932891 7 #include "EncoderCounter.h"
altb2 5:768e10f6d372 8 #include "EncoderCounterIndex.h"
altb2 0:a56e9c932891 9 #include "DiffCounter.h"
altb2 0:a56e9c932891 10 #include "IIR_filter.h"
altb2 0:a56e9c932891 11 #include "LinearCharacteristics.h"
altb2 5:768e10f6d372 12 #include "Unwrapper_2pi.h"
altb2 5:768e10f6d372 13 #include "path_1d.h"
altb2 5:768e10f6d372 14 #include "GPA.h"
altb2 5:768e10f6d372 15 #include "ControllerLoop.h"
altb2 6:9ebeffe446e4 16 #include "Mirror_Kinematic.h"
altb2 6:9ebeffe446e4 17 #include "data_structs.h"
altb2 6:9ebeffe446e4 18 #include "uart_comm_thread.h"
altb2 8:49ac75c42da0 19 #include "FastPWM.h"
altb2 0:a56e9c932891 20
altb2 6:9ebeffe446e4 21 static BufferedSerial serial_port(USBTX, USBRX);
altb2 10:bfacffec199a 22 DigitalIn big_button(PC_3); // Enable button an backside
altb2 10:bfacffec199a 23 bool key_was_pressed = false; //
altb2 10:bfacffec199a 24 float Ts=.0002f; // sampling time
altb2 5:768e10f6d372 25 void pressed(void);
altb2 5:768e10f6d372 26 void released(void);
altb2 5:768e10f6d372 27 //------------- DEFINE FILTERS ----------------
altb2 5:768e10f6d372 28 // missing
altb2 6:9ebeffe446e4 29 //------------- Define In/Out -----------------
altb2 7:942fd77d5e19 30 AnalogOut i_des1(PA_5);
altb2 6:9ebeffe446e4 31 AnalogOut i_des2(PA_4);
altb2 8:49ac75c42da0 32 //AnalogIn i_act1(PA_3);
altb2 8:49ac75c42da0 33 //AnalogIn i_act2(PC_0);
altb2 8:49ac75c42da0 34 //FastPWM i_des1(PB_10);
altb2 8:49ac75c42da0 35 //FastPWM i_des2(PA_15);
altb2 6:9ebeffe446e4 36 DigitalOut i_enable(PC_4);
altb2 6:9ebeffe446e4 37 DigitalOut laser_on(PB_0);
altb2 6:9ebeffe446e4 38 ///------------- Encoder -----------------------
altb2 5:768e10f6d372 39 EncoderCounter counter1(PA_6, PC_7); // initialize counter on PA_6 and PC_7
altb2 5:768e10f6d372 40 InterruptIn indexpulse1(PA_8);
altb2 5:768e10f6d372 41 EncoderCounterIndex index1(counter1,indexpulse1);
altb2 6:9ebeffe446e4 42 // ------------------------------------
altb2 5:768e10f6d372 43 EncoderCounter counter2(PB_6, PB_7); // initialize counter on PB_6 and PB_7
altb2 5:768e10f6d372 44 InterruptIn indexpulse2(PB_4);
altb2 5:768e10f6d372 45 EncoderCounterIndex index2(counter2,indexpulse2); // initialize counter on PA_6 and PC_7
altb2 6:9ebeffe446e4 46 // ------------------------------------
altb2 6:9ebeffe446e4 47 DiffCounter diff1(0.0005f,Ts,4000); // discrete differentiate, based on encoder data
altb2 6:9ebeffe446e4 48 DiffCounter diff2(0.0005f,Ts,4000); // discrete differentiate, based on encoder data
altb2 6:9ebeffe446e4 49 //LinearCharacteristics i2pwm(-1.0,1.0,0.02,0.98,.02,.98);
altb2 10:bfacffec199a 50 LinearCharacteristics i2u(-.80,.80,0.0f,1.0f);
altb2 8:49ac75c42da0 51 LinearCharacteristics u2i(0.0,1.0,-1.0,1.0);
altb2 6:9ebeffe446e4 52
altb2 6:9ebeffe446e4 53 Unwrapper_2pi uw2pi1;
altb2 6:9ebeffe446e4 54 Unwrapper_2pi uw2pi2;
altb2 0:a56e9c932891 55 //------------------------------------------
altb2 0:a56e9c932891 56 // ----- User defined functions -----------
altb2 10:bfacffec199a 57 ControllerLoop loop(Ts); // this is forthe main controller loop
altb2 15:9f32f64eee5b 58 uart_comm_thread uart_com(&serial_port,.025f); // this is the communication thread
altb2 10:bfacffec199a 59 Timer glob_ti; // the global timer
altb2 10:bfacffec199a 60 path_1d p1; // pathplanner (under constr.)
altb2 10:bfacffec199a 61 path_1d p2; // pathplanner (under constr.)
altb2 5:768e10f6d372 62 path_1d *current_path;
altb2 10:bfacffec199a 63 // --------- GPA -----------------------------
altb2 10:bfacffec199a 64 //init values: f0, f1, nbPts, A0, A1, Ts
altb2 16:28b6bb8a4b7f 65 GPA myGPA(5 , 2450, 40, 45, 35, Ts);
altb2 10:bfacffec199a 66 //------------------------------------------------------------------------------
altb2 10:bfacffec199a 67 // --------- Mirror kinematik, define values, trafos etc there
altb2 7:942fd77d5e19 68 Mirror_Kinematic mk;
altb2 10:bfacffec199a 69 //------------------------------------------------------------------------------
altb2 10:bfacffec199a 70 // --------- data: overall data structure for x-change
altb2 6:9ebeffe446e4 71 DATA_Xchange data;
altb2 6:9ebeffe446e4 72
altb2 0:a56e9c932891 73 //******************************************************************************
altb2 0:a56e9c932891 74 //---------- main loop -------------
altb2 0:a56e9c932891 75 //******************************************************************************
altb2 0:a56e9c932891 76 int main()
altb2 0:a56e9c932891 77 {
altb2 6:9ebeffe446e4 78 serial_port.set_baud(115200);
altb2 6:9ebeffe446e4 79 serial_port.set_format(
altb2 6:9ebeffe446e4 80 /* bits */ 8,
altb2 6:9ebeffe446e4 81 /* parity */ BufferedSerial::None,
altb2 6:9ebeffe446e4 82 /* stop bit */ 1
altb2 6:9ebeffe446e4 83 );
altb2 6:9ebeffe446e4 84 serial_port.set_blocking(false); // force to send whenever possible and data is there
altb2 10:bfacffec199a 85 i_enable = 0; // disable current first
altb2 0:a56e9c932891 86 counter1.reset(); // encoder reset
altb2 5:768e10f6d372 87 counter2.reset(); // encoder reset
altb2 15:9f32f64eee5b 88 mk.set_offsets(328,8); // individal set values for global position
altb2 5:768e10f6d372 89 glob_ti.start();
altb2 5:768e10f6d372 90 glob_ti.reset();
altb2 15:9f32f64eee5b 91 //loop.init_controllers();
altb2 10:bfacffec199a 92 uart_com.start_uart();
altb2 10:bfacffec199a 93 loop.start_loop();
altb2 7:942fd77d5e19 94 i_des1.write(i2u(0));
altb2 6:9ebeffe446e4 95 i_des2.write(i2u(0));
altb2 8:49ac75c42da0 96 ThisThread::sleep_for(200);
altb2 8:49ac75c42da0 97 uart_com.send_text((char *)"Start Mirroractuator 1.0");
altb2 6:9ebeffe446e4 98 /* p1.initialize(300,10,A,0,0,0);
altb2 10:bfacffec199a 99 p2.initialize(300,10,-A,0,0,A);*/
altb2 6:9ebeffe446e4 100 laser_on = 0;
altb2 6:9ebeffe446e4 101 //for(int wk =0;wk<5;wk++)
altb2 6:9ebeffe446e4 102 while(0)
altb2 5:768e10f6d372 103 {
altb2 6:9ebeffe446e4 104 short c1 = counter1; // get counts from Encoder
altb2 6:9ebeffe446e4 105 short c2 = counter2; // get counts from Encoder
altb2 10:bfacffec199a 106 current_path = &p1;
altb2 5:768e10f6d372 107 current_path->start(glob_ti.read());
altb2 5:768e10f6d372 108 while(!current_path->finished)
altb2 11:d43f8b421d6d 109 ThisThread::sleep_for(100);
altb2 5:768e10f6d372 110 current_path = &p2;
altb2 5:768e10f6d372 111 current_path->start(glob_ti.read());
altb2 5:768e10f6d372 112 while(!current_path->finished)
altb2 11:d43f8b421d6d 113 ThisThread::sleep_for(100);
altb2 5:768e10f6d372 114 ThisThread::sleep_for(100);
altb2 11:d43f8b421d6d 115 laser_on = !laser_on;
altb2 10:bfacffec199a 116 } // end of while(..)
altb2 10:bfacffec199a 117 i_enable = 0;
altb2 10:bfacffec199a 118 while(1)
altb2 10:bfacffec199a 119 {
altb2 15:9f32f64eee5b 120 ThisThread::sleep_for(100);
altb2 5:768e10f6d372 121 }
altb2 0:a56e9c932891 122 } // END OF main