Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added sensors example apps

  • Loading branch information...
commit cc59c53852fa513a0201cfc84fa9962b82414f63 1 parent 3f8b0a8
@svdgraaf authored
View
50 engine/engines/applet/engines.cpp
@@ -95,52 +95,4 @@ void foo()
}
}
cmd = "";
-}
-
-//
-// Serial.println("got nothing, full stop");
-// // full stop
-// keyUp = false;
-// w = false;
-// a = false;
-// s = false;
-// d = false;
-// analogWrite(E1, 0);
-// analogWrite(E2, 0);
-//
-// // if forward
-// if(w)
-// {
-// // Serial.println("w active");
-// digitalWrite(M1, HIGH);
-// digitalWrite(M2, HIGH);
-// analogWrite(E1, 255);
-// analogWrite(E2, 255);
-// }
-// else if(s)
-// {
-// // Serial.println("s active");
-// digitalWrite(M1, LOW);
-// digitalWrite(M2, LOW);
-// analogWrite(E1, 255);
-// analogWrite(E2, 255);
-// }
-// else
-// {
-// // Serial.println("s/w not active");
-// analogWrite(E1, 0);
-// analogWrite(E2, 0);
-// }
-//
-// if(a)
-// {
-// // Serial.println("a active");
-// analogWrite(E1, 255);
-// analogWrite(E2, 64);
-// }
-// else if(d)
-// {
-// // Serial.println("d active");
-// analogWrite(E1, 64);
-// analogWrite(E2, 255);
-// }
+}
View
85 engine/engines/engines.ino
@@ -8,18 +8,24 @@ int M2 = 4;
int piece = 0;
String cmd = "";
bool reading = false;
+bool back = false;
+unsigned long lastbeep;
void setup()
{
+ lastbeep = 0;
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
+ pinMode(12, OUTPUT);
Serial.begin(9600);
Serial.println("start");
+ digitalWrite(12, HIGH);
}
void loop()
-{
+{
+ beep();
while (Serial.available() > 0) {
char input = Serial.read();
if (input == '[' && reading == false)
@@ -39,11 +45,24 @@ void loop()
}
}
+void beep()
+{
+ if(back == true)
+ {
+ if(lastbeep < millis() - 1000)
+ {
+ tone(8, 440, 1000/4);
+ lastbeep = millis();
+ }
+ }
+}
+
void foo()
{
Serial.println(cmd);
- if(cmd == "0|0|0|0")
+ if(cmd == "0|0|0|0" or cmd == "1|0|1|0")
{
+ back = false;
// full stop
Serial.println("full stop");
analogWrite(E1, 0);
@@ -55,15 +74,17 @@ void foo()
if(cmd == "1|0|0|0")
{
// forward!
+ back = false;
Serial.println("forward");
digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 255);
analogWrite(E2, 255);
}
- if(cmd == '0|0|1|0')
+ if(cmd == "0|0|1|0")
{
// forward!
+ back = true;
Serial.println("backwards");
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
@@ -71,25 +92,69 @@ void foo()
analogWrite(E2, 255);
}
- if(cmd == '1|1|0|0')
+ if(cmd == "0|0|0|1")
{
- // left forward
- Serial.println("left forward");
+ back = false;
+ Serial.println("left around axis");
+ digitalWrite(M1, LOW);
+ digitalWrite(M2, HIGH);
+ analogWrite(E1, 255);
+ analogWrite(E2, 255);
+ }
+
+ if(cmd == "0|1|0|0")
+ {
+ back = false;
+ Serial.println("right around axis");
digitalWrite(M1, HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 255);
+ analogWrite(E2, 255);
+ }
+
+ if(cmd == "0|1|1|0")
+ {
+ back = true;
+ // left backward
+ Serial.println("left backward");
+ digitalWrite(M1, LOW);
+ digitalWrite(M2, LOW);
+ analogWrite(E1, 255);
+ analogWrite(E2, 128);
+ }
+
+ if(cmd == "0|0|1|1")
+ {
+ back = true;
+ // right backward
+ Serial.println("right backward");
+ digitalWrite(M1, LOW);
+ digitalWrite(M2, LOW);
+ analogWrite(E1, 128);
analogWrite(E2, 255);
}
+
+ if(cmd == "1|1|0|0")
+ {
+ back = false;
+ // left forward
+ Serial.println("left forward");
+ digitalWrite(M1, HIGH);
+ digitalWrite(M2, HIGH);
+ analogWrite(E1, 255);
+ analogWrite(E2, 128);
+ }
- if(cmd == '1|0|0|1')
+ if(cmd == "1|0|0|1")
{
+ back = false;
// left forward
Serial.println("right forward");
- digitalWrite(M1, LOW);
+ digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
- analogWrite(E1, 255);
+ analogWrite(E1, 128);
analogWrite(E2, 255);
}
}
cmd = "";
-}
+}
View
10 engine/engines/ps3.py
@@ -10,9 +10,9 @@
j = pygame.joystick.Joystick(0)
j.init()
-s = serial.Serial('/dev/tty.SLAB_USBtoUART')
+# s = serial.Serial('/dev/tty.SLAB_USBtoUART')
# s = serial.Serial('/dev/tty.usbmodemfa141')
-# s = serial.Serial('/dev/tty.usbmodemfd131')
+s = serial.Serial('/dev/tty.usbmodemfd131')
pygame.init()
# start by sending full stop
@@ -28,11 +28,11 @@
# check left joystick
if j.get_axis(0) < -0.5:
- a = 1
- d = 0
- elif j.get_axis(0) > 0.5:
a = 0
d = 1
+ elif j.get_axis(0) > 0.5:
+ a = 1
+ d = 0
else:
a = 0
d = 0
View
159 sensors/gyro/sketch_mar06a/sketch_mar06a.ino
@@ -0,0 +1,159 @@
+#include <Wire.h>
+
+//This is a list of registers in the ITG-3200. Registers are parameters that determine how the sensor will behave, or they can hold data that represent the
+//sensors current status.
+//To learn more about the registers on the ITG-3200, download and read the datasheet.
+char WHO_AM_I = 0x00;
+char SMPLRT_DIV= 0x15;
+char DLPF_FS = 0x16;
+char GYRO_XOUT_H = 0x1D;
+char GYRO_XOUT_L = 0x1E;
+char GYRO_YOUT_H = 0x1F;
+char GYRO_YOUT_L = 0x20;
+char GYRO_ZOUT_H = 0x21;
+char GYRO_ZOUT_L = 0x22;
+
+//This is a list of settings that can be loaded into the registers.
+//DLPF, Full Scale Register Bits
+//FS_SEL must be set to 3 for proper operation
+//Set DLPF_CFG to 3 for 1kHz Fint and 42 Hz Low Pass Filter
+char DLPF_CFG_0 = 1<<0;
+char DLPF_CFG_1 = 1<<1;
+char DLPF_CFG_2 = 1<<2;
+char DLPF_FS_SEL_0 = 1<<3;
+char DLPF_FS_SEL_1 = 1<<4;
+
+//I2C devices each have an address. The address is defined in the datasheet for the device. The ITG-3200 breakout board can have different address depending on how
+//the jumper on top of the board is configured. By default, the jumper is connected to the VDD pin. When the jumper is connected to the VDD pin the I2C address
+//is 0x69.
+char itgAddress = 0x69;
+
+void setup()
+{
+ //Create a serial connection using a 9600bps baud rate.
+ Serial.begin(9600);
+
+ //Initialize the I2C communication. This will set the Arduino up as the 'Master' device.
+ Wire.begin();
+
+ //Read the WHO_AM_I register and print the result
+ char id=0;
+ id = itgRead(itgAddress, 0x00);
+ Serial.print("ID: ");
+ Serial.println(id, HEX);
+
+ //Configure the gyroscope
+ //Set the gyroscope scale for the outputs to +/-2000 degrees per second
+ itgWrite(itgAddress, DLPF_FS, (DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0));
+ //Set the sample rate to 100 hz
+ itgWrite(itgAddress, SMPLRT_DIV, 9);
+}
+
+ //The loop section of the sketch will read the X,Y and Z output rates from the gyroscope and output them in the Serial Terminal
+void loop()
+{
+ //Create variables to hold the output rates.
+ int xRate, yRate, zRate;
+
+ //Read the x,y and z output rates from the gyroscope.
+ xRate = readX();
+ yRate = readY();
+ zRate = readZ();
+
+ //Print the output rates to the terminal, seperated by a TAB character.
+ Serial.print(xRate);
+ Serial.print('\t');
+ Serial.print(yRate);
+ Serial.print('\t');
+ Serial.println(zRate);
+
+ //Wait 10ms before reading the values again. (Remember, the output rate was set to 100hz and 1reading per 10ms = 100hz.)
+ delay(10);
+}
+
+//Parameters:
+// char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
+// char registerAddress: The address of the register on the sensor that should be written to.
+// char data: The value to be written to the specified register.
+void itgWrite(char address, char registerAddress, char data)
+{
+ //Initiate a communication sequence with the desired i2c device
+ Wire.beginTransmission(address);
+ //Tell the I2C address which register we are writing to
+ Wire.write(registerAddress);
+ //Send the value to write to the specified register
+ Wire.write(data);
+ //End the communication sequence
+ Wire.endTransmission();
+}
+
+//This function will read the data from a specified register on the ITG-3200 and return the value.
+//Parameters:
+// char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
+// char registerAddress: The address of the register on the sensor that should be read
+//Return:
+// unsigned char: The value currently residing in the specified register
+unsigned char itgRead(char address, char registerAddress)
+{
+ //This variable will hold the contents read from the i2c device.
+ unsigned char data=0;
+
+ //Send the register address to be read.
+ Wire.beginTransmission(address);
+ //Send the Register Address
+ Wire.write(registerAddress);
+ //End the communication sequence.
+ Wire.endTransmission();
+
+ //Ask the I2C device for data
+ Wire.beginTransmission(address);
+ Wire.requestFrom(address, 1);
+
+ //Wait for a response from the I2C device
+ if(Wire.available()){
+ //Save the data sent from the I2C device
+ data = Wire.read();
+ }
+
+ //End the communication sequence.
+ Wire.endTransmission();
+
+ //Return the data read during the operation
+ return data;
+}
+
+//This function is used to read the X-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
+//NOTE: This value is NOT in degrees per second.
+//Usage: int xRate = readX();
+int readX(void)
+{
+ int data=0;
+ data = itgRead(itgAddress, GYRO_XOUT_H)<<8;
+ data |= itgRead(itgAddress, GYRO_XOUT_L);
+
+ return data;
+}
+
+//This function is used to read the Y-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
+//NOTE: This value is NOT in degrees per second.
+//Usage: int yRate = readY();
+int readY(void)
+{
+ int data=0;
+ data = itgRead(itgAddress, GYRO_YOUT_H)<<8;
+ data |= itgRead(itgAddress, GYRO_YOUT_L);
+
+ return data;
+}
+
+//This function is used to read the Z-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
+//NOTE: This value is NOT in degrees per second.
+//Usage: int zRate = readZ();
+int readZ(void)
+{
+ int data=0;
+ data = itgRead(itgAddress, GYRO_ZOUT_H)<<8;
+ data |= itgRead(itgAddress, GYRO_ZOUT_L);
+
+ return data;
+}
View
193 sensors/pressure/pressure.ino
@@ -0,0 +1,193 @@
+#include <Wire.h>
+
+// From the datasheet the BMP module address LSB distinguishes
+// between read (1) and write (0) operations, corresponding to
+// address 0xEF (read) and 0xEE (write).
+// shift the address 1 bit right (0xEF or 0xEE), the Wire library only needs the 7
+// most significant bits for the address 0xEF >> 1 = 0x77
+// 0xEE >> 1 = 0x77
+
+int I2C_ADDRESS = 0x77; // sensor address
+
+// oversampling setting
+// 0 = ultra low power
+// 1 = standard
+// 2 = high
+// 3 = ultra high resolution
+const unsigned char oversampling_setting = 3; //oversampling for measurement
+const unsigned char pressure_conversiontime[4] = {
+ 5, 8, 14, 26 }; // delays for oversampling settings 0, 1, 2 and 3
+
+// sensor registers from the BOSCH BMP085 datasheet
+int ac1;
+int ac2;
+int ac3;
+unsigned int ac4;
+unsigned int ac5;
+unsigned int ac6;
+int b1;
+int b2;
+int mb;
+int mc;
+int md;
+
+// variables to keep the values
+int temperature = 0;
+long pressure = 0;
+
+
+void setup()
+{
+ Serial.begin(9600);
+ Wire.begin();
+ getCalibrationData();
+}
+
+void loop()
+{
+ readSensor();
+ Serial.print("Temperature: ");
+ Serial.print(temperature,DEC);
+ Serial.print(" Pressure: ");
+ Serial.println(pressure,DEC);
+ delay(100);
+}
+
+
+// Below there are the utility functions to get data from the sensor.
+
+// read temperature and pressure from sensor
+void readSensor() {
+ int ut= readUT();
+ long up = readUP();
+ long x1, x2, x3, b3, b5, b6, p;
+ unsigned long b4, b7;
+
+ //calculate true temperature
+ x1 = ((long)ut - ac6) * ac5 >> 15;
+ x2 = ((long) mc << 11) / (x1 + md);
+ b5 = x1 + x2;
+ temperature = (b5 + 8) >> 4;
+
+ //calculate true pressure
+ b6 = b5 - 4000;
+ x1 = (b2 * (b6 * b6 >> 12)) >> 11;
+ x2 = ac2 * b6 >> 11;
+ x3 = x1 + x2;
+ b3 = (((int32_t) ac1 * 4 + x3)<<oversampling_setting + 2) >> 2;
+ x1 = ac3 * b6 >> 13;
+ x2 = (b1 * (b6 * b6 >> 12)) >> 16;
+ x3 = ((x1 + x2) + 2) >> 2;
+ b4 = (ac4 * (uint32_t) (x3 + 32768)) >> 15;
+ b7 = ((uint32_t) up - b3) * (50000 >> oversampling_setting);
+ p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
+
+ x1 = (p >> 8) * (p >> 8);
+ x1 = (x1 * 3038) >> 16;
+ x2 = (-7357 * p) >> 16;
+ pressure = p + ((x1 + x2 + 3791) >> 4);
+}
+
+// read uncompensated temperature value
+unsigned int readUT() {
+ writeRegister(0xf4,0x2e);
+ delay(5); // the datasheet suggests 4.5 ms
+ return readIntRegister(0xf6);
+}
+
+// read uncompensated pressure value
+long readUP() {
+ writeRegister(0xf4,0x34+(oversampling_setting<<6));
+ delay(pressure_conversiontime[oversampling_setting]);
+
+ unsigned char msb, lsb, xlsb;
+ Wire.beginTransmission(I2C_ADDRESS);
+ Wire.write(0xf6); // register to read
+ Wire.endTransmission();
+
+ Wire.requestFrom(I2C_ADDRESS, 3); // request three bytes
+ while(!Wire.available()); // wait until data available
+ msb = Wire.read();
+ while(!Wire.available()); // wait until data available
+ lsb |= Wire.read();
+ while(!Wire.available()); // wait until data available
+ xlsb |= Wire.read();
+ return (((long)msb<<16) | ((long)lsb<<8) | ((long)xlsb)) >>(8-oversampling_setting);
+}
+
+void getCalibrationData() {
+ Serial.println("Reading Calibration Data");
+ ac1 = readIntRegister(0xAA);
+ Serial.print("AC1: ");
+ Serial.println(ac1,DEC);
+ ac2 = readIntRegister(0xAC);
+ Serial.print("AC2: ");
+ Serial.println(ac2,DEC);
+ ac3 = readIntRegister(0xAE);
+ Serial.print("AC3: ");
+ Serial.println(ac3,DEC);
+ ac4 = readIntRegister(0xB0);
+ Serial.print("AC4: ");
+ Serial.println(ac4,DEC);
+ ac5 = readIntRegister(0xB2);
+ Serial.print("AC5: ");
+ Serial.println(ac5,DEC);
+ ac6 = readIntRegister(0xB4);
+ Serial.print("AC6: ");
+ Serial.println(ac6,DEC);
+ b1 = readIntRegister(0xB6);
+ Serial.print("B1: ");
+ Serial.println(b1,DEC);
+ b2 = readIntRegister(0xB8);
+ Serial.print("B2: ");
+ Serial.println(b1,DEC);
+ mb = readIntRegister(0xBA);
+ Serial.print("MB: ");
+ Serial.println(mb,DEC);
+ mc = readIntRegister(0xBC);
+ Serial.print("MC: ");
+ Serial.println(mc,DEC);
+ md = readIntRegister(0xBE);
+ Serial.print("MD: ");
+ Serial.println(md,DEC);
+}
+
+void writeRegister(unsigned char r, unsigned char v)
+{
+ Wire.beginTransmission(I2C_ADDRESS);
+ Wire.write(r);
+ Wire.write(v);
+ Wire.endTransmission();
+}
+
+// read a 16 bit register
+int readIntRegister(unsigned char r)
+{
+ unsigned char msb, lsb;
+ Wire.beginTransmission(I2C_ADDRESS);
+ Wire.write(r); // register to read
+ Wire.endTransmission();
+
+ Wire.requestFrom(I2C_ADDRESS, 2); // request two bytes
+ while(!Wire.available()); // wait until data available
+ msb = Wire.read();
+ while(!Wire.available()); // wait until data available
+ lsb = Wire.read();
+ return (((int)msb<<8) | ((int)lsb));
+}
+
+// read an 8 bit register
+/*
+unsigned char readRegister(unsigned char r)
+{
+ unsigned char v;
+ Wire.beginTransmission(I2C_ADDRESS);
+ Wire.send(r); // register to read
+ Wire.endTransmission();
+
+ Wire.requestFrom(I2C_ADDRESS, 1); // request a byte
+ while(!Wire.available()); // wait until data available
+ v = Wire.receive();
+ return v;
+}
+*/
Please sign in to comment.
Something went wrong with that request. Please try again.