Alex Louden
/
Accelerometer
Revision 0:05ddd3ecad81, committed 2009-12-06
- Comitter:
- alex89
- Date:
- Sun Dec 06 12:56:50 2009 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 05ddd3ecad81 Servo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.h Sun Dec 06 12:56:50 2009 +0000 @@ -0,0 +1,87 @@ +/* mbed Microcontroller Library - Servo + * Copyright (c) 2007-2008, sford + */ + +#ifndef MBED_SERVO_H +#define MBED_SERVO_H + +#include "Servo.h" + +#include "mbed.h" +#include "platform.h" + + +namespace mbed { + +/* Class: Servo + * Abstraction on top of PwmOut to control the position of a servo motor + * + * Example: + * > // Continuously sweep the servo through it's full range + * > + * > #include "mbed.h" + * > #include "Servo.h" + * > + * > Servo myServo (p21); + * > int main() { + * > int i; + * > while (1) { + * > for (i=0 ; i<100 ; i++){ + * > myServo = i/100.0; + * > wait (0.01); + * > } + * > for (i=100 ; i>0 ; i--){ + * > myServo = i/100.0; + * > wait (0.01); + * > } + * > } + * >} + */ + + +class Servo : public Base { + +public: + /* Constructor: Servo + * Create a servo object connected to the specified PwmOut pin + * + * Variables: + * pin - PwmOut pin to connect to + */ + Servo(PinName pin, const char* = NULL); + + /* Function: write + * Set the servo position, normalised to it's full range + * + * Variables: + * percent - A normalised number 0.0-1.0 to represent the full range. + */ + void write(float percent); + /* Function: read + * Read the servo motors current position + * + * Variables: + * returns - A normalised number 0.0-1.0 representing the full range. + */ + float read(); + /* Function: operator= + * Shorthand for the write and read functions + */ + Servo& operator= (float percent); + Servo& operator= (Servo& rhs); + operator float(); + +#ifdef MBED_RPC + virtual const struct rpc_method *get_rpc_methods(); + static struct rpc_class *get_rpc_class(); +#endif + +protected: + + PwmOut _pwm; + float _p; +}; + +} + +#endif
diff -r 000000000000 -r 05ddd3ecad81 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Dec 06 12:56:50 2009 +0000 @@ -0,0 +1,175 @@ +#include "mbed.h" + +Serial pc(USBTX, USBRX); // debugging + +DigitalOut enable(p26); // enable pin +I2C i2c(p28, p27); // accelerometer + +const int address = 0x30; + +const int CTRL_REGB = 0x0D; +const int CTRL_REGC = 0x0C; + +const int XOUT_H = 0x00; //x +const int XOUT_L = 0x01; + +const int YOUT_H = 0x02; //y +const int YOUT_L = 0x03; + +const int ZOUT_H = 0x04; //z +const int ZOUT_L = 0x05; + + +//--------------- IC2 helper functions --------------- + +// write value into register regno, return success +bool write_reg(int regno, int value) { + + char data[2] = {regno, value}; + + return i2c.write(address, data, 2) == 0; + +} + +// read value from register regno, return success +bool read_reg(int regno, int *value) { + + char data = regno; + + if (i2c.write(address, &data, 1) == 0 && i2c.read(address, &data, 1) == 0){ + *value = data; + return true; + } + return false; + +} + +// read complete value of X axis, return it or -1 on failure +int read_x() { + + int low, high; + + if ( read_reg(XOUT_H, &high) && read_reg(XOUT_L, &low) ) + return high<<8 | low; + else + return -1; +} + +// read complete value of Y axis, return it or -1 on failure +int read_y() { + + int low, high; + + if ( read_reg(YOUT_H, &high) && read_reg(YOUT_L, &low) ) + return high<<8 | low; + else + return -1; +} + +// read complete value of Z axis, return it or -1 on failure +int read_z() { + + int low, high; + + if ( read_reg(ZOUT_H, &high) && read_reg(ZOUT_L, &low) ) + return high<<8 | low; + else + return -1; +} + +void accData(){ + + enable = 1; + + write_reg(CTRL_REGB, 0xC2); + write_reg(CTRL_REGC, 0x00); + + for (int i = 0; i < 1000; i++){ + + printf("%d,%d,%d \n", read_x(), read_y(), read_z()); + + wait(0.05); + + } + + enable = 0; + +} + +void accLEDs(){ + + DigitalOut x0(p20); //red + DigitalOut x1(p19); //green + DigitalOut x2(p16); //green + DigitalOut x3(p15); //green + DigitalOut x4(p13); //green + DigitalOut x5(p11); //red + + enable = 1; + + write_reg(CTRL_REGB, 0xC2); //start + write_reg(CTRL_REGC, 0x00); + + for (int i = 0; i < 1000; i++){ + + double x = double (read_x() - 32768)/9000; //normalise to around 0g/1g + double y = double (read_y() - 32768)/9000; + double z = double (read_z() - 32768)/9000; + + //pc.printf("%.2f, %.2f, %.2f \n", x, y, z); + //pc.printf("%.2f \n", x); + + x0 = 0; x1 = 0; x2 = 0; x3 = 0; x4 = 0; x5 = 0; + + int intx = (x+0.1)*5; //scale to an integer from 0 to 5 + + pc.printf("%d \n", intx); + + switch (intx) { + + case 0: + x0 = 1; + break; + case 1: + x1 = 1; + break; + case 2: + x2 = 1; + break; + case 3: + x3 = 1; + break; + case 4: + x4 = 1; + break; + case 5: + x5 = 1; + break; + + default: + break; + } + + wait(0.1); + + } + + x0 = 0; x1 = 0; x2 = 0; x3 = 0; x4 = 0; x5 = 0; + + enable = 0; + +} + + +//-------------------- Main ------------------- + +int main() { + + pc.printf("Program started\n"); + + //accData(); + + accLEDs(); + + pc.printf("Program complete\n\n"); +}
diff -r 000000000000 -r 05ddd3ecad81 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Dec 06 12:56:50 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/32af5db564d4