hardware classes
Revision 0:1fc280fa2177, committed 2010-06-02
- Comitter:
- UAVguy
- Date:
- Wed Jun 02 21:40:10 2010 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.cpp Wed Jun 02 21:40:10 2010 +0000
@@ -0,0 +1,79 @@
+#include "Servo.h"
+
+Servo::Servo(PinName port) : PwmOut(port)
+{
+ period = 20000;
+ maximum = 2400;
+ minimum = 600;
+ center = 1500;
+ position = 0;
+ //set default values
+ pulsewidth_us(0);
+ period_us(period);
+}
+
+void Servo::setposition(int input)
+{
+ position = input;
+ if(position > 255)
+ {
+ position = 255;
+ }
+ else if (position < -255)
+ {
+ position = -255;
+ }
+ //saturate
+ if(position >= 0)
+ {
+ pulsewidth_us(position * (maximum - center) / 255 + center);
+ }
+ else
+ {
+ pulsewidth_us(position * (center - minimum) / 255 + center);
+ }
+ //set position normalized to 255
+}
+
+void Servo::setperiod(int input)
+{
+ period = input;
+ period_us(period);
+}
+
+void Servo::setcenter(int input)
+{
+ center = input;
+}
+
+void Servo::setmaximum(int input)
+{
+ maximum = input;
+}
+
+void Servo::setminimum(int input)
+{
+ minimum = input;
+}
+
+Servo& Servo::operator =(int assignment)
+{
+ Servo::setposition(assignment);
+ //shorthand for setposition
+ *this = assignment;
+ return *this;
+ //allow multiple assignment
+}
+
+Servo::operator int()
+{
+ int measurement = read() * period;
+ if(measurement >= center)
+ {
+ return 255 * (measurement - center) / (maximum - center);
+ }
+ else
+ {
+ return 255 * (measurement - position) / (center - minimum);
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.h Wed Jun 02 21:40:10 2010 +0000
@@ -0,0 +1,32 @@
+#include "stdint.h"
+#include "mbed.h"
+
+class Servo : private PwmOut
+{
+ private:
+ int period;
+ int center;
+ int maximum;
+ int minimum;
+ int position;
+
+ public:
+ Servo(PinName);
+ //constructor sets defaults and starts servo with 0% duty cycle
+ void setposition (int);
+ //set normalized servo position (-255 to 255), default = 0
+ void setperiod (int);
+ //set the period in us, default = 20000
+ void setcenter (int);
+ //set center deflection pulse width in us, default = 1500
+ void setmaximum (int);
+ //set maximum deflection pulse width in us, default = 2400
+ void setminimum (int);
+ //set minimum deflection pulse width in us, default = 600
+ Servo& operator =(int);
+ //shorthand for setposition, ex: "ServoObj = 231;"
+ operator int();
+ //shorthand for position reading, ex: "check = ServoObj;"
+ /*NOTE: setposition must be called before any changes to
+ period, center, maximum, or minimum can take effect*/
+};
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/USRF_MB1310.cpp Wed Jun 02 21:40:10 2010 +0000
@@ -0,0 +1,132 @@
+#include "USRF_MB1310.h"
+
+USRF_MB1310::USRF_MB1310(PinName an, PinName tx, PinName rx)
+{
+ if (an != NC)
+ {
+ AnalogInput = new AnalogIn(an);
+ }
+ if (tx != NC)
+ {
+ SerialOutput = new DigitalOut(tx);
+ }
+ if (rx != NC)
+ {
+ SerialInput = new Serial(NC, rx);
+ }
+ operatingvoltage = 3.3;
+ unitfactor = 1;
+ scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;//range, millivolts, device, units
+ //set defaults
+}
+
+void USRF_MB1310::setoperatingvoltage(float input)
+{
+ operatingvoltage = input;
+ scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
+}
+
+void USRF_MB1310::selectunit(int unit)
+{
+ switch(unit)
+ {
+ case 0:
+ unitfactor = 1;
+ scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
+ break;
+ case 1:
+ unitfactor = 1 / 100;
+ scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
+ break;
+ case 2:
+ unitfactor = 1 / 2.54;
+ scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
+ break;
+ case 3:
+ unitfactor = 1 / 2.54 / 12;
+ scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
+ break;
+ }
+}
+
+void USRF_MB1310::requestserialquery()
+{
+ if (SerialOutput != 0)
+ {
+ SerialOutput->write(1);
+ wait_us(50);
+ SerialOutput->write(0);
+ }
+}
+ //hold pin high for at least 20 us to signal a serial query
+
+float USRF_MB1310::query()
+{
+ if (AnalogInput != 0)
+ {
+ distance = AnalogInput->read() * scalingfactor;
+ }
+ else if ((SerialInput != 0) && (SerialInput->readable()))
+ {
+ SerialInput->scanf("R%f\r", &distance);
+ distance = distance * unitfactor;
+ }
+ else
+ {
+ distance = 0;
+ }
+ return distance;
+}
+float USRF_MB1310::query(int mode)
+{
+ switch (mode)
+ {
+ case 0:
+ if (AnalogInput != 0)
+ {
+ distance = AnalogInput->read() * scalingfactor;
+ }
+ else
+ {
+ distance = 0;
+ }
+ break;
+ case 1:
+ if ((SerialInput != 0) && (SerialInput->readable()))
+ {
+ SerialInput->scanf("R%f\r", &distance);
+ distance = distance * unitfactor;
+ }
+ else
+ {
+ distance = 0;
+ }
+ break;
+ }
+ return distance;
+}
+
+USRF_MB1310& USRF_MB1310::operator =(float assignment)
+{
+ unitfactor = assignment;
+ *this = assignment;
+ return *this;
+}
+
+USRF_MB1310::operator float()
+{
+ if (AnalogInput != 0)
+ {
+ distance = AnalogInput->read() * scalingfactor;
+ }
+ else if ((SerialInput != 0) && (SerialInput->readable()))
+ {
+ SerialInput->scanf("R%f\r", &distance);
+ distance = distance * unitfactor;
+ }
+ else
+ {
+ distance = 0;
+ }
+ return distance;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/USRF_MB1310.h Wed Jun 02 21:40:10 2010 +0000
@@ -0,0 +1,47 @@
+#ifndef USRF_MB1310Library
+#define USRF_MB1310Library
+
+#include "mbed.h"
+
+class USRF_MB1310
+{
+ private:
+ AnalogIn* AnalogInput;
+ DigitalOut* SerialOutput;
+ Serial* SerialInput;
+ float operatingvoltage;
+ float unitfactor;
+ float scalingfactor;
+ float distance;
+
+ public:
+ USRF_MB1310(PinName, PinName, PinName);
+ //analog input, serial output, serial input; specify NC if not used
+ void setoperatingvoltage(float);
+ //sets expected operating voltage of scaling functions;
+ //user responsibility to ensure operating voltage between 3.3V and 5V;
+ //default is 3.3V
+ void selectunit(int);
+ //argument sets unit scaling factor;
+ //0 for cm, 1 for m, 2 for in, 3 for ft;
+ //default is 0
+ void requestserialquery();
+ //this tells the device to prepare a serial reading;
+ //must be done at least 99 ms before the query is read
+ float query();
+ float query(int);
+ //get a reading from the device by the mode specified in the argument;
+ //0 for analog, 1 for serial; requestserialquery() must be called at least
+ //99ms before query(1) can be called, otherwise it will return 0;
+ //overloaded so default no argument is an analog reading
+ USRF_MB1310& operator =(float);
+ //can be used for shorthand for unit selection or for setting non included units;
+ //the output in cm will be multiplied by the assigned float;
+ //ex: "USRF_MB1310object = 10 will convert units to mm
+ operator float();
+ //can be used as shorthand for taking an analog reading;
+ //ex: "float reading = USRF_MB1310object;"
+ //NOTE: STILL UNDER DEVELOPMENT, PLEASE TEST
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Jun 02 21:40:10 2010 +0000
@@ -0,0 +1,25 @@
+#include "mbed.h"
+#include "Servo.h"
+#include "USRF_MB1310.h"
+
+//this file is meant for testing
+
+DigitalOut myled(LED1);
+ Servo test(p23);
+ int check;
+
+
+int main()
+{
+
+ test = 234;
+ check = test;
+ while(1)
+ {
+ myled = 1;
+ wait(0.2);
+ myled = 0;
+ wait(0.2);
+ }//blink an led
+
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jun 02 21:40:10 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e6be4cd80aad