
FRDM_wahadlo_torsyjne
Dependencies: mbed BufferedSerial
Fork of FRDM_wahadlo_torsyjne by
main.cpp@10:497e9a0da7d6, 2014-06-12 (annotated)
- Committer:
- wojtekmir
- Date:
- Thu Jun 12 17:30:39 2014 +0000
- Revision:
- 10:497e9a0da7d6
- Parent:
- 9:3540ad710232
- Child:
- 11:b3bd4b86f914
Pomiar na jednym przerwaniu, sterowanie silnika na drugim. Nie potrafi? zrobi? pocz?tkowej ?wiartki obrotu (trzeba ustawi? krzywk? silnika pod k?tem 45 stopni). Wysy?anie danych co 10ms, silnik nie gubi krok?w, p?tla nie zawiesza si?.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wojtekmir | 1:a4f9276a0f35 | 1 | #include "mbed.h" |
Pawel_13 | 8:434d613f0929 | 2 | #include "TSISensor.h" |
Pawel_13 | 9:3540ad710232 | 3 | #include "Timer.h" |
wojtekmir | 1:a4f9276a0f35 | 4 | |
wojtekmir | 10:497e9a0da7d6 | 5 | Ticker triger1; |
wojtekmir | 10:497e9a0da7d6 | 6 | Ticker triger2; |
wojtekmir | 10:497e9a0da7d6 | 7 | |
Pawel_13 | 9:3540ad710232 | 8 | int motor_PWM_period_us = 200; //okres sygnału PWM (w mikrosekundach) wyzwalającego kolejny krok silnika |
wojtekmir | 10:497e9a0da7d6 | 9 | int i_start = 0; |
wojtekmir | 10:497e9a0da7d6 | 10 | int EXC_angle = 3200/4; //360 stopni to 3200 krok. przy mkrok. = 1.8/16 |
wojtekmir | 10:497e9a0da7d6 | 11 | float switch_dir_time=(float)((float)EXC_angle/500.0)*((float)motor_PWM_period_us/1000.0); |
wojtekmir | 10:497e9a0da7d6 | 12 | int DIR_switched = 0; |
wojtekmir | 10:497e9a0da7d6 | 13 | int SWITCH_DIR = EXC_angle; |
wojtekmir | 10:497e9a0da7d6 | 14 | |
Pawel_13 | 8:434d613f0929 | 15 | DigitalOut direction(PTC7); //zmiana wartości logicznej na wyjściu PTC7 powoduje zmianę kieruneku obrotów |
Pawel_13 | 8:434d613f0929 | 16 | DigitalOut myled(LED_GREEN); //dioda zielona sygnalizuje zmianę kierunku obrotów |
Pawel_13 | 8:434d613f0929 | 17 | |
Pawel_13 | 8:434d613f0929 | 18 | AnalogIn analog_IN_0(PTB0); //odczyt napięć z wejść analogowych (kąty obrotu) |
Pawel_13 | 8:434d613f0929 | 19 | AnalogIn analog_IN_1(PTB1); |
Pawel_13 | 3:2ba53b9a499a | 20 | |
wojtekmir | 2:78a032749524 | 21 | PwmOut motor(PTA5); //sygnał PWM do sterowania sterownikiem silnika krokowego |
Pawel_13 | 8:434d613f0929 | 22 | Serial pc(USBTX,USBRX); //komunikacja z PC |
wojtekmir | 1:a4f9276a0f35 | 23 | |
Pawel_13 | 8:434d613f0929 | 24 | TSISensor tsi; |
Pawel_13 | 8:434d613f0929 | 25 | |
Pawel_13 | 9:3540ad710232 | 26 | Timer timer; |
Pawel_13 | 9:3540ad710232 | 27 | |
Pawel_13 | 8:434d613f0929 | 28 | float get_voltage_in(AnalogIn analog_in, float zero_analog_in) { |
Pawel_13 | 8:434d613f0929 | 29 | return (analog_in.read() - zero_analog_in) * 2.9035; // przesunięcie do zera i skalowanie napiecia do zakresu |
Pawel_13 | 3:2ba53b9a499a | 30 | } |
wojtekmir | 10:497e9a0da7d6 | 31 | void task1() { |
wojtekmir | 10:497e9a0da7d6 | 32 | //i_start += 1;1 |
wojtekmir | 10:497e9a0da7d6 | 33 | //if (i_start == SWITCH_DIR) { |
wojtekmir | 10:497e9a0da7d6 | 34 | // i_start = 0; |
wojtekmir | 10:497e9a0da7d6 | 35 | myled = !myled; |
wojtekmir | 10:497e9a0da7d6 | 36 | direction = !direction; |
wojtekmir | 10:497e9a0da7d6 | 37 | /*if (DIR_switched == 0) { |
wojtekmir | 10:497e9a0da7d6 | 38 | if (SWITCH_DIR == EXC_angle) { |
wojtekmir | 10:497e9a0da7d6 | 39 | SWITCH_DIR = 2*EXC_angle; |
wojtekmir | 10:497e9a0da7d6 | 40 | DIR_switched = 1; |
wojtekmir | 10:497e9a0da7d6 | 41 | } |
wojtekmir | 10:497e9a0da7d6 | 42 | }*/ |
wojtekmir | 10:497e9a0da7d6 | 43 | //} |
wojtekmir | 10:497e9a0da7d6 | 44 | } |
Pawel_13 | 3:2ba53b9a499a | 45 | |
wojtekmir | 10:497e9a0da7d6 | 46 | void task2() { |
wojtekmir | 10:497e9a0da7d6 | 47 | //wyslij dane |
wojtekmir | 10:497e9a0da7d6 | 48 | int zero_analog_IN_0 = analog_IN_0.read(); |
wojtekmir | 10:497e9a0da7d6 | 49 | int zero_analog_IN_1 = analog_IN_1.read(); |
wojtekmir | 10:497e9a0da7d6 | 50 | |
wojtekmir | 10:497e9a0da7d6 | 51 | get_voltage_in(analog_IN_0, zero_analog_IN_0); |
wojtekmir | 10:497e9a0da7d6 | 52 | pc.printf("%.3f\t\n", get_voltage_in(analog_IN_0, zero_analog_IN_0)); |
wojtekmir | 10:497e9a0da7d6 | 53 | } |
Pawel_13 | 8:434d613f0929 | 54 | |
Pawel_13 | 8:434d613f0929 | 55 | int main() { |
Pawel_13 | 6:9414a292d7f2 | 56 | motor.period_us(motor_PWM_period_us); |
Pawel_13 | 9:3540ad710232 | 57 | motor = 0.0; // wypełnienie PWM sygnału CLK |
wojtekmir | 10:497e9a0da7d6 | 58 | pc.baud(115200); // największa szybkość standardowa, jaką można zadać to 19200 |
wojtekmir | 10:497e9a0da7d6 | 59 | |
wojtekmir | 10:497e9a0da7d6 | 60 | wait(5); //czas na ustawienia początkowe 15 sek. |
wojtekmir | 10:497e9a0da7d6 | 61 | |
wojtekmir | 10:497e9a0da7d6 | 62 | triger2.attach(&task2, 0.02); //1/1000000); |
wojtekmir | 10:497e9a0da7d6 | 63 | |
Pawel_13 | 8:434d613f0929 | 64 | |
wojtekmir | 10:497e9a0da7d6 | 65 | motor = 0.5; |
wojtekmir | 1:a4f9276a0f35 | 66 | |
wojtekmir | 10:497e9a0da7d6 | 67 | //wait(switch_dir_time/2); //tu jest teoretyczna ćwiartka obrotu |
wojtekmir | 10:497e9a0da7d6 | 68 | //direction = !direction; |
Pawel_13 | 8:434d613f0929 | 69 | |
wojtekmir | 10:497e9a0da7d6 | 70 | triger1.attach(&task1, switch_dir_time); //motor_PWM_period_us/1000000); |
wojtekmir | 10:497e9a0da7d6 | 71 | |
wojtekmir | 10:497e9a0da7d6 | 72 | |
Pawel_13 | 8:434d613f0929 | 73 | while (true) { |
wojtekmir | 10:497e9a0da7d6 | 74 | // t0 = timer.read_us(); |
wojtekmir | 10:497e9a0da7d6 | 75 | //if (tsi.readPercentage() < 0.5) |
wojtekmir | 10:497e9a0da7d6 | 76 | |
wojtekmir | 10:497e9a0da7d6 | 77 | //else motor = 0.0; // silnik zatrzyma się po dotknięciu prawej strony potencjometru |
wojtekmir | 10:497e9a0da7d6 | 78 | wait(10); // "nic nierobienie, żeby się nie zawiesił" |
Pawel_13 | 3:2ba53b9a499a | 79 | |
wojtekmir | 10:497e9a0da7d6 | 80 | //t1 = timer.read_us(); |
wojtekmir | 10:497e9a0da7d6 | 81 | //t0_t1 = t1 - t0; |
wojtekmir | 10:497e9a0da7d6 | 82 | //wait_us(motor_PWM_period_us); |
wojtekmir | 1:a4f9276a0f35 | 83 | } |
wojtekmir | 10:497e9a0da7d6 | 84 | } |
wojtekmir | 10:497e9a0da7d6 | 85 |