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:
@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user