1
0
mirror of https://github.com/2martens/uni.git synced 2026-05-06 11:26:25 +02:00

[ES] Finished 3.2 and 3.3

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2015-05-12 15:43:33 +02:00
parent e3ab682a93
commit cfc5de6692
2 changed files with 77 additions and 58 deletions

View File

@ -1,4 +1,4 @@
#include <Servo> #include <Servo.h>
// these variables describe the used hardware pins // these variables describe the used hardware pins
// adjust them when you use other pins // adjust them when you use other pins
@ -12,6 +12,9 @@ Servo ourServo;
// don't touch them // don't touch them
long rc = 1049999; long rc = 1049999;
// borders
int currentMax = 25;
/** /**
* Setup function for initial setup code * Setup function for initial setup code
*/ */
@ -28,19 +31,43 @@ void setup() {
* Loop function for main code * Loop function for main code
*/ */
void loop() { void loop() {
for (int i = 90; i <= 180; ++i) { while (currentMax < 70) {
ourServo.write(i); move();
delay(10); Serial.print("currentMax: ");
Serial.println(currentMax);
if (currentMax < 70) {
currentMax += 5;
}
} }
currentMax = 65;
while (currentMax > 20) {
move();
Serial.print("currentMax: ");
Serial.println(currentMax);
if (currentMax > 20) {
currentMax -= 5;
}
}
currentMax = 25;
delay(1000); delay(1000);
for (int i = 180; i >= 0; --i) { }
ourServo.write(i);
delay(10); void move() {
} int currentPos = 90;
delay(1000); int currentMin = 90 - currentMax;
for (int i = 0; i <= 90; ++i) { for (int i = 0; i <= currentMax; ++i) {
ourServo.write(i); ourServo.write(90 + i);
delay(10); currentPos = 90 + i;
} delay(10);
delay(2000); }
for (int i = currentPos; i >= currentMin; --i) {
ourServo.write(i);
currentPos = i;
delay(10);
}
for (int i = currentPos; i <= 90; ++i) {
ourServo.write(i);
currentPos = i;
delay(10);
}
} }

View File

@ -1,13 +1,13 @@
#include <Servo> #include <Servo.h>
// these variables describe the used hardware pins // these variables describe the used hardware pins
// adjust them when you use other pins // adjust them when you use other pins
// hardware pins // hardware pins
int az = 50; int az = 50;
int xout = A1; int xout = A4;
int zout = A3; int zout = A2;
int vref = A0; int vref = A3;
int ledPin = 13; int ledPin = 13;
// servo // servo
@ -19,9 +19,9 @@ int servoPin = 11;
long rc = 1049999; long rc = 1049999;
// flags for servo // flags for servo
bool cwMaxReached = false; bool volatile cwMaxReached = false;
bool ccwMaxReached = false; bool volatile ccwMaxReached = false;
bool lightLED = false; bool volatile lightLED = false;
bool volatile read_ready = false; bool volatile read_ready = false;
@ -29,36 +29,19 @@ bool volatile read_ready = false;
* Calculates the servo position. * Calculates the servo position.
* *
* @param int currentServoPos * @param int currentServoPos
* @param int rotationX * @param int rotationZ
*/ */
int calculate_new_servo_pos(int currentServoPos, int rotationX) { int calculate_new_servo_pos(int currentServoPos, int rotationZ) {
int newServoPos = currentServoPos; int newServoPos = currentServoPos + rotationZ;
if (!cwMaxReached && !ccwMaxReached) { if (newServoPos > 159) {
// clockwise rotation from 90 to 180 newServoPos = 159;
newServoPos = currentServoPos + rotationX;
}
else if (cwMaxReached && !ccwMaxReached) {
// counter clockwise rotation from 180 to 0
newServoPos = currentServoPos - rotationX;
}
else if (cwMaxReached && ccwMaxReached) {
// clockwise rotation from 0 to 90
newServoPos = currentServoPos + rotationX;
if (newServoPos >= 90) {
cwMaxReached = false;
ccwMaxReached = false;
}
}
if (newServoPos > 180) {
newServoPos = 180;
cwMaxReached = true; cwMaxReached = true;
lightLED = true; lightLED = true;
} }
if (newServoPos < 0) { if (newServoPos < 25) {
newServoPos = 0; newServoPos = 25;
ccwMaxReached = true; ccwMaxReached = true;
lightLED = true; lightLED = true;
} }
@ -74,17 +57,17 @@ void setup() {
pmc_enable_periph_clk(ID_TC1); pmc_enable_periph_clk(ID_TC1);
// configure hardware timer // configure hardware timer
TC_Configure(TC1, 0, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2) ; TC_Configure(TC0, 1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2) ;
TC_SetRC(TC1, 0, rc); TC_SetRC(TC0, 1, rc);
TC1->TC_CHANNEL[0].TC_IER=TC_IER_CPCS; // IER = interrupt enable register TC0->TC_CHANNEL[1].TC_IER=TC_IER_CPCS; // IER = interrupt enable register
TC1->TC_CHANNEL[0].TC_IDR=~TC_IER_CPCS; TC0->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS;
NVIC_ClearPendingIRQ(TC1_IRQn); NVIC_ClearPendingIRQ(TC1_IRQn);
NVIC_EnableIRQ(TC1_IRQn); NVIC_EnableIRQ(TC1_IRQn);
// start hardware timer // start hardware timer
TC_Start(TC1, 0); TC_Start(TC0, 1);
// Configure pins // Configure pins
pinMode(ledPin, OUTPUT); pinMode(ledPin, OUTPUT);
@ -104,15 +87,24 @@ void setup() {
*/ */
void loop() { void loop() {
if (read_ready) { if (read_ready) {
int xAxis = analogRead(xout); int zAxis = analogRead(zout);
int ref = analogRead(vref); int ref = analogRead(vref);
double differenceXRef = xAxis - ref; double differenceZRef = zAxis - ref;
double rotationX = differenceXRef / 9.1; double rotationZ = differenceZRef / 9.1;
Serial.print("rotationZ: ");
Serial.println(rotationZ);
int currentServoPos = ourServo.read(); int currentServoPos = ourServo.read();
int newServoPos = calculate_new_servo_pos(currentServoPos, rotationX); int newServoPos = calculate_new_servo_pos(currentServoPos, rotationZ);
Serial.print("currentServoPos: ");
Serial.println(currentServoPos);
Serial.print("newServoPos: ");
Serial.println(newServoPos);
if (lightLED) { if (lightLED) {
digitalWrite(ledPin, HIGH); digitalWrite(ledPin, HIGH);
delay(10); delay(10);
@ -127,9 +119,9 @@ void loop() {
digitalWrite(ledPin, LOW); digitalWrite(ledPin, LOW);
lightLED = false; lightLED = false;
} }
ourServo.write(newServoPos); ourServo.write(newServoPos + 1);
read_ready = false; read_ready = false;
} }
} }
/** /**
@ -138,6 +130,6 @@ void loop() {
void TC1_Handler() void TC1_Handler()
{ {
// request static for some magic behind the curtain // request static for some magic behind the curtain
TC_GetStatus(TC1, 0); TC_GetStatus(TC0, 1);
read_ready = true; read_ready = true;
} }