Simulation of flight pitch control with servo sweep.

Dependencies:   mbed Servo mbed-rtos Motor

Committer:
4180_1
Date:
Sun Sep 22 17:57:46 2013 +0000
Revision:
4:79863d2ea5a0
Parent:
3:3ec443c0842a
Child:
5:8db82a61e052
ver 1.0 see https://mbed.org/users/4180_1/notebook/mbed-application-board-hands-on-demos/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 2:a69c8c5f5b03 1 // example to test the mbed Lab Board lcd lib with the mbed rtos
4180_1 4:79863d2ea5a0 2 // Pot1 changes the contrast
4180_1 4:79863d2ea5a0 3 // Pot2 changes the speed of the sin wave
dreschpe 1:1c6a9eaf55b5 4
dreschpe 0:f6a57b843f79 5 #include "mbed.h"
dreschpe 0:f6a57b843f79 6 #include "rtos.h"
dreschpe 0:f6a57b843f79 7 #include "Small_6.h"
dreschpe 0:f6a57b843f79 8 #include "Small_7.h"
dreschpe 0:f6a57b843f79 9 #include "Arial_9.h"
dreschpe 0:f6a57b843f79 10 #include "stdio.h"
dreschpe 1:1c6a9eaf55b5 11 #include "C12832_lcd.h"
dreschpe 0:f6a57b843f79 12
4180_1 4:79863d2ea5a0 13
dreschpe 3:3ec443c0842a 14 C12832_LCD LCD;
dreschpe 1:1c6a9eaf55b5 15 AnalogIn Pot1(p19);
dreschpe 1:1c6a9eaf55b5 16 AnalogIn Pot2(p20);
4180_1 4:79863d2ea5a0 17 PwmOut Speaker(p26);
4180_1 4:79863d2ea5a0 18 PwmOut RGBLED_r(p23);
4180_1 4:79863d2ea5a0 19 PwmOut RGBLED_g(p24);
4180_1 4:79863d2ea5a0 20 PwmOut RGBLED_b(p25);
4180_1 4:79863d2ea5a0 21 DigitalIn joyfire(p14);
4180_1 4:79863d2ea5a0 22 BusIn joy(p15,p12,p13,p16);
4180_1 4:79863d2ea5a0 23 BusOut leds(LED1,LED2,LED3,LED4);
dreschpe 2:a69c8c5f5b03 24
4180_1 4:79863d2ea5a0 25 // mutex to make the lcd lib thread safe
dreschpe 1:1c6a9eaf55b5 26 Mutex lcd_mutex;
dreschpe 0:f6a57b843f79 27
dreschpe 2:a69c8c5f5b03 28 // Thread 1
dreschpe 2:a69c8c5f5b03 29 // print counter into first line and wait for 1 s
dreschpe 0:f6a57b843f79 30 void thread1(void const *args)
dreschpe 0:f6a57b843f79 31 {
dreschpe 0:f6a57b843f79 32 int i;
dreschpe 0:f6a57b843f79 33 while(true) { // thread loop
dreschpe 2:a69c8c5f5b03 34 lcd_mutex.lock();
dreschpe 0:f6a57b843f79 35 LCD.locate(0,0);
dreschpe 0:f6a57b843f79 36 LCD.set_font((unsigned char*) Small_6);
dreschpe 3:3ec443c0842a 37 LCD.printf("Thread1 count: %d",i);
dreschpe 2:a69c8c5f5b03 38 lcd_mutex.unlock();
dreschpe 0:f6a57b843f79 39 i++;
dreschpe 0:f6a57b843f79 40 Thread::wait(1000);
dreschpe 0:f6a57b843f79 41 }
dreschpe 0:f6a57b843f79 42 }
dreschpe 0:f6a57b843f79 43
dreschpe 2:a69c8c5f5b03 44 // Thread 2
dreschpe 2:a69c8c5f5b03 45 // print counter into third line and wait for 0,5s
dreschpe 0:f6a57b843f79 46 void thread2(void const *args)
dreschpe 0:f6a57b843f79 47 {
dreschpe 1:1c6a9eaf55b5 48 int k;
dreschpe 0:f6a57b843f79 49 while(true) { // thread loop
dreschpe 2:a69c8c5f5b03 50 lcd_mutex.lock();
dreschpe 0:f6a57b843f79 51 LCD.locate(0,20);
dreschpe 0:f6a57b843f79 52 LCD.set_font((unsigned char*) Arial_9);
dreschpe 3:3ec443c0842a 53 LCD.printf("Thread 2 count : %d",k);
dreschpe 2:a69c8c5f5b03 54 lcd_mutex.unlock();
dreschpe 1:1c6a9eaf55b5 55 k++;
dreschpe 2:a69c8c5f5b03 56 Thread::wait(500); // wait 0.5s
dreschpe 0:f6a57b843f79 57 }
dreschpe 0:f6a57b843f79 58 }
dreschpe 0:f6a57b843f79 59
dreschpe 2:a69c8c5f5b03 60 // Thread 3
dreschpe 2:a69c8c5f5b03 61 // print a sin function in a small window
4180_1 4:79863d2ea5a0 62 // the value of pot 1 changes the speed of the sine wave
dreschpe 2:a69c8c5f5b03 63 void thread3(void const *args)
dreschpe 1:1c6a9eaf55b5 64 {
dreschpe 1:1c6a9eaf55b5 65 int i,k,v;
dreschpe 1:1c6a9eaf55b5 66 double s,a;
dreschpe 1:1c6a9eaf55b5 67 k = 1;
dreschpe 2:a69c8c5f5b03 68 lcd_mutex.lock();
dreschpe 1:1c6a9eaf55b5 69 LCD.rect(89,0,127,17,1);
dreschpe 2:a69c8c5f5b03 70 lcd_mutex.unlock();
dreschpe 1:1c6a9eaf55b5 71 while(true) { // thread loop
dreschpe 1:1c6a9eaf55b5 72 v = Pot1.read_u16(); // get value of pot 1
dreschpe 2:a69c8c5f5b03 73 lcd_mutex.lock();
4180_1 4:79863d2ea5a0 74 for (i=90; i<127; i++) {
4180_1 4:79863d2ea5a0 75 s = 8 * sin((long double)(i+k) /5); // pixel to print
dreschpe 2:a69c8c5f5b03 76 a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase
4180_1 4:79863d2ea5a0 77 LCD.pixel(i,9 + (int)a ,0); // erase pixel
4180_1 4:79863d2ea5a0 78 LCD.pixel(i,9 + (int)s ,1); // print pixel
dreschpe 1:1c6a9eaf55b5 79 }
4180_1 4:79863d2ea5a0 80 LCD.copy_to_lcd(); // LCD.pixel does not update the lcd
dreschpe 2:a69c8c5f5b03 81 lcd_mutex.unlock();
dreschpe 1:1c6a9eaf55b5 82 k++;
dreschpe 2:a69c8c5f5b03 83 Thread::wait(v/100); // value of pot1 / 100
dreschpe 1:1c6a9eaf55b5 84 }
dreschpe 1:1c6a9eaf55b5 85 }
dreschpe 0:f6a57b843f79 86
dreschpe 2:a69c8c5f5b03 87 // Thread 4
4180_1 4:79863d2ea5a0 88 // input pot 2 and change the contrast of LCD
dreschpe 2:a69c8c5f5b03 89 void thread4(void const *args)
dreschpe 1:1c6a9eaf55b5 90 {
dreschpe 1:1c6a9eaf55b5 91 int k;
dreschpe 1:1c6a9eaf55b5 92 while(true) { // thread loop
4180_1 4:79863d2ea5a0 93 k = Pot2.read_u16(); // get the value of poti 2
4180_1 4:79863d2ea5a0 94 k = k >> 10; // need only 6 bits for contrast
4180_1 4:79863d2ea5a0 95 lcd_mutex.lock();
4180_1 4:79863d2ea5a0 96 LCD.set_contrast(k);
4180_1 4:79863d2ea5a0 97 lcd_mutex.unlock();
4180_1 4:79863d2ea5a0 98 Thread::wait(500); // wait 0.5s
4180_1 4:79863d2ea5a0 99 }
4180_1 4:79863d2ea5a0 100 }
4180_1 4:79863d2ea5a0 101 // Thread 5
4180_1 4:79863d2ea5a0 102 // RGB LED
4180_1 4:79863d2ea5a0 103 void thread5(void const *args)
4180_1 4:79863d2ea5a0 104 {
4180_1 4:79863d2ea5a0 105 while(true) { // thread loop
4180_1 4:79863d2ea5a0 106 RGBLED_r = 0.5 + (rand() % 11)/20.0;
4180_1 4:79863d2ea5a0 107 RGBLED_g = 0.5 + (rand() % 11)/20.0;
4180_1 4:79863d2ea5a0 108 RGBLED_b = 0.5 + (rand() % 11)/20.0;
4180_1 4:79863d2ea5a0 109 Thread::wait(1667); // wait 1.5s
4180_1 4:79863d2ea5a0 110 }
dreschpe 1:1c6a9eaf55b5 111 }
4180_1 4:79863d2ea5a0 112 // Thread 6
4180_1 4:79863d2ea5a0 113 // Speaker
4180_1 4:79863d2ea5a0 114 void thread6(void const *args)
4180_1 4:79863d2ea5a0 115 {
4180_1 4:79863d2ea5a0 116 while(true) { // thread loop
4180_1 4:79863d2ea5a0 117 Speaker.period(1.0/800.0);
4180_1 4:79863d2ea5a0 118 Speaker = 0.01;
4180_1 4:79863d2ea5a0 119 Thread::wait(1000); // wait 1.0s
4180_1 4:79863d2ea5a0 120 Speaker.period(1.0/969.0);
4180_1 4:79863d2ea5a0 121 Speaker = 0.01;
4180_1 4:79863d2ea5a0 122 Thread::wait(1000); // wait 1.0s
4180_1 4:79863d2ea5a0 123 }
4180_1 4:79863d2ea5a0 124 }
4180_1 4:79863d2ea5a0 125
4180_1 4:79863d2ea5a0 126 // Thread 7
4180_1 4:79863d2ea5a0 127 // Joystick controls onboard mbed LEDs
4180_1 4:79863d2ea5a0 128 void thread7(void const *args)
4180_1 4:79863d2ea5a0 129 {
4180_1 4:79863d2ea5a0 130 while(true) { // thread loop
4180_1 4:79863d2ea5a0 131 if (joyfire) {
4180_1 4:79863d2ea5a0 132 leds = 0xf;
4180_1 4:79863d2ea5a0 133 } else {
4180_1 4:79863d2ea5a0 134 leds = joy;
4180_1 4:79863d2ea5a0 135 }
4180_1 4:79863d2ea5a0 136 Thread::wait(200); // wait 0.25s
4180_1 4:79863d2ea5a0 137 }
dreschpe 1:1c6a9eaf55b5 138 }
dreschpe 1:1c6a9eaf55b5 139
dreschpe 2:a69c8c5f5b03 140
4180_1 4:79863d2ea5a0 141
dreschpe 0:f6a57b843f79 142 int main()
dreschpe 0:f6a57b843f79 143 {
dreschpe 0:f6a57b843f79 144 int j;
dreschpe 2:a69c8c5f5b03 145 LCD.cls();
4180_1 4:79863d2ea5a0 146
dreschpe 2:a69c8c5f5b03 147 Thread t1(thread1); //start thread1
dreschpe 2:a69c8c5f5b03 148 Thread t2(thread2); //start thread2
dreschpe 2:a69c8c5f5b03 149 Thread t3(thread3); //start thread3
dreschpe 2:a69c8c5f5b03 150 Thread t4(thread4); //start thread4
4180_1 4:79863d2ea5a0 151 Thread t5(thread5); //start thread5
4180_1 4:79863d2ea5a0 152 Thread t6(thread6); //start thread6
4180_1 4:79863d2ea5a0 153 Thread t7(thread7); //start thread7
dreschpe 0:f6a57b843f79 154
dreschpe 2:a69c8c5f5b03 155 while(true) { // main is the next thread
dreschpe 2:a69c8c5f5b03 156 lcd_mutex.lock();
dreschpe 1:1c6a9eaf55b5 157 LCD.locate(0,9);
dreschpe 0:f6a57b843f79 158 LCD.set_font((unsigned char*) Small_7);
dreschpe 2:a69c8c5f5b03 159 j = LCD.get_contrast(); // read the actual contrast
dreschpe 3:3ec443c0842a 160 LCD.printf("contrast : %d",j);
dreschpe 2:a69c8c5f5b03 161 lcd_mutex.unlock();
dreschpe 2:a69c8c5f5b03 162 Thread::wait(500); // wait 0.5s
dreschpe 0:f6a57b843f79 163 }
dreschpe 0:f6a57b843f79 164 }