YP Roy / Mbed 2 deprecated Nucleo_sg90_remote_control Featured

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //247-426-Nucleo_Lab_7
00002 //This software is derived from Nucleo_pwm_servo_sg90.
00003 //It outputs a PWM signal that is compatible with Tower Pro SG90 servos.
00004 //It makes the servo move from one programmed position to another each time the
00005 //user button is pressed.
00006 //The programmed positions are defined by the values that are stored in
00007 //the variable "pulseDurationInMicroSeconds". 
00008 //An index counter is used to select which value is to be used to set the pulse
00009 //duration of the PWM signal that controls that servo.
00010 //The value of this index counter is changed each time the user button is
00011 //pressed and the changes are made in a way that makes the program repeatedly
00012 //cycles through all the possible positions.
00013 //It is just necessary to add values to "pulseDurationInMicroSeconds" to have
00014 //the servo adopt more positions.
00015 
00016 //Just connect the brown wire of a SG90 servo to GND, its red wire to AVDD and
00017 //its the orange wire to D11 to have the SMT32-F401RE control the servo.
00018 //
00019 
00020 //Remote control of a SG90 servo is also possible if the Nucleo board is allowed
00021 //to control an infrared LED as an infrared receptor is connected to the servo.
00022 //
00023 //material:
00024 //      Infrared LED = LTE-5802A, Receiver = RPM7140-V4R
00025 //
00026 //signals:
00027 //      D11= pwm signal to use to control SG90 directly
00028 //      D10= !D11 = signal to use to control the transmitter's LED
00029 //      D9 = 40kHz signal to use to modulate the LED output
00030 //
00031 //
00032 //Transmitter: (powered by Nucleo's supply)
00033 //   +5 ---/\/\/\----------- |
00034 //           180             |
00035 //                           |_
00036 //                           \/  LTE-5802A
00037 //                           --
00038 //                     ______|______
00039 //                   |/             \|
00040 //  D10---/\/\/\-----|               |-----/\/\/\----- D9
00041 //  (!pwm)   10k      |\ <-emitter-> /|       10k      (40kHz)
00042 //                    |             |
00043 //                   GND           GND
00044 //
00045 //  GND of Nucleo board ------ GND
00046 //
00047 //Receiver: (powered by remote supply)
00048 //
00049 //                                                             _____
00050 //                                                             | O | RPM7140-V4R
00051 //                                      +5---/\/\/\----        ----- (front view)
00052 //                                            10k     |        | | |
00053 //                                                    |        | | |______+5v
00054 //      Orange wire of servo SG90 --------------------.--      | |________GND 
00055 //      Red wire of servo SG90 -----+5V                  \|    | 
00056 //      Brown wire of servo SG90 ---GND                   |-----
00057 //                                              emitter  /|
00058 //                                                       |
00059 //                                                      GND
00060 
00061 #include "mbed.h"
00062 #define NUMBER_OF_POSITIONS sizeof(pulseDurationInMicroSeconds)/sizeof(int)
00063 #define PWM_PERIOD_FOR_SG90_IN_MS           20
00064 #define PWM_PERIOD_FOR_MODULATION_IN_US     25  
00065 DigitalOut userLED(LED1);
00066 PwmOut towerProSG90(D11);
00067 PwmOut remoteControlOutput(D10);
00068 PwmOut modulatingOutput(D9);
00069 InterruptIn userButton(USER_BUTTON);
00070 int index;
00071 int pulseDurationInMicroSeconds[]=
00072         {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375};    
00073 void responseToUserButtonPressed(void)
00074 {
00075     index++;
00076     if (index >= NUMBER_OF_POSITIONS)
00077     {
00078         index = 0;
00079     }
00080     towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);    
00081     remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
00082 }
00083 
00084 int main()
00085 {
00086     index = 0;    
00087     towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
00088     towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
00089     remoteControlOutput.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
00090     remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
00091     modulatingOutput.period_us(PWM_PERIOD_FOR_MODULATION_IN_US);
00092     modulatingOutput.pulsewidth_us(PWM_PERIOD_FOR_MODULATION_IN_US/2);
00093     userButton.fall(&responseToUserButtonPressed);
00094       
00095     while(1)
00096     {
00097         userLED = !userLED;
00098         wait(1);
00099     }
00100 }