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@6:6e9120cf5640, 2017-03-24 (annotated)
- Committer:
- YROY2004
- Date:
- Fri Mar 24 18:14:46 2017 +0000
- Revision:
- 6:6e9120cf5640
- Parent:
- 5:78a94312c383
- Child:
- 7:db4f43c3ae10
Software that allows control of the position of a sg90 servo using the user button and reading of the ambiant temperature using a 1-wire DS18S20 (based on the DS1820 librairy available on the web: only 3 lines modified)
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 | 1:df2cee74ab8b | 54 | InterruptIn userButton(USER_BUTTON); |
YPROY | 1:df2cee74ab8b | 55 | int index; |
YPROY | 3:91070e0c0431 | 56 | int pulseDurationInMicroSeconds[]= |
YPROY | 4:85a8391945b2 | 57 | {1500,1625,1750,1875,2000, 1875,1750,1625,1500,1375,1250,1125,1000,1125,1250,1375}; |
YPROY | 1:df2cee74ab8b | 58 | void responseToUserButtonPressed(void) |
YPROY | 0:7a91558f8c41 | 59 | { |
YPROY | 1:df2cee74ab8b | 60 | index++; |
YPROY | 1:df2cee74ab8b | 61 | if (index >= NUMBER_OF_POSITIONS) |
YPROY | 0:7a91558f8c41 | 62 | { |
YPROY | 1:df2cee74ab8b | 63 | index = 0; |
YPROY | 0:7a91558f8c41 | 64 | } |
YPROY | 1:df2cee74ab8b | 65 | towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]); |
YPROY | 0:7a91558f8c41 | 66 | } |
YPROY | 0:7a91558f8c41 | 67 | |
YPROY | 1:df2cee74ab8b | 68 | int main() |
YPROY | 1:df2cee74ab8b | 69 | { |
YPROY | 1:df2cee74ab8b | 70 | index = 0; |
YPROY | 1:df2cee74ab8b | 71 | towerProSG90.period_ms(PWM_PERIOD_FOR_SG90_IN_MS); |
YPROY | 1:df2cee74ab8b | 72 | towerProSG90.pulsewidth_us(pulseDurationInMicroSeconds[index]); |
YPROY | 1:df2cee74ab8b | 73 | userButton.fall(&responseToUserButtonPressed); |
YROY2004 | 5:78a94312c383 | 74 | |
YROY2004 | 5:78a94312c383 | 75 | userLED = 1; |
YPROY | 1:df2cee74ab8b | 76 | while(1) |
YPROY | 1:df2cee74ab8b | 77 | { |
YPROY | 1:df2cee74ab8b | 78 | userLED = !userLED; |
YROY2004 | 5:78a94312c383 | 79 | |
YROY2004 | 6:6e9120cf5640 | 80 | probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
YROY2004 | 5:78a94312c383 | 81 | printf("Il fait: %3.1foC\r\n", probe.temperature()); |
YROY2004 | 5:78a94312c383 | 82 | wait(1.25); |
YPROY | 0:7a91558f8c41 | 83 | } |
YPROY | 0:7a91558f8c41 | 84 | } |