This program uses SeeedArch to control the two servo motors of a small sailboat. One is general servo (rudder), and the other one is the multi-turn servo.
main.cpp
00001 #include "mbed.h" 00002 #include "Servo.h" 00003 00004 //DigitalOut led1(LED1); 00005 //DigitalOut led2(LED2); 00006 //DigitalOut led3(LED3); 00007 //DigitalOut led4(LED4); 00008 00009 /* 00010 The arch cookbook in mbed.org website 00011 http://mbed.org/users/viswesr/notebook/arch-cookbook/ 00012 */ 00013 PwmOut servo(P1_24); //seeedarch pin for the multiturn servo 00014 Servo rudder(P1_25); //seeedarch pin for the rudder servo 00015 00016 Serial bbb(P1_18,P1_17);//seeedarch pin for the UART communication tx, rx 00017 00018 00019 AnalogIn ain(P0_11);//seeedarch analog in pin 00020 00021 void servo_initial(void); 00022 void multiturn_servo(int type, int circle); 00023 00024 int main() 00025 { 00026 int circle=5; 00027 00028 //servo_initial(); 00029 bbb.baud(115200); 00030 while(~bbb.writeable()); 00031 bbb.putc('s'); 00032 while(1) 00033 { 00034 if(bbb.readable()) { 00035 bbb.putc(bbb.getc()+1); 00036 } 00037 00038 } 00039 00040 while(1) 00041 { 00042 //Multiturn servo 00043 multiturn_servo(2,1);//20+10 degree 00044 multiturn_servo(0,10); 00045 multiturn_servo(2,1);//20+20 degree 00046 multiturn_servo(0,10); 00047 multiturn_servo(2,1);//50 00048 multiturn_servo(0,10); 00049 multiturn_servo(2,1);//60 00050 multiturn_servo(0,10); 00051 multiturn_servo(2,1);//70 00052 multiturn_servo(0,10); 00053 multiturn_servo(2,1);//80 00054 multiturn_servo(0,10); 00055 00056 00057 //multiturn_servo(1,1); 00058 multiturn_servo(1,2); 00059 multiturn_servo(0,10); 00060 multiturn_servo(1,2); 00061 multiturn_servo(0,10); 00062 multiturn_servo(1,2); 00063 multiturn_servo(0,10); 00064 multiturn_servo(1,2); 00065 multiturn_servo(0,10); 00066 multiturn_servo(1,4); 00067 multiturn_servo(0,10); 00068 multiturn_servo(1,5); 00069 multiturn_servo(0,10); 00070 wait(2); 00071 00072 //Rudder servo 00073 rudder.write(0.0); 00074 wait(0.01); 00075 wait(2); 00076 rudder.write(0.5); 00077 wait(0.01); 00078 wait(2); 00079 rudder.write(1.0); 00080 wait(0.01); 00081 wait(2); 00082 } 00083 } 00084 00085 void servo_initial() 00086 { 00087 servo.period_ms(22); //multiturn servo requires a 22ms period 00088 //Initial the position to far end 00089 multiturn_servo(1,10); 00090 multiturn_servo(0,20); 00091 } 00092 00093 /* 00094 Control the multiturn servo to turn or stop it 00095 To turn the servo clockwise use pulsewidth 1.7 ms 00096 To turn the servo counter clockwise use pulsewidth 2.0 ms 00097 To stop the servo use the pulsewidth 10 ms 00098 */ 00099 void multiturn_servo(int type, int circle) 00100 { 00101 float pulse_width=0.0; 00102 int i; 00103 00104 switch(type) 00105 { 00106 case 0: 00107 pulse_width=10.0;//stop 00108 break; 00109 case 1: 00110 pulse_width=1.6;//counter clockwise 00111 break; 00112 case 2: 00113 pulse_width=2.0;//clockwise 00114 break; 00115 } 00116 for(i=0;i<circle;i++) 00117 { 00118 servo.pulsewidth_ms(pulse_width); // servo position determined by a pulsewidth between 1-2ms 00119 wait(0.25); 00120 } 00121 00122 }
Generated on Fri Jul 29 2022 00:34:39 by
1.7.2