this is part of the pill dispenser prototype model. the servo motor here controls the gate of the cartridge to drop one pill at a time to the channel.
Revision 0:e31980f4132f, committed 2018-03-23
- Comitter:
- khp007
- Date:
- Fri Mar 23 22:25:11 2018 +0000
- Commit message:
- to run the servo motor to move the gate of the dispenser cartridge
Changed in this revision
diff -r 000000000000 -r e31980f4132f Servo.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.cpp Fri Mar 23 22:25:11 2018 +0000 @@ -0,0 +1,74 @@ +/* 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. + */ + +#include "Servo.h" +#include "mbed.h" + +static float clamp(float value, float min, float max) { + if(value < min) { + return min; + } else if(value > max) { + return max; + } else { + return value; + } +} + +Servo::Servo(PinName pin) : _pwm(pin) { + calibrate(); + write(0.5); +} + +void Servo::write(float percent) { + float offset = _range * 2.0 * (percent - 0.5); + _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range)); + _p = clamp(percent, 0.0, 1.0); +} + +void Servo::position(float degrees) { + float offset = _range * (degrees / _degrees); + _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range)); +} + +void Servo::calibrate(float range, float degrees) { + _range = range; + _degrees = degrees; +} + +float Servo::read() { + return _p; +} + +Servo& Servo::operator= (float percent) { + write(percent); + return *this; +} + +Servo& Servo::operator= (Servo& rhs) { + write(rhs.read()); + return *this; +} + +Servo::operator float() { + return read(); +}
diff -r 000000000000 -r e31980f4132f Servo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.h Fri Mar 23 22:25:11 2018 +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" +//#include "Servo.cpp" +/** 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
diff -r 000000000000 -r e31980f4132f main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Mar 23 22:25:11 2018 +0000 @@ -0,0 +1,80 @@ +// Servo control class, based on a PwmOut + + + // Continuously sweep the servo through it's full range + #include "mbed.h" + #include "Servo.h" + + + + Serial pc(USBTX,USBRX); // Create a serial connection to pc through the mbed USB cable + Servo LRmyservo(PTC5); + InterruptIn motion(D2); + DigitalIn sw2(SW2); + + int motion_detected = 0; + + void irq_handler(void) + { + //motion_detected = 1; + } + + void rotateServo(bool rotate){ + int i = 0; + if (rotate == true){ //rotate right + for(i=10; i<111; i++){ + LRmyservo = i/100.00; + //pc.printf("read = %f\r\n", LRmyservo); + wait_ms(4); + if(motion_detected){ + break; + } + } + } else{ + for(i=110; i>=10; i--){ //rotate left + LRmyservo = i/100.00; + //pc.printf("read = %d, signal = %d\r\n", LRmyservo.read(),something); + wait_ms(4); + if(motion_detected){ + break; + } + } + } + } + + //Servo UDmyservo(PTC7); + + int main() + { + //float range = .0009; + //LRmyservo.calibrate(range,45); + //dt.start(); + + //Initialize variables + bool flag = false; + bool rotate = true; + // motion.fall(&irq_handler); + + //Initilize motor positions + + while(flag == false){ + //check to make sure the motion sensor was not triggered + // if(motion_detected){ + // motion_detected = 0; + // pc.printf("I have fallen and I can't get up!\r\n"); + // flag = true; + // } + if (sw2 == 0){ + rotateServo(rotate); + if (rotate == true){ //adjust rotation direction for next iteration + rotate = false; + } else{ + rotate = true; + } + } + } + pc.printf("I am finished with the program"); + //wait_ms(5); + } + + \ No newline at end of file
diff -r 000000000000 -r e31980f4132f mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Mar 23 22:25:11 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/aa5281ff4a02 \ No newline at end of file