Lab 3
Dependencies: Motor Servo mbed
Revision 0:cb674e873f8e, committed 2015-10-12
- Comitter:
- m182376
- Date:
- Mon Oct 12 17:54:27 2015 +0000
- Commit message:
- Lab 3
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.h Mon Oct 12 17:54:27 2015 +0000
@@ -0,0 +1,56 @@
+/* mbed simple H-bridge motor controller
+ * Copyright (c) 2007-2010, sford, http://mbed.org
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_MOTOR_H
+#define MBED_MOTOR_H
+
+#include "mbed.h"
+
+/** Interface to control a standard DC motor
+ *
+ * with an H-bridge using a PwmOut and 2 DigitalOuts
+ */
+class Motor {
+public:
+
+ /** Create a motor control interface
+ *
+ * @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed
+ * @param fwd A DigitalOut, set high when the motor should go forward
+ * @param rev A DigitalOut, set high when the motor should go backwards
+ */
+ Motor(PinName pwm, PinName fwd, PinName rev);
+
+ /** Set the speed of the motor
+ *
+ * @param speed The speed of the motor as a normalised value between -1.0 and 1.0
+ */
+ void speed(float speed);
+
+protected:
+ PwmOut _pwm;
+ DigitalOut _fwd;
+ DigitalOut _rev;
+
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.lib Mon Oct 12 17:54:27 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Motor/#f265e441bcd9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.h Mon Oct 12 17:54:27 2015 +0000
@@ -0,0 +1,98 @@
+/* mbed R/C Servo Library
+ * Copyright (c) 2007-2010 sford, cstyles
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_SERVO_H
+#define MBED_SERVO_H
+
+#include "mbed.h"
+
+/** Servo control class, based on a PwmOut
+ *
+ * Example:
+ * @code
+ * // Continuously sweep the servo through it's full range
+ * #include "mbed.h"
+ * #include "Servo.h"
+ *
+ * Servo myservo(p21);
+ *
+ * int main() {
+ * while(1) {
+ * for(int i=0; i<100; i++) {
+ * myservo = i/100.0;
+ * wait(0.01);
+ * }
+ * for(int i=100; i>0; i--) {
+ * myservo = i/100.0;
+ * wait(0.01);
+ * }
+ * }
+ * }
+ * @endcode
+ */
+class Servo {
+
+public:
+ /** Create a servo object connected to the specified PwmOut pin
+ *
+ * @param pin PwmOut pin to connect to
+ */
+ Servo(PinName pin);
+
+ /** Set the servo position, normalised to it's full range
+ *
+ * @param percent A normalised number 0.0-1.0 to represent the full range.
+ */
+ void write(float percent);
+
+ /** Read the servo motors current position
+ *
+ * @param returns A normalised number 0.0-1.0 representing the full range.
+ */
+ float read();
+
+ /** Set the servo position
+ *
+ * @param degrees Servo position in degrees
+ */
+ void position(float degrees);
+
+ /** Allows calibration of the range and angles for a particular servo
+ *
+ * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
+ * @param degrees Angle from centre to maximum/minimum position in degrees
+ */
+ void calibrate(float range = 0.0005, float degrees = 45.0);
+
+ /** Shorthand for the write and read functions */
+ Servo& operator= (float percent);
+ Servo& operator= (Servo& rhs);
+ operator float();
+
+protected:
+ PwmOut _pwm;
+ float _range;
+ float _degrees;
+ float _p;
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Mon Oct 12 17:54:27 2015 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/m182376/code/Servo/#d8b93b7c3505
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Oct 12 17:54:27 2015 +0000
@@ -0,0 +1,82 @@
+#include "mbed.h"
+#include "Motor.h"
+#include "Servo.h"
+
+
+
+Motor m(p25,p27,p28);
+Servo mservo(p24);
+Servo mservo1(p23);
+
+float i = 0.0;
+float j = 0.0;
+
+int main()
+{
+
+ mservo.calibrate(.0009,90);
+ mservo1.calibrate(.0009,90);
+
+ m.speed(i);
+ while(1) {
+
+ if (i!=1) {
+
+ for(i=0.2; i<=1.0; i=i+.1 ) {
+
+ m.speed(i);
+
+ for( j=0.0; j<=1.0; j=j+0.001 ) {
+
+ mservo = j ;
+ mservo1 = (1-j);
+ wait(.005);
+
+ }
+
+ m.speed(-i);
+
+ for( j=1.0 ; j>=0.0; j=j-0.001 ) {
+
+ mservo = j ;
+ mservo1 = (1-j);
+ wait(.005);
+
+ }
+
+ }
+
+ }
+
+ else if (i>=1) {
+
+ m.speed(i);
+
+ for( j=0.0; j<=1.0; j=j+0.001 ) {
+
+ mservo = j ;
+ mservo1 = (1-j);
+ wait(.005);
+
+ }
+
+ m.speed(-i);
+
+ for( j=1.0 ; j>=0.0; j=j-0.001 ) {
+
+ mservo = j ;
+ mservo1 = (1-j);
+ wait(.005);
+
+ }
+
+ }
+
+ }
+
+}
+
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Oct 12 17:54:27 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68 \ No newline at end of file