For make this example you must use a NUCLEO board (we tested this example on NUCLEO-F411RE) and connect it the components below: A potentiometer - Connect it to 3,3v and GND and the central pin of the potentiometer must connect to A5 (PA_10). A RC motor - Connect the BLACK wire to GND, RED wire to 5v and ORANGE wire to D2 (PC_0).

Dependencies:   mbed

Committer:
emcu
Date:
Sun Dec 17 22:35:50 2017 +0000
Revision:
0:2a4e076aeefa
Simple test for RC motor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emcu 0:2a4e076aeefa 1 #include "mbed.h"
emcu 0:2a4e076aeefa 2
emcu 0:2a4e076aeefa 3 /*
emcu 0:2a4e076aeefa 4 By www.emcu.eu
emcu 0:2a4e076aeefa 5 Date December 2018
emcu 0:2a4e076aeefa 6 Simple test for RC motor
emcu 0:2a4e076aeefa 7 Version 1.0
emcu 0:2a4e076aeefa 8 Tested on the NUCLEO-F411RE and MG996R RC motor
emcu 0:2a4e076aeefa 9
emcu 0:2a4e076aeefa 10 For make this example you must use a NUCLEO board (we tested this example on
emcu 0:2a4e076aeefa 11 NUCLEO-F411RE) and connect it the components below:
emcu 0:2a4e076aeefa 12 A potentiometer - Connect it to 3,3v and GND and the central pin of the
emcu 0:2a4e076aeefa 13 potentiometer must connect to A5 (PA_10).
emcu 0:2a4e076aeefa 14 A RC motor - Connect the BLACK wire to GND, RED wire to 5v and
emcu 0:2a4e076aeefa 15 ORANGE wire to D2 (PC_0).
emcu 0:2a4e076aeefa 16
emcu 0:2a4e076aeefa 17 For more info see here:
emcu 0:2a4e076aeefa 18 http://www.emcu.eu/test-board-for-rc-motors
emcu 0:2a4e076aeefa 19
emcu 0:2a4e076aeefa 20 For use this example turn the potentiometer all to right and you must see the motor that move to right and a LED that flash.
emcu 0:2a4e076aeefa 21 If you move the potentiometer all to left you must see the motor that move left.
emcu 0:2a4e076aeefa 22 If you move the potentiometer in the middle position the RC motor is in stop and the LED flash fast.
emcu 0:2a4e076aeefa 23 If the potentiometer is in the middle and you press Blue button you start an automatic sequence.
emcu 0:2a4e076aeefa 24
emcu 0:2a4e076aeefa 25 For see the message from the SW open TeraTerm (terminal emulation) and configure it to: 8N1
emcu 0:2a4e076aeefa 26
emcu 0:2a4e076aeefa 27 Tested on Servo Motor:
emcu 0:2a4e076aeefa 28 MG996R - http://www.electronicoscaldas.com/datasheet/MG996R_Tower-Pro.pdf
emcu 0:2a4e076aeefa 29
emcu 0:2a4e076aeefa 30 RED - + Vcc from 4,5 to 7,2V
emcu 0:2a4e076aeefa 31 BLACK - GND
emcu 0:2a4e076aeefa 32 ORANGE - PWM
emcu 0:2a4e076aeefa 33
emcu 0:2a4e076aeefa 34 Duty - example: 1.9 (mS)
emcu 0:2a4e076aeefa 35 \ /
emcu 0:2a4e076aeefa 36 \/
emcu 0:2a4e076aeefa 37 __
emcu 0:2a4e076aeefa 38 __| |_____________
emcu 0:2a4e076aeefa 39 ... 20mS......
emcu 0:2a4e076aeefa 40
emcu 0:2a4e076aeefa 41 For rotate to right use value 1 for Duty
emcu 0:2a4e076aeefa 42 For rotate to left use value 5 for Duty
emcu 0:2a4e076aeefa 43
emcu 0:2a4e076aeefa 44 */
emcu 0:2a4e076aeefa 45
emcu 0:2a4e076aeefa 46 AnalogIn adc_chA5(PC_0);
emcu 0:2a4e076aeefa 47 DigitalOut Drive(PA_10);
emcu 0:2a4e076aeefa 48 DigitalOut led(LED1);
emcu 0:2a4e076aeefa 49 DigitalIn mybutton(USER_BUTTON);
emcu 0:2a4e076aeefa 50
emcu 0:2a4e076aeefa 51 #define Right 1 // Move to Right -> DESTRA or DX
emcu 0:2a4e076aeefa 52 #define Left 5 // Move to LEFT <- SINISTRA or SX
emcu 0:2a4e076aeefa 53
emcu 0:2a4e076aeefa 54 void Move(double);
emcu 0:2a4e076aeefa 55 void Seq1(int n);
emcu 0:2a4e076aeefa 56 void Seq2(void);
emcu 0:2a4e076aeefa 57
emcu 0:2a4e076aeefa 58 int main()
emcu 0:2a4e076aeefa 59 {
emcu 0:2a4e076aeefa 60 float Pot = 0;
emcu 0:2a4e076aeefa 61 int Movimento = 1;
emcu 0:2a4e076aeefa 62
emcu 0:2a4e076aeefa 63 printf("\n\r STM32 RC motor test ver.1.0\n\r");
emcu 0:2a4e076aeefa 64
emcu 0:2a4e076aeefa 65 Pot=adc_chA5;
emcu 0:2a4e076aeefa 66
emcu 0:2a4e076aeefa 67 while(1)
emcu 0:2a4e076aeefa 68 {
emcu 0:2a4e076aeefa 69 led = !led;
emcu 0:2a4e076aeefa 70 wait(0.1);
emcu 0:2a4e076aeefa 71
emcu 0:2a4e076aeefa 72 Pot = adc_chA5;
emcu 0:2a4e076aeefa 73 Pot = Pot * 3300; // Change the value to be in the 0 to 3300 range
emcu 0:2a4e076aeefa 74 Movimento = Pot/1000;
emcu 0:2a4e076aeefa 75 printf(" 1 - Val.Pot %.0f mV - Movimento %d \n\r",Pot,Movimento);
emcu 0:2a4e076aeefa 76
emcu 0:2a4e076aeefa 77 if(Pot>=1000 & Pot<=2900)
emcu 0:2a4e076aeefa 78 {
emcu 0:2a4e076aeefa 79 printf("\n\r *** RC motor stopped - Press Blue button for automatic sequence ***\n\r");
emcu 0:2a4e076aeefa 80 led = 0;
emcu 0:2a4e076aeefa 81 if (mybutton == 0)
emcu 0:2a4e076aeefa 82 Seq2();
emcu 0:2a4e076aeefa 83 }
emcu 0:2a4e076aeefa 84 else
emcu 0:2a4e076aeefa 85 {
emcu 0:2a4e076aeefa 86 if (Movimento <= 1)
emcu 0:2a4e076aeefa 87 Movimento = 1;
emcu 0:2a4e076aeefa 88 else
emcu 0:2a4e076aeefa 89 Movimento = 5;
emcu 0:2a4e076aeefa 90 printf(" 2 - Val.Pot %.0f mV - Movimento %d \n\r",Pot,Movimento);
emcu 0:2a4e076aeefa 91 Seq1(Movimento);
emcu 0:2a4e076aeefa 92 wait(1);
emcu 0:2a4e076aeefa 93 }
emcu 0:2a4e076aeefa 94
emcu 0:2a4e076aeefa 95 wait(0.2);
emcu 0:2a4e076aeefa 96 }
emcu 0:2a4e076aeefa 97 }
emcu 0:2a4e076aeefa 98
emcu 0:2a4e076aeefa 99 void Move(double ON)
emcu 0:2a4e076aeefa 100 {
emcu 0:2a4e076aeefa 101 Drive=1;
emcu 0:2a4e076aeefa 102 wait_ms(ON);
emcu 0:2a4e076aeefa 103 Drive=0;
emcu 0:2a4e076aeefa 104 wait_ms(25);
emcu 0:2a4e076aeefa 105 }
emcu 0:2a4e076aeefa 106
emcu 0:2a4e076aeefa 107
emcu 0:2a4e076aeefa 108
emcu 0:2a4e076aeefa 109 void Seq1(int n)
emcu 0:2a4e076aeefa 110 {
emcu 0:2a4e076aeefa 111 if (n<=1)
emcu 0:2a4e076aeefa 112 {
emcu 0:2a4e076aeefa 113 n=5;
emcu 0:2a4e076aeefa 114 printf("\n\rMove to RIGHT ->\n\r");
emcu 0:2a4e076aeefa 115 }
emcu 0:2a4e076aeefa 116 else
emcu 0:2a4e076aeefa 117 {
emcu 0:2a4e076aeefa 118 n=1;
emcu 0:2a4e076aeefa 119 printf("\n\rMove to LEFT <-\n\r");
emcu 0:2a4e076aeefa 120 }
emcu 0:2a4e076aeefa 121 Move(n);
emcu 0:2a4e076aeefa 122 }
emcu 0:2a4e076aeefa 123
emcu 0:2a4e076aeefa 124 void Seq2(void)
emcu 0:2a4e076aeefa 125 {
emcu 0:2a4e076aeefa 126 int n=0;
emcu 0:2a4e076aeefa 127
emcu 0:2a4e076aeefa 128 printf("\n\rSequ n.2\n\r");
emcu 0:2a4e076aeefa 129 led = 1;
emcu 0:2a4e076aeefa 130
emcu 0:2a4e076aeefa 131 printf("Move to 20 times to <-\n\r");
emcu 0:2a4e076aeefa 132 for(n=0; n<20; n++)
emcu 0:2a4e076aeefa 133 Move(Left);
emcu 0:2a4e076aeefa 134 wait(0.3);
emcu 0:2a4e076aeefa 135 printf("Move to 20 times to ->\n\r");
emcu 0:2a4e076aeefa 136 for(n=0; n<20; n++)
emcu 0:2a4e076aeefa 137 Move(Right);
emcu 0:2a4e076aeefa 138 wait(0.3);
emcu 0:2a4e076aeefa 139
emcu 0:2a4e076aeefa 140 printf("Move to 8 times to <-\n\r");
emcu 0:2a4e076aeefa 141 for(n=0; n<8; n++)
emcu 0:2a4e076aeefa 142 Move(Left);
emcu 0:2a4e076aeefa 143 wait(0.3);
emcu 0:2a4e076aeefa 144 printf("Move to 5 times to ->\n\r");
emcu 0:2a4e076aeefa 145 for(n=0; n<5; n++)
emcu 0:2a4e076aeefa 146 Move(Right);
emcu 0:2a4e076aeefa 147 wait(0.3);
emcu 0:2a4e076aeefa 148
emcu 0:2a4e076aeefa 149 printf("Move to 3 times to <-\n\r");
emcu 0:2a4e076aeefa 150 for(n=0; n<3; n++)
emcu 0:2a4e076aeefa 151 Move(Left);
emcu 0:2a4e076aeefa 152 wait(0.3);
emcu 0:2a4e076aeefa 153 printf("Move to 3 times to ->\n\r");
emcu 0:2a4e076aeefa 154 for(n=0; n<3; n++)
emcu 0:2a4e076aeefa 155 Move(Right);
emcu 0:2a4e076aeefa 156 wait(0.3);
emcu 0:2a4e076aeefa 157
emcu 0:2a4e076aeefa 158 printf("Move to 2 times to <-\n\r");
emcu 0:2a4e076aeefa 159 for(n=0; n<2; n++)
emcu 0:2a4e076aeefa 160 Move(Left);
emcu 0:2a4e076aeefa 161 wait(0.3);
emcu 0:2a4e076aeefa 162 printf("Move to 2 times to ->\n\r");
emcu 0:2a4e076aeefa 163 for(n=0; n<2; n++)
emcu 0:2a4e076aeefa 164 Move(Right);
emcu 0:2a4e076aeefa 165 wait(0.3);
emcu 0:2a4e076aeefa 166
emcu 0:2a4e076aeefa 167 printf("Move to 1 times to <-\n\r");
emcu 0:2a4e076aeefa 168 for(n=0; n<1; n++)
emcu 0:2a4e076aeefa 169 Move(Left);
emcu 0:2a4e076aeefa 170 wait(0.3);
emcu 0:2a4e076aeefa 171 printf("Move to 1 times to ->\n\r");
emcu 0:2a4e076aeefa 172 for(n=0; n<1; n++)
emcu 0:2a4e076aeefa 173 Move(Right);
emcu 0:2a4e076aeefa 174 wait(0.3);
emcu 0:2a4e076aeefa 175
emcu 0:2a4e076aeefa 176 printf("END - Seq n.2\n\r");
emcu 0:2a4e076aeefa 177 led = 0;
emcu 0:2a4e076aeefa 178 }