for publish
Dependencies: HC_SR04_Ultrasonic_Library mbed
Revision 0:fded9bf799e2, committed 2017-03-14
- Comitter:
- jasonbx
- Date:
- Tue Mar 14 15:49:33 2017 +0000
- Commit message:
- For publish
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HC_SR04_Ultrasonic_Library.lib Tue Mar 14 15:49:33 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Motordriver/motordriver.cpp Tue Mar 14 15:49:33 2017 +0000
@@ -0,0 +1,140 @@
+/*motor driver libary modified from the following libary,
+*
+* mbed simple H-bridge motor controller
+* Copyright (c) 2007-2010, sford
+*
+* by Christopher Hasler.
+*
+* from sford's libary,
+*
+* 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 "motordriver.h"
+
+#include "mbed.h"
+
+Motor::Motor(PinName pwm, PinName fwd, PinName rev, int brakeable):
+ _pwm(pwm), _fwd(fwd), _rev(rev) {
+
+ // Set initial condition of PWM
+ _pwm.period(0.001);
+ _pwm = 0;
+
+ // Initial condition of output enables
+ _fwd = 0;
+ _rev = 0;
+
+ //set if the motor dirver is capable of braking. (addition)
+ Brakeable= brakeable;
+ sign = 0;//i.e nothing.
+}
+
+float Motor::speed(float speed) {
+ float temp = 0;
+ if (sign == 0) {
+ _fwd = (speed > 0.0);
+ _rev = (speed < 0.0);
+ temp = abs(speed);
+ _pwm = temp;
+ } else if (sign == 1) {
+ if (speed < 0) {
+ _fwd = (speed > 0.0);
+ _rev = (speed < 0.0);
+ _pwm = 0;
+ temp = 0;
+ } else {
+ _fwd = (speed > 0.0);
+ _rev = (speed < 0.0);
+ temp = abs(speed);
+ _pwm = temp;
+ }
+ } else if (sign == -1) {
+ if (speed > 0) {
+ _fwd = (speed > 0.0);
+ _rev = (speed < 0.0);
+ _pwm = 0;
+ temp = 0;
+ } else {
+ _fwd = (speed > 0.0);
+ _rev = (speed < 0.0);
+ temp = abs(speed);
+ _pwm = temp;
+ }
+ }
+ if (speed > 0)
+ sign = 1;
+ else if (speed < 0) {
+ sign = -1;
+ } else if (speed == 0) {
+ sign = 0;
+ }
+ return temp;
+}
+// (additions)
+void Motor::coast(void) {
+ _fwd = 0;
+ _rev = 0;
+ _pwm = 0;
+ sign = 0;
+}
+
+float Motor::stop(float duty) {
+ if (Brakeable == 1) {
+ _fwd = 1;
+ _rev = 1;
+ _pwm = duty;
+ sign = 0;
+ return duty;
+ } else
+ Motor::coast();
+ return -1;
+}
+
+float Motor::state(void) {
+ if ((_fwd == _rev) && (_pwm > 0)) {
+ return -2;//braking
+ } else if (_pwm == 0) {
+ return 2;//coasting
+ } else if ((_fwd == 0) && (_rev == 1)) {
+ return -(_pwm);//reversing
+ } else if ((_fwd == 1) && (_rev == 0)) {
+ return _pwm;//fowards
+ } else
+ return -3;//error
+}
+
+/*
+ test code, this demonstrates working motor drivers.
+
+Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can break
+Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can break
+int main() {
+ for (float s=-1.0; s < 1.0 ; s += 0.01) {
+ A.speed(s);
+ B.speed(s);
+ wait(0.02);
+ }
+ A.stop();
+ B.stop();
+ wait(1);
+ A.coast();
+ B.coast();
+}
+*/
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Motordriver/motordriver.h Tue Mar 14 15:49:33 2017 +0000
@@ -0,0 +1,91 @@
+/*motor driver libary modified from the following libary,
+*
+* mbed simple H-bridge motor controller
+* Copyright (c) 2007-2010, sford
+*
+* by Christopher Hasler.
+*
+* from sford's libary,
+*
+* 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
+* @param set if the motor driver is able to do braking 0 false 1 true.
+*/
+ Motor(PinName pwm, PinName fwd, PinName rev, int brakeable);
+
+/** Set the speed of the motor
+*
+* @param speed The speed of the motor as a normalised value between -1.0 and 1.0.
+* @return the applied speed to the motor after checking to ensure motor doesn't switch from forward to reverse without stopping.
+*/
+ float speed(float speed);
+
+/** Set the the motor to coast
+*
+* @param void
+* @return motor coasts until another instruction is recived.
+*/
+
+ void coast(void);
+
+/** Set the motor to dynamicaly brake
+*
+* @param float 0 - 1.0 provides some control over how hard the motor brakes.
+* @return duty applied to motor driver. -1 is error, motor driver can't brake.
+*/
+
+ float stop(float duty);
+/** return the current state of the motor
+*
+* @param void
+* @return state of motor, -1 to 1 is speed, -2 is braking, 2 is coasting. -3 is error.
+*/
+ float state(void);
+
+ protected:
+ PwmOut _pwm;
+ DigitalOut _fwd;
+ DigitalOut _rev;
+ int Brakeable; // cna the motor driver break
+ int sign; //prevents throwing the motor from full foward to full reverse and stuff melting.
+
+};
+
+
+
+
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SongPlayer.h Tue Mar 14 15:49:33 2017 +0000
@@ -0,0 +1,41 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class SongPlayer
+{
+public:
+ SongPlayer(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the constructor
+ }
+// class method to play a note based on PwmOut class
+ void PlaySong(float frequency[], float duration[], float volume=1.0) {
+ vol = volume;
+ notecount = 0;
+ _pin.period(1.0/frequency[notecount]);
+ _pin = volume/2.0;
+ noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
+ // setup timer to interrupt for next note to play
+ frequencyptr = frequency;
+ durationptr = duration;
+ //returns after first note starts to play
+ }
+ void nextnote();
+private:
+ Timeout noteduration;
+ PwmOut _pin;
+ int notecount;
+ float vol;
+ float * frequencyptr;
+ float * durationptr;
+};
+//Interrupt Routine to play next note
+void SongPlayer::nextnote()
+{
+ _pin = 0.0;
+ notecount++; //setup next note in song
+ if (durationptr[notecount]!=0.0) {
+ _pin.period(1.0/frequencyptr[notecount]);
+ noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
+ _pin = vol/2.0;
+ } else
+ _pin = 0.0; //turn off on last note
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Mar 14 15:49:33 2017 +0000
@@ -0,0 +1,147 @@
+#include "mbed.h"
+#include "motordriver.h"
+#include "ultrasonic.h"
+#include "SongPlayer.h"
+int x=1;
+
+
+void dist(int distance)
+{
+ if (distance<100){
+ x=0;
+ }
+ //put code here to execute when the distance has changed
+ printf("Distance %d mm\r\n", distance);
+
+}
+
+
+ultrasonic mu(p9, p10, .1, 1, &dist);
+//BusOut myled(LED1,LED2,LED3,LED4);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+Serial blue(p28,p27);
+AnalogIn ain(A0); //p15
+SongPlayer mySpeaker(p26);
+Motor A(p23, p6, p5, 1); // pwm, fwd, rev, can brake
+Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can brake
+
+float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
+ 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
+ };
+float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
+ 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
+ };
+
+int main()
+{
+
+ mu.startUpdates();//start measuring the distance
+ char bnum=0;
+ char bhit=0;
+
+ while(x==1){
+ mu.checkDistance();
+
+ led1 = (ain > 0.15f) ? 1 : 0;
+ led2 = (ain > 0.30f) ? 1 : 0;
+ led3 = (ain > 0.45f) ? 1 : 0;
+ led4 = (ain > 0.7f) ? 1 : 0;
+ A.speed(0.4);
+ B.speed(0.4);
+
+
+
+
+ // printf("Distance %d mm\r\n", distance);
+
+ }
+ mySpeaker.PlaySong(note,duration);
+ // loops forever while song continues to play to end using interrupts
+ A.speed(0);
+ B.speed(0);
+ while(1){
+ if (blue.getc()=='!') {
+ if (blue.getc()=='B') { //button data packet
+ bnum = blue.getc(); //button number
+ bhit = blue.getc(); //1=hit, 0=release
+ if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
+ // myled = bnum - '0'; //current button number will appear on LEDs
+ switch (bnum) {
+ case '1': //number button 1
+ if (bhit=='1') {
+ A.speed(0);
+ B.speed(0);
+ } else {
+ //add release code here
+ }
+ break;
+ case '2': //number button 2
+ if (bhit=='1') {
+ //add hit code here
+ } else {
+ //add release code here
+ }
+ break;
+ case '3': //number button 3
+ if (bhit=='1') {
+ //add hit code here
+ } else {
+ //add release code here
+ }
+ break;
+ case '4': //number button 4
+ if (bhit=='1') {
+ //add hit code here
+ } else {
+ //add release code here
+ }
+ break;
+ case '5': //button 5 up arrow
+ if (bhit=='1') {
+ A.speed(1);
+ B.speed(1);
+ wait(0.02);
+ } else {
+ //add release code here
+ }
+ break;
+ case '6': //button 6 down arrow
+ if (bhit=='1') {
+ A.speed(-1);
+ B.speed(-1);
+ wait(0.02);
+ } else {
+ //add release code here
+ }
+ break;
+ case '7': //button 7 left arrow
+ if (bhit=='1') {
+ A.speed(-1);
+ B.speed(1);
+ wait(0.02);
+ } else {
+ //add release code here
+ }
+ break;
+ case '8': //button 8 right arrow
+ if (bhit=='1') {
+ A.speed(1);
+ B.speed(-1);
+ wait(0.02);
+ } else {
+ //add release code here
+ }
+ break;
+ default:
+ A.speed(0.3);
+ B.speed(0.3);
+ break;
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Mar 14 15:49:33 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e1686b8d5b90 \ No newline at end of file