Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Motor.cpp
00001 /* mbed simple H-bridge motor controller 00002 * Copyright (c) 2007-2010, sford 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 */ 00022 00023 #include "mbed.h" 00024 #include "Motor.h" 00025 #include<iostream> 00026 00027 Motor::Motor(PinName pwm, PinName fwd, PinName rev): 00028 _pwm(pwm), _fwd(fwd), _rev(rev) { 00029 00030 // Set initial condition of PWM 00031 _pwm.period(0.002); 00032 _pwm = 0; 00033 00034 // Initial condition of output enables 00035 _fwd = 0; 00036 _rev = 0; 00037 } 00038 00039 void Motor::speed(float speed) { 00040 _fwd = (speed > (float)0.0); 00041 _rev = (speed < (float)0.0); 00042 _pwm = fabs(speed); 00043 } 00044 00045 void Motor::period(float period){ 00046 00047 _pwm.period(period); 00048 00049 } 00050 00051 void Motor::brake(int highLow){ 00052 00053 if(highLow == BRAKE_HIGH){ 00054 _fwd = 1; 00055 _rev = 1; 00056 } 00057 else if(highLow == BRAKE_LOW){ 00058 _fwd = 0; 00059 _rev = 0; 00060 } 00061 00062 } 00063 00064 void Motor::forcebrake(){ 00065 _pwm = 1.0; 00066 _fwd = 1; 00067 _rev = 1; 00068 } 00069 00070 int main() { 00071 string arah, rotate, haha; 00072 00073 Motor depan(PC_7,PA_9,PA_8); 00074 Motor kiri(PA_7,PA_6,PA_5); 00075 Motor kanan(PB_3,PA_10,PA_2); 00076 00077 do { 00078 getline (cin, arah); 00079 getline (cin, rotate); 00080 getline (cin, haha); 00081 if (arah == "depan"){ 00082 kanan.speed(-1.0); 00083 kiri.speed(1.0); 00084 } 00085 else if (arah == "belakang"){ 00086 kiri.speed(-1.0); 00087 kanan.speed(1.0); 00088 } 00089 else if (arah == "kiri") { 00090 depan.speed(-1.0); 00091 kiri.speed(1.0); 00092 kanan.speed(1.0); 00093 } 00094 else if (arah == "kanan") { 00095 depan.speed(1.0); 00096 kiri.speed(-1.0); 00097 kanan.speed(-1.0); 00098 } 00099 00100 if (rotate == "jam") { //searah jarum jam 00101 depan.speed(0.5); 00102 kiri.speed(0.5); 00103 kanan.speed(0.5); 00104 } 00105 else if (rotate == "nonjam") { //berlawanan jarum jam 00106 depan.speed(-0.5); 00107 kiri.speed(-0.5); 00108 kanan.speed(-0.5); 00109 } 00110 00111 if (haha == "haha") { //maju ke depan sambil berotasi berlawan 00112 depan.speed(-0.9); 00113 kiri.speed(-0.6); 00114 kanan.speed(-0.75); 00115 } 00116 00117 } while(1 != 0); 00118 }
Generated on Fri Jul 15 2022 19:25:11 by
1.7.2