From bbbf1c47ffc452f1fa94684c2064f7b7ace0f58b Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Tue, 16 Jun 2015 16:07:39 +0200 Subject: [PATCH] [ES] Aufgabenblatt 4 beendet Signed-off-by: Jim Martens --- es/blatt4/uebung4-2/uebung4-2.ino | 33 ++++--- es/blatt4/uebung4-3/uebung4-3.ino | 150 ++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 es/blatt4/uebung4-3/uebung4-3.ino diff --git a/es/blatt4/uebung4-2/uebung4-2.ino b/es/blatt4/uebung4-2/uebung4-2.ino index 53b74bd..d8ca6fd 100644 --- a/es/blatt4/uebung4-2/uebung4-2.ino +++ b/es/blatt4/uebung4-2/uebung4-2.ino @@ -5,7 +5,6 @@ // hardware pins int ledPin = 13; int analogLevel = 0; - int slaveAddress = 4; /** @@ -14,12 +13,18 @@ int slaveAddress = 4; void setup() { // Configure pins Wire.begin(); - Wire.onReceive(receiveEvent); - + pinMode(ledPin, OUTPUT); + digitalWrite(ledPin, LOW); // initialize serial port Serial.begin(9600); } +void writeResult(bool result) +{ + Serial.print("Result: "); + Serial.println(result ? "on" : "off"); +} + /** * Loop function for main code */ @@ -28,23 +33,21 @@ void loop() { Wire.write(1); Wire.endTransmission(); delay(100); - Wire.beginTransmission(slaveAddress); - Wire.write('r'); - Wire.endTransmission(); + Wire.requestFrom(slaveAddress, 1); + if (Wire.available()) { + int x = Wire.read(); + writeResult((bool) x); + } delay(1900); Wire.beginTransmission(slaveAddress); Wire.write(0); Wire.endTransmission(); delay(100); - Wire.beginTransmission(slaveAddress); - Wire.write('r'); - Wire.endTransmission(); + Wire.requestFrom(slaveAddress, 1); + if (Wire.available()) { + int x = Wire.read(); + writeResult((bool) x); + } delay(1900); } -void receiveEvent(int readBytes) -{ - int x = Wire.read(); - Serial.print("Result: "); - Serial.println(x ? "on" : "off"); -} diff --git a/es/blatt4/uebung4-3/uebung4-3.ino b/es/blatt4/uebung4-3/uebung4-3.ino new file mode 100644 index 0000000..a42cdda --- /dev/null +++ b/es/blatt4/uebung4-3/uebung4-3.ino @@ -0,0 +1,150 @@ +#include +#include +// these variables describe the used hardware pins +// adjust them when you use other pins +// hardware pins +int az = 50; +int xout = A4; +int zout = A2; +int vref = A3; +int ledPin = 13; +int slaveAddress = 4; +// servo +Servo ourServo; +int servoPin = 11; +// used to achieve a 10 Hz frequency +// don't touch them +long rc = 1049999; +// flags for servo +bool volatile cwMaxReached = false; +bool volatile ccwMaxReached = false; +bool volatile lightLED = false; +bool volatile read_ready = false; + +// tmp variables +int volatile zAxis = 0; +int volatile ref = 0; +double volatile differenceZRef = 0; +double volatile rotationZ = 0; +int volatile currentServoPos = 0; +int volatile newServoPos = 0; + +/** +* Calculates the servo position. +* +* @param int currentServoPos +* @param int rotationZ +*/ +int calculate_new_servo_pos(int currentServoPos, int rotationZ) { + int newServoPos = currentServoPos + rotationZ; + // faktor 10 zu gross (weil messung grad/sec + + if (newServoPos > 159) { + newServoPos = 159; + cwMaxReached = true; + lightLED = true; + } + if (newServoPos < 25) { + newServoPos = 25; + ccwMaxReached = true; + lightLED = true; + } + return newServoPos; +} +/** +* Setup function for initial setup code +*/ +void setup() { + Wire.begin(); + + pmc_set_writeprotect(false); + pmc_enable_periph_clk(ID_TC1); + // configure hardware timer + TC_Configure(TC0, 1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2) ; + TC_SetRC(TC0, 1, rc); + TC0->TC_CHANNEL[1].TC_IER = TC_IER_CPCS; // IER = interrupt enable register + TC0->TC_CHANNEL[1].TC_IDR = ~TC_IER_CPCS; + NVIC_ClearPendingIRQ(TC1_IRQn); + NVIC_EnableIRQ(TC1_IRQn); + // start hardware timer + TC_Start(TC0, 1); + + pinMode(ledPin, OUTPUT); + digitalWrite(ledPin, LOW); + ourServo.attach(servoPin); + ourServo.write(90); + // initialize serial port + Serial.begin(9600); +} +/** +* Loop function for main code +*/ +void loop() { + if (read_ready) { + + // berechnung auf 5000/1024 umstellen - was hat volt mit der berechnung zu tun? warscheinlich steht das so im aufgabenblatt ? + Serial.print("rotationZ: "); + Serial.println(rotationZ); + + Serial.print("currentServoPos: "); + Serial.println(currentServoPos); + Serial.print("newServoPos: "); + Serial.println(newServoPos); + /* + if (lightLED) { + blink(); + }*/ + ourServo.write(newServoPos + 1); + read_ready = false; + } +} +/** +* Used to handle the timer. +*/ +void TC1_Handler() +{ + // request static for some magic behind the curtain + TC_GetStatus(TC0, 1); + read_ready = true; + requestValues(); + //zAxis = analogRead(zout); + //ref = analogRead(vref); + //differenceZRef = zAxis - ref; + differenceZRef = (differenceZRef * 5000) / 1024; + rotationZ = (differenceZRef / 9.1); + if (fabs(rotationZ) > 4) { + currentServoPos = ourServo.read(); + newServoPos = calculate_new_servo_pos(currentServoPos, rotationZ / 10); + } +} + +void requestValues() +{ + // request zAxis + Wire.requestFrom(slaveAddress, 4); + int result = 0; + int i = 1; + while (Wire.available()) { + uint8_t x = (uint8_t) Wire.read(); + result = result | ((x) << ((sizeof(int) - i)*8)); + i++; + } + differenceZRef = (double) result; + Serial.print("Got: "); + Serial.println(result); +} + +void blink(){ + digitalWrite(ledPin, HIGH); + delay(10); + digitalWrite(ledPin, LOW); + delay(10); + digitalWrite(ledPin, HIGH); + delay(10); + digitalWrite(ledPin, LOW); + delay(10); + digitalWrite(ledPin, HIGH); + delay(10); + digitalWrite(ledPin, LOW); + lightLED = false; +}