Software that allows basic remote control of the position of a servo (schematic included in the comments)

Dependencies:   mbed

Committer:
YPROY
Date:
Thu Mar 09 01:16:43 2017 +0000
Revision:
4:85a8391945b2
Parent:
3:91070e0c0431
Code for basic remote control of a servo
; (schematic included in comments)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YPROY 4:85a8391945b2 1 //247-426-Nucleo_Lab_7
YPROY 4:85a8391945b2 2 //This software is derived from Nucleo_pwm_servo_sg90.
YPROY 1:df2cee74ab8b 3 //It outputs a PWM signal that is compatible with Tower Pro SG90 servos.
YPROY 1:df2cee74ab8b 4 //It makes the servo move from one programmed position to another each time the
YPROY 1:df2cee74ab8b 5 //user button is pressed.
YPROY 1:df2cee74ab8b 6 //The programmed positions are defined by the values that are stored in
YPROY 1:df2cee74ab8b 7 //the variable "pulseDurationInMicroSeconds".
YPROY 1:df2cee74ab8b 8 //An index counter is used to select which value is to be used to set the pulse
YPROY 1:df2cee74ab8b 9 //duration of the PWM signal that controls that servo.
YPROY 1:df2cee74ab8b 10 //The value of this index counter is changed each time the user button is
YPROY 1:df2cee74ab8b 11 //pressed and the changes are made in a way that makes the program repeatedly
YPROY 1:df2cee74ab8b 12 //cycles through all the possible positions.
YPROY 1:df2cee74ab8b 13 //It is just necessary to add values to "pulseDurationInMicroSeconds" to have
YPROY 1:df2cee74ab8b 14 //the servo adopt more positions.
YPROY 1:df2cee74ab8b 15
YPROY 1:df2cee74ab8b 16 //Just connect the brown wire of a SG90 servo to GND, its red wire to AVDD and
YPROY 2:7aacbdf39425 17 //its the orange wire to D11 to have the SMT32-F401RE control the servo.
YPROY 4:85a8391945b2 18 //
YPROY 4:85a8391945b2 19
YPROY 4:85a8391945b2 20 //Remote control of a SG90 servo is also possible if the Nucleo board is allowed
YPROY 4:85a8391945b2 21 //to control an infrared LED as an infrared receptor is connected to the servo.
YPROY 4:85a8391945b2 22 //
YPROY 4:85a8391945b2 23 //material:
YPROY 4:85a8391945b2 24 // Infrared LED = LTE-5802A, Receiver = RPM7140-V4R
YPROY 4:85a8391945b2 25 //
YPROY 4:85a8391945b2 26 //signals:
YPROY 4:85a8391945b2 27 // D11= pwm signal to use to control SG90 directly
YPROY 4:85a8391945b2 28 // D10= !D11 = signal to use to control the transmitter's LED
YPROY 4:85a8391945b2 29 // D9 = 40kHz signal to use to modulate the LED output
YPROY 4:85a8391945b2 30 //
YPROY 4:85a8391945b2 31 //
YPROY 4:85a8391945b2 32 //Transmitter: (powered by Nucleo's supply)
YPROY 4:85a8391945b2 33 // +5 ---/\/\/\----------- |
YPROY 4:85a8391945b2 34 // 180 |
YPROY 4:85a8391945b2 35 // |_
YPROY 4:85a8391945b2 36 // \/ LTE-5802A
YPROY 4:85a8391945b2 37 // --
YPROY 4:85a8391945b2 38 // ______|______
YPROY 4:85a8391945b2 39 // |/ \|
YPROY 4:85a8391945b2 40 // D10---/\/\/\-----| |-----/\/\/\----- D9
YPROY 4:85a8391945b2 41 // (!pwm) 10k |\ <-emitter-> /| 10k (40kHz)
YPROY 4:85a8391945b2 42 // | |
YPROY 4:85a8391945b2 43 // GND GND
YPROY 4:85a8391945b2 44 //
YPROY 4:85a8391945b2 45 // GND of Nucleo board ------ GND
YPROY 4:85a8391945b2 46 //
YPROY 4:85a8391945b2 47 //Receiver: (powered by remote supply)
YPROY 4:85a8391945b2 48 //
YPROY 4:85a8391945b2 49 // _____
YPROY 4:85a8391945b2 50 // | O | RPM7140-V4R
YPROY 4:85a8391945b2 51 // +5---/\/\/\---- ----- (front view)
YPROY 4:85a8391945b2 52 // 10k | | | |
YPROY 4:85a8391945b2 53 // | | | |______+5v
YPROY 4:85a8391945b2 54 // Orange wire of servo SG90 --------------------.-- | |________GND
YPROY 4:85a8391945b2 55 // Red wire of servo SG90 -----+5V \| |
YPROY 4:85a8391945b2 56 // Brown wire of servo SG90 ---GND |-----
YPROY 4:85a8391945b2 57 // emitter /|
YPROY 4:85a8391945b2 58 // |
YPROY 4:85a8391945b2 59 // GND
YPROY 0:7a91558f8c41 60
YPROY 1:df2cee74ab8b 61 #include "mbed.h"
YPROY 1:df2cee74ab8b 62 #define NUMBER_OF_POSITIONS sizeof(pulseDurationInMicroSeconds)/sizeof(int)
YPROY 4:85a8391945b2 63 #define PWM_PERIOD_FOR_SG90_IN_MS 20
YPROY 4:85a8391945b2 64 #define PWM_PERIOD_FOR_MODULATION_IN_US 25
YPROY 1:df2cee74ab8b 65 DigitalOut userLED(LED1);
YPROY 2:7aacbdf39425 66 PwmOut towerProSG90(D11);
YPROY 4:85a8391945b2 67 PwmOut remoteControlOutput(D10);
YPROY 4:85a8391945b2 68 PwmOut modulatingOutput(D9);
YPROY 1:df2cee74ab8b 69 InterruptIn userButton(USER_BUTTON);
YPROY 1:df2cee74ab8b 70 int index;
YPROY 3:91070e0c0431 71 int pulseDurationInMicroSeconds[]=
YPROY 4:85a8391945b2 72 {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375};
YPROY 1:df2cee74ab8b 73 void responseToUserButtonPressed(void)
YPROY 0:7a91558f8c41 74 {
YPROY 1:df2cee74ab8b 75 index++;
YPROY 1:df2cee74ab8b 76 if (index >= NUMBER_OF_POSITIONS)
YPROY 0:7a91558f8c41 77 {
YPROY 1:df2cee74ab8b 78 index = 0;
YPROY 0:7a91558f8c41 79 }
YPROY 1:df2cee74ab8b 80 towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
YPROY 4:85a8391945b2 81 remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
YPROY 0:7a91558f8c41 82 }
YPROY 0:7a91558f8c41 83
YPROY 1:df2cee74ab8b 84 int main()
YPROY 1:df2cee74ab8b 85 {
YPROY 1:df2cee74ab8b 86 index = 0;
YPROY 1:df2cee74ab8b 87 towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
YPROY 1:df2cee74ab8b 88 towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]);
YPROY 4:85a8391945b2 89 remoteControlOutput.period_ms(PWM_PERIOD_FOR_SG90_IN_MS);
YPROY 4:85a8391945b2 90 remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]);
YPROY 4:85a8391945b2 91 modulatingOutput.period_us(PWM_PERIOD_FOR_MODULATION_IN_US);
YPROY 4:85a8391945b2 92 modulatingOutput.pulsewidth_us(PWM_PERIOD_FOR_MODULATION_IN_US/2);
YPROY 1:df2cee74ab8b 93 userButton.fall(&responseToUserButtonPressed);
YPROY 0:7a91558f8c41 94
YPROY 1:df2cee74ab8b 95 while(1)
YPROY 1:df2cee74ab8b 96 {
YPROY 1:df2cee74ab8b 97 userLED = !userLED;
YPROY 0:7a91558f8c41 98 wait(1);
YPROY 0:7a91558f8c41 99 }
YPROY 0:7a91558f8c41 100 }