[ES] Finished 3.2 and 3.3

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
Jim Martens 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
// adjust them when you use other pins
@ -12,6 +12,9 @@ Servo ourServo;
// don't touch them
long rc = 1049999;
// borders
int currentMax = 25;
/**
* Setup function for initial setup code
*/
@ -28,19 +31,43 @@ void setup() {
* Loop function for main code
*/
void loop() {
for (int i = 90; i <= 180; ++i) {
ourServo.write(i);
delay(10);
while (currentMax < 70) {
move();
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);
for (int i = 180; i >= 0; --i) {
ourServo.write(i);
delay(10);
}
delay(1000);
for (int i = 0; i <= 90; ++i) {
ourServo.write(i);
delay(10);
}
delay(2000);
}
void move() {
int currentPos = 90;
int currentMin = 90 - currentMax;
for (int i = 0; i <= currentMax; ++i) {
ourServo.write(90 + i);
currentPos = 90 + i;
delay(10);
}
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
// adjust them when you use other pins
// hardware pins
int az = 50;
int xout = A1;
int zout = A3;
int vref = A0;
int xout = A4;
int zout = A2;
int vref = A3;
int ledPin = 13;
// servo
@ -19,9 +19,9 @@ int servoPin = 11;
long rc = 1049999;
// flags for servo
bool cwMaxReached = false;
bool ccwMaxReached = false;
bool lightLED = false;
bool volatile cwMaxReached = false;
bool volatile ccwMaxReached = false;
bool volatile lightLED = false;
bool volatile read_ready = false;
@ -29,36 +29,19 @@ bool volatile read_ready = false;
* Calculates the servo position.
*
* @param int currentServoPos
* @param int rotationX
* @param int rotationZ
*/
int calculate_new_servo_pos(int currentServoPos, int rotationX) {
int newServoPos = currentServoPos;
int calculate_new_servo_pos(int currentServoPos, int rotationZ) {
int newServoPos = currentServoPos + rotationZ;
if (!cwMaxReached && !ccwMaxReached) {
// clockwise rotation from 90 to 180
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;
if (newServoPos > 159) {
newServoPos = 159;
cwMaxReached = true;
lightLED = true;
}
if (newServoPos < 0) {
newServoPos = 0;
if (newServoPos < 25) {
newServoPos = 25;
ccwMaxReached = true;
lightLED = true;
}
@ -74,17 +57,17 @@ void setup() {
pmc_enable_periph_clk(ID_TC1);
// configure hardware timer
TC_Configure(TC1, 0, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2) ;
TC_SetRC(TC1, 0, rc);
TC_Configure(TC0, 1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2) ;
TC_SetRC(TC0, 1, rc);
TC1->TC_CHANNEL[0].TC_IER=TC_IER_CPCS; // IER = interrupt enable register
TC1->TC_CHANNEL[0].TC_IDR=~TC_IER_CPCS;
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(TC1, 0);
TC_Start(TC0, 1);
// Configure pins
pinMode(ledPin, OUTPUT);
@ -104,15 +87,24 @@ void setup() {
*/
void loop() {
if (read_ready) {
int xAxis = analogRead(xout);
int zAxis = analogRead(zout);
int ref = analogRead(vref);
double differenceXRef = xAxis - ref;
double rotationX = differenceXRef / 9.1;
double differenceZRef = zAxis - ref;
double rotationZ = differenceZRef / 9.1;
Serial.print("rotationZ: ");
Serial.println(rotationZ);
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) {
digitalWrite(ledPin, HIGH);
delay(10);
@ -127,9 +119,9 @@ void loop() {
digitalWrite(ledPin, LOW);
lightLED = false;
}
ourServo.write(newServoPos);
ourServo.write(newServoPos + 1);
read_ready = false;
}
}
}
/**
@ -138,6 +130,6 @@ void loop() {
void TC1_Handler()
{
// request static for some magic behind the curtain
TC_GetStatus(TC1, 0);
TC_GetStatus(TC0, 1);
read_ready = true;
}