Commit 25f3da85 authored by Margit Haspel's avatar Margit Haspel
Browse files

neue bessere raetsel

parent 9e94bbc6
......@@ -4,6 +4,15 @@
extern "C" {
#endif
struct spring{
int threshold;
char direction;
int power;
bool solved;
};
struct spring set_spring(int threshold, char direction, int power, bool solved);
void all_init();
void print_gyro();
......@@ -14,8 +23,8 @@ double get_degree_short();
double get_degree_long();
double get_degree_max();
bool simulate_spring();
void block_spring();
bool simulate_spring(struct spring springs[], int len);
void block_spring(int pos);
#ifdef __cplusplus
}
......
......@@ -26,6 +26,20 @@ void main_thread(cyg_addrword_t arg) {
servo_set(SERVO_0, 0);
dc_set(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) {
......@@ -42,13 +56,68 @@ void main_thread(cyg_addrword_t arg) {
//spring riddle can only be solved if button is pressed
if(button_pressed) {
if (!spring_broken) {
spring_broken = simulate_spring();
spring_broken = simulate_spring(riddle1, 3);
}
}
else {
block_spring();
block_spring(2048);
}
}
*/
//riddle2
bool springa_broken = false;
bool springb_broken = false;
bool unlocked_b = false;
struct spring riddle2a[5];
riddle2a[0] = set_spring(256, 'n', 0, false);
riddle2a[1] = set_spring(1324, 'r', 100, false);
riddle2a[2] = set_spring(1500, 'n', 0, true); //solved
riddle2a[3] = set_spring(2048, 'l', 100, false);
riddle2a[4] = set_spring(4096, 'n', 0, false);
struct spring riddle2b[5];
riddle2b[0] = set_spring(256, 'n', 0, true); //solved
riddle2b[1] = set_spring(1024, 'l', 100, false);
riddle2b[2] = set_spring(1800, 'n', 0, false);
riddle2b[3] = set_spring(2048, 'r', 100, false);
riddle2b[4] = set_spring(4096, 'n', 0, true); //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 < 45);
//1st spring riddle can only be solved if button is pressed
//2nd spring riddle needs the button to not be pressed
springa_broken = simulate_spring(riddle2a, 5);
if (springa_broken) {
if(button_pressed){
unlocked_b = true;
}
}
if (!springb_broken) {
springb_broken = simulate_spring(riddle2b, 5);
}
if (springa_broken && springb_broken){
spring_broken = true;
}
}
}
void cyg_user_start(void) {
......
......@@ -13,6 +13,15 @@
#include "ExternButton.h"
#include "ServoMotor.h"
extern "C" struct spring set_spring(int threshold, char direction, int power, bool solved){
struct spring set;
set.threshold = threshold;
set.direction = direction;
set.power = power;
set.solved = solved;
return set;
}
extern "C" void all_init() {
ezs_printf("\n\n================================\n");
ezs_printf("Start Init\n\n");
......@@ -92,12 +101,47 @@ extern "C" double get_degree_max() {
return fabs(neigung_zu_lang_deg);
}
//returns true if spring is in "broken" state
// TODO: make parameters possible in a cool way
// get array with (thresold, direction, power, solved)
extern "C" bool simulate_spring() {
/*returns true if spring is in "broken" state
gets an array of structs that contain:
int threshold - if the poti gives back a value smaller than this
char direction - motor turn in this direction (l, r, anything else)
int power - with this power (-1 sets the power to be linear to the adc value)
bool solved - what is returned if the threshold is reached
*/
extern "C" bool simulate_spring(struct spring springs[], int len) {
uint16_t adc_value = adc_get(11);
for(int i = 0; i < len; i++){
// ezs_printf("%d\n", adc_value);
if (adc_value < springs[i].threshold){
if (springs[i].power < 0){
if (springs[i].direction == 'l'){
dc_set(DC_LEFT, adc_value / 4096.0 * 255);
} else if (springs[i].direction == 'r'){
dc_set(DC_RIGHT, adc_value / 4096.0 * 255);
} else {
dc_set(0,0);
}
}
else {
if (springs[i].direction == 'l'){
dc_set(DC_LEFT, springs[i].power);
} else if (springs[i].direction == 'r'){
dc_set(DC_RIGHT, springs[i].power);
} else {
dc_set(0,0);
}
}
return springs[i].solved;
}
}
// in case no threshold is "reached" (e.g. the struct params are faulty)
// the box shouldn't be unopenable then
return true;
/*old stuff without params:
if (adc_value < 256) {
//potty protection deadzone
dc_set(0, 0);
......@@ -112,17 +156,17 @@ extern "C" bool simulate_spring() {
dc_set(DC_RIGHT, adc_value / 4096.0 * 255);
}
return false;
*/
}
// turns the motor real fast if in certain position
// TODO: make that position a parameter
extern "C" void block_spring() {
// turns the motor real fast if above certain position
extern "C" void block_spring(int pos) {
uint16_t adc_value = adc_get(11);
if(adc_value > 2048) {
if(adc_value > pos) {
dc_set(DC_RIGHT, 255);
}
else if(adc_value > (2048 - 256)) {
uint8_t speed = adc_value - (2048 - 256) / 256.0 * 255;
else if(adc_value > (pos - 256)) {
uint8_t speed = adc_value - (pos - 256) / 256.0 * 255;
dc_set(DC_RIGHT, speed);
}
else {
......
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