mirror of
https://github.com/2martens/uni.git
synced 2026-05-06 19:36:26 +02:00
Merge branch 'master' of github.com:frmwrk123/uni
Conflicts: es/blatt3/uebung3-4/uebung3-4.ino es/blatt3/uebung3-5/uebung3-5.ino
This commit is contained in:
@ -4,8 +4,8 @@
|
|||||||
int az = 50;
|
int az = 50;
|
||||||
|
|
||||||
int xout = A1;
|
int xout = A1;
|
||||||
int zout = A3;
|
int zout = A2;
|
||||||
int vref = A0;
|
int vref = A3;
|
||||||
|
|
||||||
// used to achieve a 10 Hz frequency
|
// used to achieve a 10 Hz frequency
|
||||||
// don't touch them
|
// don't touch them
|
||||||
@ -55,8 +55,8 @@ void loop() {
|
|||||||
double differenceXRef = xAxis - ref;
|
double differenceXRef = xAxis - ref;
|
||||||
double differenceZRef = zAxis - ref;
|
double differenceZRef = zAxis - ref;
|
||||||
|
|
||||||
double drehrateX = differenceXRef / 9.1;
|
double drehrateX = ((differenceXRef * 5000) / 1024) / 9.1;
|
||||||
double drehrateZ = differenceZRef / 9.1;
|
double drehrateZ = ((differenceZRef * 5000) / 1024) / 9.1;
|
||||||
|
|
||||||
Serial.print("x:");
|
Serial.print("x:");
|
||||||
Serial.println(xAxis);
|
Serial.println(xAxis);
|
||||||
|
|||||||
@ -1,135 +1,130 @@
|
|||||||
#include <Servo.h>
|
#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 = A4;
|
int xout = A4;
|
||||||
int zout = A2;
|
int zout = A2;
|
||||||
int vref = A3;
|
int vref = A3;
|
||||||
int ledPin = 13;
|
int ledPin = 13;
|
||||||
|
|
||||||
// servo
|
// servo
|
||||||
Servo ourServo;
|
Servo ourServo;
|
||||||
int servoPin = 11;
|
int servoPin = 11;
|
||||||
|
|
||||||
// used to achieve a 10 Hz frequency
|
// used to achieve a 10 Hz frequency
|
||||||
// don't touch them
|
// don't touch them
|
||||||
long rc = 1049999;
|
long rc = 1049999;
|
||||||
|
|
||||||
// flags for servo
|
// flags for servo
|
||||||
bool volatile cwMaxReached = false;
|
bool volatile cwMaxReached = false;
|
||||||
bool volatile ccwMaxReached = false;
|
bool volatile ccwMaxReached = false;
|
||||||
bool volatile lightLED = false;
|
bool volatile lightLED = false;
|
||||||
|
|
||||||
bool volatile read_ready = 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.
|
* Calculates the servo position.
|
||||||
*
|
*
|
||||||
* @param int currentServoPos
|
* @param int currentServoPos
|
||||||
* @param int rotationZ
|
* @param int rotationZ
|
||||||
*/
|
*/
|
||||||
int calculate_new_servo_pos(int currentServoPos, int rotationZ) {
|
int calculate_new_servo_pos(int currentServoPos, int rotationZ) {
|
||||||
int newServoPos = currentServoPos + rotationZ;
|
int newServoPos = currentServoPos + rotationZ;
|
||||||
|
// faktor 10 zu gross (weil messung grad/sec
|
||||||
if (newServoPos > 159) {
|
|
||||||
newServoPos = 159;
|
if (newServoPos > 159) {
|
||||||
cwMaxReached = true;
|
newServoPos = 159;
|
||||||
lightLED = true;
|
cwMaxReached = true;
|
||||||
}
|
lightLED = true;
|
||||||
|
}
|
||||||
if (newServoPos < 25) {
|
if (newServoPos < 25) {
|
||||||
newServoPos = 25;
|
newServoPos = 25;
|
||||||
ccwMaxReached = true;
|
ccwMaxReached = true;
|
||||||
lightLED = true;
|
lightLED = true;
|
||||||
}
|
}
|
||||||
|
return newServoPos;
|
||||||
return newServoPos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup function for initial setup code
|
* Setup function for initial setup code
|
||||||
*/
|
*/
|
||||||
void setup() {
|
void setup() {
|
||||||
pmc_set_writeprotect(false);
|
pmc_set_writeprotect(false);
|
||||||
pmc_enable_periph_clk(ID_TC1);
|
pmc_enable_periph_clk(ID_TC1);
|
||||||
|
// configure hardware timer
|
||||||
// configure hardware timer
|
TC_Configure(TC0, 1, 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(TC0, 1, rc);
|
||||||
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;
|
||||||
TC0->TC_CHANNEL[1].TC_IER=TC_IER_CPCS; // IER = interrupt enable register
|
NVIC_ClearPendingIRQ(TC1_IRQn);
|
||||||
TC0->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS;
|
NVIC_EnableIRQ(TC1_IRQn);
|
||||||
|
// start hardware timer
|
||||||
NVIC_ClearPendingIRQ(TC1_IRQn);
|
TC_Start(TC0, 1);
|
||||||
NVIC_EnableIRQ(TC1_IRQn);
|
// Configure pins
|
||||||
|
pinMode(ledPin, OUTPUT);
|
||||||
// start hardware timer
|
pinMode(az, OUTPUT);
|
||||||
TC_Start(TC0, 1);
|
digitalWrite(az, HIGH);
|
||||||
|
digitalWrite(ledPin, LOW);
|
||||||
// Configure pins
|
ourServo.attach(servoPin);
|
||||||
pinMode(ledPin, OUTPUT);
|
ourServo.write(90);
|
||||||
pinMode(az, OUTPUT);
|
// initialize serial port
|
||||||
digitalWrite(az, HIGH);
|
Serial.begin(9600);
|
||||||
digitalWrite(ledPin, LOW);
|
|
||||||
|
|
||||||
ourServo.attach(servoPin);
|
|
||||||
ourServo.write(90);
|
|
||||||
|
|
||||||
// initialize serial port
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loop function for main code
|
* Loop function for main code
|
||||||
*/
|
*/
|
||||||
void loop() {
|
void loop() {
|
||||||
if (read_ready) {
|
if (read_ready) {
|
||||||
int zAxis = analogRead(zout);
|
|
||||||
int ref = analogRead(vref);
|
// berechnung auf 5000/1024 umstellen - was hat volt mit der berechnung zu tun? warscheinlich steht das so im aufgabenblatt ?
|
||||||
|
Serial.print("rotationZ: ");
|
||||||
double differenceZRef = zAxis - ref;
|
Serial.println(rotationZ);
|
||||||
double rotationZ = differenceZRef / 9.1;
|
|
||||||
|
Serial.print("currentServoPos: ");
|
||||||
Serial.print("rotationZ: ");
|
Serial.println(currentServoPos);
|
||||||
Serial.println(rotationZ);
|
Serial.print("newServoPos: ");
|
||||||
|
Serial.println(newServoPos);
|
||||||
int currentServoPos = ourServo.read();
|
if (lightLED) {
|
||||||
int newServoPos = calculate_new_servo_pos(currentServoPos, rotationZ);
|
blink();
|
||||||
|
}
|
||||||
Serial.print("currentServoPos: ");
|
ourServo.write(newServoPos + 1);
|
||||||
Serial.println(currentServoPos);
|
read_ready = false;
|
||||||
|
}
|
||||||
Serial.print("newServoPos: ");
|
|
||||||
Serial.println(newServoPos);
|
|
||||||
|
|
||||||
if (lightLED) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
ourServo.write(newServoPos + 1);
|
|
||||||
read_ready = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to handle the timer.
|
* Used to handle the timer.
|
||||||
*/
|
*/
|
||||||
void TC1_Handler()
|
void TC1_Handler()
|
||||||
{
|
{
|
||||||
// request static for some magic behind the curtain
|
// request static for some magic behind the curtain
|
||||||
TC_GetStatus(TC0, 1);
|
TC_GetStatus(TC0, 1);
|
||||||
read_ready = true;
|
read_ready = true;
|
||||||
|
zAxis = analogRead(zout);
|
||||||
|
ref = analogRead(vref);
|
||||||
|
differenceZRef = zAxis - ref;
|
||||||
|
differenceZRef = (differenceZRef * 5000) / 1024;
|
||||||
|
rotationZ = (differenceZRef / 9.1);
|
||||||
|
if (fabs(rotationZ) > 5) {
|
||||||
|
currentServoPos = ourServo.read();
|
||||||
|
newServoPos = calculate_new_servo_pos(currentServoPos, rotationZ / 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,31 +24,7 @@ bool volatile ccwMaxReached = false;
|
|||||||
bool volatile lightLED = false;
|
bool volatile lightLED = false;
|
||||||
|
|
||||||
bool volatile read_ready = false;
|
bool volatile read_ready = false;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the servo position.
|
|
||||||
*
|
|
||||||
* @param int currentServoPos
|
|
||||||
* @param int rotationZ
|
|
||||||
*/
|
|
||||||
int calculate_new_servo_pos(int currentServoPos, int rotationZ) {
|
|
||||||
int newServoPos = currentServoPos + rotationZ;
|
|
||||||
|
|
||||||
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
|
* Setup function for initial setup code
|
||||||
@ -88,27 +64,17 @@ void setup() {
|
|||||||
*/
|
*/
|
||||||
void loop() {
|
void loop() {
|
||||||
if (Serial.available() > 0) {
|
if (Serial.available() > 0) {
|
||||||
char command[8];
|
String command;
|
||||||
char currentChar;
|
command = Serial.readString();
|
||||||
int i = 0;
|
String ledOn = "LED_on";
|
||||||
bool readable = true;
|
String ledOff = "LED_off";
|
||||||
while (readable) {
|
|
||||||
currentChar = Serial.read();
|
if (command == ledOn) {
|
||||||
readable = (currentChar != -1);
|
digitalWrite(ledPin, HIGH);
|
||||||
if (readable) {
|
}
|
||||||
command[i] = currentChar;
|
else if (command == ledOff) {
|
||||||
i++;
|
digitalWrite(ledPin, LOW);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
command[i] = '\0';
|
|
||||||
|
|
||||||
if (strcmp(command, "LED_on") == 0) {
|
|
||||||
digitalWrite(ledPin, HIGH);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
digitalWrite(ledPin, LOW);
|
|
||||||
}
|
|
||||||
Serial.println(command);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,9 @@
|
|||||||
// hardware pins
|
// hardware pins
|
||||||
int az = 50;
|
int az = 50;
|
||||||
|
|
||||||
|
int xout = A4;
|
||||||
|
int zout = A2;
|
||||||
|
int vref = A3;
|
||||||
int ledPin = 13;
|
int ledPin = 13;
|
||||||
|
|
||||||
// servo
|
// servo
|
||||||
@ -24,53 +27,71 @@ bool volatile read_ready = false;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the new servo position.
|
* Validates the servo position.
|
||||||
*
|
*
|
||||||
* @param long newServoPos
|
* @param int newServoPos
|
||||||
*/
|
*/
|
||||||
int validate_new_servo_pos(long newServoPos) {
|
int validate_new_servo_pos(int newServoPos) {
|
||||||
|
|
||||||
if (newServoPos > 159) {
|
if (newServoPos > 159) {
|
||||||
newServoPos = 159;
|
newServoPos = 159;
|
||||||
cwMaxReached = true;
|
cwMaxReached = true;
|
||||||
lightLED = true;
|
lightLED = true;
|
||||||
|
Serial.println("Interval 25-159");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newServoPos < 25) {
|
if (newServoPos < 25) {
|
||||||
newServoPos = 25;
|
newServoPos = 25;
|
||||||
ccwMaxReached = true;
|
ccwMaxReached = true;
|
||||||
lightLED = true;
|
lightLED = true;
|
||||||
|
Serial.println("Interval 25-159");
|
||||||
}
|
}
|
||||||
|
|
||||||
int validatedServoPos = (int) newServoPos;
|
return newServoPos;
|
||||||
|
|
||||||
return validatedServoPos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
int parse_angle(String command) {
|
||||||
* Parses the angle of the moveTo command.
|
int indexP1 = command.indexOf('(');
|
||||||
*
|
int indexP2 = command.indexOf(')');
|
||||||
* @param char command
|
String command2 = command;
|
||||||
*/
|
command2.remove(indexP1);
|
||||||
long parse_angle(char* command) {
|
if (command2 != "moveTo") {
|
||||||
char* angle;
|
Serial.println("Use moveTo(<angle>)");
|
||||||
|
return ourServo.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
command.remove(indexP2);
|
||||||
|
int angle = command.substring(indexP1 + 1).toInt();
|
||||||
|
|
||||||
angle = strtok(command, "()");
|
return angle;
|
||||||
angle = strtok(NULL, "()");
|
|
||||||
|
|
||||||
return strtol(angle, NULL, 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup function for initial setup code
|
* Setup function for initial setup code
|
||||||
*/
|
*/
|
||||||
void setup() {
|
void setup() {
|
||||||
|
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);
|
||||||
|
|
||||||
// Configure pins
|
// Configure pins
|
||||||
pinMode(ledPin, OUTPUT);
|
pinMode(ledPin, OUTPUT);
|
||||||
pinMode(az, OUTPUT);
|
pinMode(az, OUTPUT);
|
||||||
digitalWrite(az, HIGH);
|
digitalWrite(az, HIGH);
|
||||||
digitalWrite(ledPin, LOW);
|
digitalWrite(ledPin, LOW);
|
||||||
|
|
||||||
ourServo.attach(servoPin);
|
ourServo.attach(servoPin);
|
||||||
ourServo.write(90);
|
ourServo.write(90);
|
||||||
|
|
||||||
@ -83,30 +104,35 @@ void setup() {
|
|||||||
*/
|
*/
|
||||||
void loop() {
|
void loop() {
|
||||||
if (Serial.available() > 0) {
|
if (Serial.available() > 0) {
|
||||||
char command[8];
|
String command;
|
||||||
char currentChar;
|
char currentChar;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
bool readable = true;
|
bool readable = true;
|
||||||
while (readable) {
|
command = Serial.readString();
|
||||||
currentChar = Serial.read();
|
|
||||||
readable = (currentChar != -1);
|
int servoPos = parse_angle(command);
|
||||||
if (readable) {
|
int validatedServoPos = validate_new_servo_pos(servoPos);
|
||||||
command[i] = currentChar;
|
|
||||||
i++;
|
Serial.print("move to angle ");
|
||||||
}
|
Serial.println(validatedServoPos);
|
||||||
}
|
|
||||||
command[i] = '\0';
|
ourServo.write(validatedServoPos);
|
||||||
|
|
||||||
long servoPos = parse_angle(command);
|
if (lightLED) {
|
||||||
int validatedServoPos = validate_new_servo_pos(servoPos);
|
lightLED = false;
|
||||||
|
digitalWrite(ledPin, HIGH);
|
||||||
ourServo.write(validatedServoPos);
|
delay(500);
|
||||||
|
digitalWrite(ledPin, LOW);
|
||||||
if (lightLED) {
|
}
|
||||||
lightLED = false;
|
|
||||||
digitalWrite(ledPin, HIGH);
|
|
||||||
delay(500);
|
|
||||||
digitalWrite(ledPin, LOW);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to handle the timer.
|
||||||
|
*/
|
||||||
|
void TC1_Handler()
|
||||||
|
{
|
||||||
|
// request static for some magic behind the curtain
|
||||||
|
TC_GetStatus(TC0, 1);
|
||||||
|
read_ready = true;
|
||||||
|
}
|
||||||
|
|||||||
50
es/blatt4/uebung4-2/uebung4-2.ino
Normal file
50
es/blatt4/uebung4-2/uebung4-2.ino
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include <Wire.h>
|
||||||
|
|
||||||
|
// these variables describe the used hardware pins
|
||||||
|
// adjust them when you use other pins
|
||||||
|
// hardware pins
|
||||||
|
int ledPin = 13;
|
||||||
|
int analogLevel = 0;
|
||||||
|
|
||||||
|
int slaveAddress = 4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup function for initial setup code
|
||||||
|
*/
|
||||||
|
void setup() {
|
||||||
|
// Configure pins
|
||||||
|
Wire.begin();
|
||||||
|
Wire.onReceive(receiveEvent);
|
||||||
|
|
||||||
|
// initialize serial port
|
||||||
|
Serial.begin(9600);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loop function for main code
|
||||||
|
*/
|
||||||
|
void loop() {
|
||||||
|
Wire.beginTransmission(slaveAddress);
|
||||||
|
Wire.write(1);
|
||||||
|
Wire.endTransmission();
|
||||||
|
delay(100);
|
||||||
|
Wire.beginTransmission(slaveAddress);
|
||||||
|
Wire.write('r');
|
||||||
|
Wire.endTransmission();
|
||||||
|
delay(1900);
|
||||||
|
Wire.beginTransmission(slaveAddress);
|
||||||
|
Wire.write(0);
|
||||||
|
Wire.endTransmission();
|
||||||
|
delay(100);
|
||||||
|
Wire.beginTransmission(slaveAddress);
|
||||||
|
Wire.write('r');
|
||||||
|
Wire.endTransmission();
|
||||||
|
delay(1900);
|
||||||
|
}
|
||||||
|
|
||||||
|
void receiveEvent(int readBytes)
|
||||||
|
{
|
||||||
|
int x = Wire.read();
|
||||||
|
Serial.print("Result: ");
|
||||||
|
Serial.println(x ? "on" : "off");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user