From d2ed70fd4965f216a249eff2ab98e8a52566c1a6 Mon Sep 17 00:00:00 2001 From: Marc Date: Thu, 22 Aug 2019 11:14:30 +0200 Subject: [PATCH] Allow for working with gravity instead of against it The hinge_opening config can be used for setup where the hinge is closing when working instead of opening. --- arduino/config.h | 6 +++++ arduino/startracker.ino | 59 ++++++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/arduino/config.h b/arduino/config.h index 6f72da0..ea462dd 100644 --- a/arduino/config.h +++ b/arduino/config.h @@ -24,6 +24,12 @@ static const float initial_rod_deploy = 30; /* millimeters per rotation of the threaded-rod */ static const float bolt_thread_mm = 1.25; +/* If true, the hinge is getting opened. If false, hinge is getting + closed. When hinge is closing, gravity is helping. When opening, + motor is working against gravity. +*/ +static const bool hinge_opening = true; + /* When using debug over UART */ static const unsigned long serial_speed = 115200UL; diff --git a/arduino/startracker.ino b/arduino/startracker.ino index 3b9ba88..d5d9e30 100644 --- a/arduino/startracker.ino +++ b/arduino/startracker.ino @@ -131,8 +131,18 @@ static void debug_long(rot_state_t *s){ } #endif -static float get_expected_stepper_rot(rot_state_t *s) { - const float r = tan(earth_rot_speed_rad_msec * s->elapsed_time_millis /* ellapsed_in_sec */) + + +// Returns the value in radian the motor should have turned to reach +// the current value of earth rotation since the beggining. +static float +get_expected_stepper_rot(rot_state_t *s) +{ + float a = earth_rot_speed_rad_msec * s->elapsed_time_millis /* ellapsed_in_sec */; + if (!hinge_opening) + a = 1.57079632679 /* PI/2 */ - a; + + const float r = tan(a) * axis_hinge_dist_mm * 2 * PI * nr_teeth_big @@ -143,10 +153,10 @@ static float get_expected_stepper_rot(rot_state_t *s) { #if MODERATE_DEBUG if (!(loop_count % 100)) { #endif - debug_long(s); + debug_long(s); - Serial.print("Angle final: "); - Serial.println(r); + Serial.print("Angle final: "); + Serial.println(r); #if MODERATE_DEBUG } @@ -155,7 +165,12 @@ static float get_expected_stepper_rot(rot_state_t *s) { return r; } -static unsigned int get_step_number(rot_state_t *s, float expected_rotation) { +// Returns the number of steps and the direction needed to reach given +// rotation angle in radian. + +static int +get_step_number(rot_state_t *s, float expected_rotation) +{ const float angle_diff = expected_rotation - s->stepper_gear_rot_rad; const float fsteps = angle_diff / stepper_gear_rad_per_step; const int steps = floor(fsteps); @@ -180,8 +195,11 @@ static unsigned int get_step_number(rot_state_t *s, float expected_rotation) { return steps; } -static void set_stepper_rotation(rot_state_t *s, float angle){ - const unsigned int needed_steps = get_step_number(s, angle); +// Make the motor move to desired angle +static void +set_stepper_rotation(rot_state_t *s, float angle) +{ + const int needed_steps = get_step_number(s, angle); if (stepper_direction) { stepper.move(needed_steps); } else { @@ -191,8 +209,9 @@ static void set_stepper_rotation(rot_state_t *s, float angle){ s->stepper_gear_rot_rad += needed_steps * stepper_gear_rad_per_step; } - -static void start_timer(unsigned long period) { +static void +start_timer(unsigned long period) +{ #if DEBUG Serial.println("start timer"); #endif @@ -210,7 +229,9 @@ static void start_timer(unsigned long period) { } -static void stop_timer(void) { +static void +stop_timer(void) +{ if (use_active_wait){ active_timer.deadline = active_timer.remain = 0; } @@ -219,7 +240,9 @@ static void stop_timer(void) { // disable timer } -static void handle_active_timer(void){ +static void +handle_active_timer(void) +{ #if DEBUG == 2 Serial.print("Timer: "); @@ -277,7 +300,9 @@ static bool new_state = true; #endif -static void control_automata(void) { +static void +control_automata(void) +{ #if DEBUG == 2 Serial.println("Automata step..."); @@ -381,7 +406,9 @@ static void control_automata(void) { } -void setup() { +void +setup() +{ // debug output Serial.begin(serial_speed); @@ -415,7 +442,9 @@ void setup() { dwprint("Setup finished, starting loop"); } -void loop(void) { +void +loop(void) +{ if (use_active_wait) handle_active_timer();