Commit 7bb3d92b authored by Werner Sembach's avatar Werner Sembach
Browse files

Merge branch 'master' into audio_werner

# Conflicts:
#	main.c
parent 31b32258
......@@ -9,6 +9,7 @@
#define SPRING_LEFT 0
#define SPRING_RIGHT 1
#define SPRING_NOTHING 2
typedef struct Spring{
unsigned int threshold;
......@@ -33,7 +34,7 @@ double get_degree_max(void);
void get_all_levers(int levers[]);
Spring set_spring(int threshold, char direction, int power, bool solved);
void set_spring(Spring *spring, unsigned int threshold, uint8_t direction, double power, bool solved);
bool simulate_spring_simple(void);
bool simulate_spring(Spring springs[], size_t length);
......
......@@ -4,8 +4,8 @@
#include "Util.h"
//#include "spring_riddle.h"
//#include "hill_riddle.h"
#include "lever_riddle.h"
#include "hill_riddle.h"
//#include "lever_riddle.h"
//#include "easy_riddle.h"
#define STACKSIZE (CYGNUM_HAL_STACK_SIZE_MINIMUM+1024)
......
#pragma once
void riddle(cyg_addrword_t arg) {
#include <stdio.h>
#include "DCMotor.h"
#include "ExternButton.h"
#include "ServoMotor.h"
#include "Util.h"
void riddle(void) {
bool button_pressed = false;
bool spring_broken = false;
bool upside_down = false;
bool levers_solved = false;
servo_set(SERVO_0, 0.0);
dc_set(0, 0.0);
servo_set_percent(SERVO_0, 0.5);
dc_set_percent(0, 0.0);
/*About the spring struct:
int threshold: remember the potty deadzone(s) (see riddle1 for example values)
char direction: l - left, r - right, n - nothing (every other char will result in no movement too)
int power: between 0 and 255; -1 makes it linear to the adc value
bool solved: the bool that is returned if the adc value is below the threshold (et it to true in the range the riddle should be solved)
the structs have have to be sorted ascending by threshold*/
//riddle 1
/*struct spring riddle1[3];
riddle1[0] = set_spring(256, 'n', 0, false); //potty deadzone
riddle1[1] = set_spring(4096 - 256, 'r', -1, false); // main spring simulation
riddle1[2] = set_spring(4096, 'n', 0, true); // spring is broken - spring riddle is solved
while (true) {
//check if solved
if(button_pressed && spring_broken && upside_down) {
ezs_printf("CONGRATULATIONS! YOU FINALLY MADE IT!\n");
servo_set(SERVO_0, 255);
break;
}
button_pressed = button_get();
unsigned int threshold: remember the potty protection zones
uint8_t direction: SPRING_LEFT, SPRING_LEFT direction the spring is turning in this interval
double power: between 0 and 1.0; power < 0 makes it linear to the adc value
bool solved: the bool that is returned if the adc value is below the threshold (set it to true in the range the riddle should be solved)
double degree_max = get_degree_max();
upside_down = (degree_max > 90);
-> the structs have have to be sorted ascending by threshold*/
//spring riddle can only be solved if button is pressed
if(button_pressed) {
if (!spring_broken) {
spring_broken = simulate_spring(riddle1, 3);
}
}
else {
block_spring(2048);
}
}*/
//riddle2
/*bool springa_broken = false;
bool springa_broken = false;
bool springb_broken = false;
bool unlocked_b = false;
//'balancing on the mountain' -> botm
struct spring riddle2a[5];
riddle2a[0] = set_spring(256, 'n', 0, false);
riddle2a[1] = set_spring(1400, 'r', 100, false);
riddle2a[2] = set_spring(1500, 'n', 0, true); //solved
riddle2a[3] = set_spring(3000, 'l', 100, false);
riddle2a[4] = set_spring(4096, 'n', 0, false);
Spring riddle2a[7];
set_spring(&riddle2a[0], 512, SPRING_NOTHING, 0.0, false);
set_spring(&riddle2a[1], 1000, 0, 0.0, false);
set_spring(&riddle2a[2], 1900, SPRING_RIGHT, 0.5, false);
set_spring(&riddle2a[3], 2100, 0, 0.0, true); //solved
set_spring(&riddle2a[4], 3000, SPRING_LEFT, 0.5, false);
set_spring(&riddle2a[5], 4096 - 512, 0, 0.0, false);
set_spring(&riddle2a[6], 4096, SPRING_NOTHING, 0.0, false);
//'climb out of the valley' -> cootv
struct spring riddle2b[5];
riddle2b[0] = set_spring(256, 'n', 0, true); //solved
riddle2b[1] = set_spring(1000, 'l', 100, false);
riddle2b[2] = set_spring(2000, 'n', 0, false);
riddle2b[3] = set_spring(3000, 'r', 100, false);
riddle2b[4] = set_spring(4096, 'n', 0, true); //solved
Spring riddle2b[7];
set_spring(&riddle2b[0], 512, SPRING_NOTHING, 0.0, false);
set_spring(&riddle2b[1], 1000, 0, 0.0, true); //solved
set_spring(&riddle2b[2], 1900, SPRING_LEFT, 0.5, false);
set_spring(&riddle2b[3], 2100, 0, 0.0, false);
set_spring(&riddle2b[4], 3000, SPRING_RIGHT, 0.5, false);
set_spring(&riddle2b[5], 4096 - 512, 0, 0.0, true); //solved
set_spring(&riddle2b[6], 4096, SPRING_NOTHING, 0.0, false);
while(true) {
//poti protection
bool protection = block_spring(LOWER_BOUND, 256) || block_spring(UPPER_BOUND, 4096 - 256);
//check if solved
if(spring_broken && upside_down) {
ezs_printf("CONGRATULATIONS! YOU FINALLY MADE IT!\n");
servo_set(SERVO_0, 255);
dc_set(0,0);
if(spring_broken) {
printf("CONGRATULATIONS! YOU FINALLY MADE IT!\n");
servo_set_percent(SERVO_0, 1.0);
dc_set_percent(0, 0.0);
break;
}
button_pressed = button_get();
double degree_max = get_degree_max();
upside_down = (degree_max < 45);
//button has to be pressed when 'botm' is solved
if (!unlocked_b) {
springa_broken = simulate_spring(riddle2a, 5);
springa_broken = simulate_spring(riddle2a, 7);
}
if (springa_broken && !unlocked_b) {
if(button_pressed){
unlocked_b = true;
......@@ -92,18 +73,20 @@ void riddle(cyg_addrword_t arg) {
//if the button is pressed when the 'cootv' riddle is solved, you have to solve the 'botm' riddle again
if (unlocked_b) {
springb_broken = simulate_spring(riddle2b, 5);
springb_broken = simulate_spring(riddle2b, 7);
}
if (springb_broken) {
if (button_pressed) {
unlocked_b = false;
spring_broken = false;
springa_broken = false;
springb_broken = false;
}
}
if (springa_broken && springb_broken){
if (springa_broken && springb_broken) {
dc_set_percent(0, 0.0);
spring_broken = true;
}
}*/
}
}
......@@ -16,6 +16,9 @@ void riddle(void) {
dc_set_percent(0, 0.0);
while(true) {
//poti protection
bool protection = block_spring(LOWER_BOUND, 256) || block_spring(UPPER_BOUND, 4096 - 256);
//check if solved
if(levers_solved && button_pressed) {
printf("CONGRATULATIONS! YOU FINALLY MADE IT!\n");
......
......@@ -20,7 +20,7 @@ void riddle(void) {
while (true) {
//poti protection
bool protection = block_spring(LOWER_BOUND, 256) && block_spring(UPPER_BOUND, 4096 - 256);
bool protection = block_spring(LOWER_BOUND, 256) || block_spring(UPPER_BOUND, 4096 - 256);
//check if solved
if(button_pressed && spring_broken && upside_down) {
......@@ -47,4 +47,33 @@ void riddle(void) {
}
}
}
//same riddle using parametrized spring simulation
/*struct spring riddle1[3];
riddle1[0] = set_spring(256, 'n', 0, false); //potty deadzone
riddle1[1] = set_spring(4096 - 256, 'r', -1, false); // main spring simulation
riddle1[2] = set_spring(4096, 'n', 0, true); // spring is broken - spring riddle is solved
while (true) {
//check if solved
if(button_pressed && spring_broken && upside_down) {
ezs_printf("CONGRATULATIONS! YOU FINALLY MADE IT!\n");
servo_set(SERVO_0, 255);
break;
}
button_pressed = button_get();
double degree_max = get_degree_max();
upside_down = (degree_max > 90);
//spring riddle can only be solved if button is pressed
if(button_pressed) {
if (!spring_broken) {
spring_broken = simulate_spring(riddle1, 3);
}
}
else {
block_spring(2048);
}
}*/
}
\ No newline at end of file
......@@ -12,6 +12,8 @@
#include <libopencm3/stm32/f4/rcc.h>
#include <libopencm3/stm32/f4/gpio.h>
#include "ADC.h"
#define GPIO_B0 CYGHWR_HAL_STM32_PIN_ALTFN_OUT(B, 0, 2, PUSHPULL, NONE, LOW)
#define GPIO_B1 CYGHWR_HAL_STM32_PIN_ALTFN_OUT(B, 1, 2, PUSHPULL, NONE, LOW)
......
......@@ -107,13 +107,11 @@ extern "C" void get_all_levers(int levers[]) {
levers[3] = lever_get(LEVER_3);
}
extern "C" Spring set_spring(int threshold, char direction, int power, bool solved){
Spring set;
set.threshold = threshold;
set.direction = direction;
set.power = power;
set.solved = solved;
return set;
extern "C" void set_spring(Spring *spring, unsigned int threshold, uint8_t direction, double power, bool solved) {
spring->threshold = threshold;
spring->direction = direction;
spring->power = power;
spring->solved = solved;
}
//returns true if spring is in "broken" state
......@@ -153,19 +151,16 @@ extern "C" bool simulate_spring(Spring springs[], size_t length) {
if (springs[i].power < 0){
if (springs[i].direction == SPRING_LEFT){
dc_set_percent(DC_LEFT, adc_value / 4096.0);
} else if (springs[i].direction == SPRING_RIGHT){
}
else if (springs[i].direction == SPRING_RIGHT) {
dc_set_percent(DC_RIGHT, adc_value / 4096.0);
} else {
dc_set_percent(0, 0.0);
}
}
else {
if (springs[i].direction == SPRING_LEFT){
dc_set_percent(DC_LEFT, springs[i].power);
} else if (springs[i].direction == SPRING_RIGHT){
} else if (springs[i].direction == SPRING_RIGHT) {
dc_set_percent(DC_RIGHT, springs[i].power);
} else {
dc_set_percent(0, 0.0);
}
}
return springs[i].solved;
......@@ -177,7 +172,7 @@ extern "C" bool simulate_spring(Spring springs[], size_t length) {
//blocks sping by giving full power on motor in certain positions
extern "C" bool block_spring(uint8_t direction, unsigned int position) {
uint32_t adc_value = adc_get(ADC_11);
unsigned int adc_value = adc_get(ADC_11);
if (direction == UPPER_BOUND) {
if (adc_value > position) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment