Software that allows basic remote control of the position of a servo (schematic included in the comments)
Fork of Nucleo_sg90_remote_control by
main.cpp@5:78a94312c383, 2017-03-23 (annotated)
- Committer:
- YROY2004
- Date:
- Thu Mar 23 19:44:48 2017 +0000
- Revision:
- 5:78a94312c383
- Parent:
- 4:85a8391945b2
- Child:
- 6:6e9120cf5640
Based on Nucleo_sg90_remote_control and MyDS1820
; Display temperature and allow control of the position of a SG90 servo by pressing on the user switch.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YROY2004 | 5:78a94312c383 | 1 | //247-426-Nucleo_Lab_8 |
YROY2004 | 5:78a94312c383 | 2 | //This software is derived from Nucleo_sg90_remote_control. |
YPROY | 1:df2cee74ab8b | 3 | //It outputs a PWM signal that is compatible with Tower Pro SG90 servos. |
YROY2004 | 5:78a94312c383 | 4 | //It makes the servo move according to the temperature read from a DS1820 |
YROY2004 | 5:78a94312c383 | 5 | //temperature sensor. |
YROY2004 | 5:78a94312c383 | 6 | //Pressing the user button makes the software define the temperature it then |
YROY2004 | 5:78a94312c383 | 7 | //reads as a reference temperature that is associated to the middle position |
YROY2004 | 5:78a94312c383 | 8 | //that can be taken by the sg90 servo. An increase in temperature relative to |
YROY2004 | 5:78a94312c383 | 9 | //this reference temperature will then make the servo rotate in one direction |
YROY2004 | 5:78a94312c383 | 10 | //whereas a decrease with respect to the reference will rather make it rotate in |
YROY2004 | 5:78a94312c383 | 11 | //the other direction. |
YROY2004 | 5:78a94312c383 | 12 | // |
YPROY | 1:df2cee74ab8b | 13 | |
YPROY | 1:df2cee74ab8b | 14 | //Just connect the brown wire of a SG90 servo to GND, its red wire to AVDD and |
YPROY | 2:7aacbdf39425 | 15 | //its the orange wire to D11 to have the SMT32-F401RE control the servo. |
YPROY | 4:85a8391945b2 | 16 | // |
YPROY | 4:85a8391945b2 | 17 | // |
YPROY | 4:85a8391945b2 | 18 | //material: |
YROY2004 | 5:78a94312c383 | 19 | // DS18S20: temperature sensor |
YPROY | 4:85a8391945b2 | 20 | // |
YPROY | 4:85a8391945b2 | 21 | //signals: |
YPROY | 4:85a8391945b2 | 22 | // D11= pwm signal to use to control SG90 directly |
YROY2004 | 5:78a94312c383 | 23 | // D8= used to communicate with the 1-wire temperature sensor Ds18s20 |
YPROY | 4:85a8391945b2 | 24 | // |
YROY2004 | 5:78a94312c383 | 25 | // Circuit: |
YPROY | 4:85a8391945b2 | 26 | // _____ |
YROY2004 | 5:78a94312c383 | 27 | // | | DS18S20 |
YROY2004 | 5:78a94312c383 | 28 | // -----(front view) |
YROY2004 | 5:78a94312c383 | 29 | // | | | |
YROY2004 | 5:78a94312c383 | 30 | // GND___| | |+5V |
YROY2004 | 5:78a94312c383 | 31 | // Orange wire of servo SG90 --D11 | |
YROY2004 | 5:78a94312c383 | 32 | // Red wire of servo SG90 -----+5V D8_______|-/\/\/\--+5v |
YROY2004 | 5:78a94312c383 | 33 | // Brown wire of servo SG90 ---GND 10k |
YROY2004 | 5:78a94312c383 | 34 | // |
YROY2004 | 5:78a94312c383 | 35 | // Hyperterminal configuration |
YROY2004 | 5:78a94312c383 | 36 | // 9600 bauds, 8-bit data, no parity |
YPROY | 0:7a91558f8c41 | 37 | |
YROY2004 | 5:78a94312c383 | 38 | |
YROY2004 | 5:78a94312c383 | 39 | |
YROY2004 | 5:78a94312c383 | 40 | #include "DS1820.h" |
YPROY | 1:df2cee74ab8b | 41 | #include "mbed.h" |
YPROY | 1:df2cee74ab8b | 42 | #define NUMBER_OF_POSITIONS sizeof(pulseDurationInMicroSeconds)/sizeof(int) |
YPROY | 4:85a8391945b2 | 43 | #define PWM_PERIOD_FOR_SG90_IN_MS 20 |
YPROY | 4:85a8391945b2 | 44 | #define PWM_PERIOD_FOR_MODULATION_IN_US 25 |
YROY2004 | 5:78a94312c383 | 45 | |
YROY2004 | 5:78a94312c383 | 46 | |
YROY2004 | 5:78a94312c383 | 47 | Serial pc(SERIAL_TX, SERIAL_RX); |
YROY2004 | 5:78a94312c383 | 48 | DS1820 probe(D8); |
YROY2004 | 5:78a94312c383 | 49 | |
YROY2004 | 5:78a94312c383 | 50 | PwmOut led(LED1); |
YPROY | 1:df2cee74ab8b | 51 | DigitalOut userLED(LED1); |
YROY2004 | 5:78a94312c383 | 52 | |
YPROY | 2:7aacbdf39425 | 53 | PwmOut towerProSG90(D11); |
YPROY | 4:85a8391945b2 | 54 | PwmOut remoteControlOutput(D10); |
YPROY | 4:85a8391945b2 | 55 | PwmOut modulatingOutput(D9); |
YPROY | 1:df2cee74ab8b | 56 | InterruptIn userButton(USER_BUTTON); |
YPROY | 1:df2cee74ab8b | 57 | int index; |
YPROY | 3:91070e0c0431 | 58 | int pulseDurationInMicroSeconds[]= |
YPROY | 4:85a8391945b2 | 59 | {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375}; |
YPROY | 1:df2cee74ab8b | 60 | void responseToUserButtonPressed(void) |
YPROY | 0:7a91558f8c41 | 61 | { |
YPROY | 1:df2cee74ab8b | 62 | index++; |
YPROY | 1:df2cee74ab8b | 63 | if (index >= NUMBER_OF_POSITIONS) |
YPROY | 0:7a91558f8c41 | 64 | { |
YPROY | 1:df2cee74ab8b | 65 | index = 0; |
YPROY | 0:7a91558f8c41 | 66 | } |
YPROY | 1:df2cee74ab8b | 67 | towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]); |
YPROY | 4:85a8391945b2 | 68 | remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]); |
YPROY | 0:7a91558f8c41 | 69 | } |
YPROY | 0:7a91558f8c41 | 70 | |
YPROY | 1:df2cee74ab8b | 71 | int main() |
YPROY | 1:df2cee74ab8b | 72 | { |
YPROY | 1:df2cee74ab8b | 73 | index = 0; |
YPROY | 1:df2cee74ab8b | 74 | towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS); |
YPROY | 1:df2cee74ab8b | 75 | towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]); |
YPROY | 4:85a8391945b2 | 76 | remoteControlOutput.period_ms(PWM_PERIOD_FOR_SG90_IN_MS); |
YPROY | 4:85a8391945b2 | 77 | remoteControlOutput.pulsewidth_us(PWM_PERIOD_FOR_SG90_IN_MS*1000 - pulseDurationInMicroSeconds[index]); |
YPROY | 4:85a8391945b2 | 78 | modulatingOutput.period_us(PWM_PERIOD_FOR_MODULATION_IN_US); |
YPROY | 4:85a8391945b2 | 79 | modulatingOutput.pulsewidth_us(PWM_PERIOD_FOR_MODULATION_IN_US/2); |
YPROY | 1:df2cee74ab8b | 80 | userButton.fall(&responseToUserButtonPressed); |
YROY2004 | 5:78a94312c383 | 81 | |
YROY2004 | 5:78a94312c383 | 82 | userLED = 1; |
YPROY | 1:df2cee74ab8b | 83 | while(1) |
YPROY | 1:df2cee74ab8b | 84 | { |
YPROY | 1:df2cee74ab8b | 85 | userLED = !userLED; |
YROY2004 | 5:78a94312c383 | 86 | |
YROY2004 | 5:78a94312c383 | 87 | int delai = 0; |
YROY2004 | 5:78a94312c383 | 88 | delai = probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
YROY2004 | 5:78a94312c383 | 89 | printf("Il fait: %3.1foC\r\n", probe.temperature()); |
YROY2004 | 5:78a94312c383 | 90 | wait(1.25); |
YPROY | 0:7a91558f8c41 | 91 | } |
YPROY | 0:7a91558f8c41 | 92 | } |