From f15b13e2a7f8f44a2bb4d10f508e78c5c20350bc Mon Sep 17 00:00:00 2001 From: Anthony Good Date: Wed, 22 Jul 2020 19:24:44 -0400 Subject: [PATCH] 2020.07.22.01 Developing FEATURE_SATELLITE_TRACKING. Yea. FEATURE_NEXTION_DISPLAY: call service_nextion_display() right after rebooting display at start up --- .../k3ng_rotator_controller.ino | 351 ++++++++++++++++-- .../rotator_debug_log_activation.h | 3 + .../rotator_features_test.h | 2 + libraries/MathHelpers/MathHelpers.h | 247 ++++++++++++ .../examples/binhex_test/binhex_test.ino | 61 +++ .../mathHelperTest/mathHelperTest.ino | 208 +++++++++++ libraries/MathHelpers/keywords.txt | 26 ++ libraries/MathHelpers/library.json | 24 ++ libraries/MathHelpers/library.properties | 9 + libraries/sunpos/sunpos.cpp | 12 +- libraries/sunpos/sunpos.h | 8 +- 11 files changed, 910 insertions(+), 41 deletions(-) create mode 100755 libraries/MathHelpers/MathHelpers.h create mode 100755 libraries/MathHelpers/examples/binhex_test/binhex_test.ino create mode 100755 libraries/MathHelpers/examples/mathHelperTest/mathHelperTest.ino create mode 100755 libraries/MathHelpers/keywords.txt create mode 100755 libraries/MathHelpers/library.json create mode 100755 libraries/MathHelpers/library.properties diff --git a/k3ng_rotator_controller/k3ng_rotator_controller.ino b/k3ng_rotator_controller/k3ng_rotator_controller.ino index 913371a..8688495 100644 --- a/k3ng_rotator_controller/k3ng_rotator_controller.ino +++ b/k3ng_rotator_controller/k3ng_rotator_controller.ino @@ -588,6 +588,10 @@ Added NOT_PROVISIONED state to gCS Clock Status API variable Added gX and gY API variables for heading Cartesian coordinates, for future use to drive combined azimuth and elevation gauges + 2020.07.22.01 + Developing FEATURE_SATELLITE_TRACKING. Yea. + FEATURE_NEXTION_DISPLAY: call service_nextion_display() right after rebooting display at start up + All library files should be placed in directories likes \sketchbook\libraries\library1\ , \sketchbook\libraries\library2\ , etc. Anything rotator_*.* should be in the ino directory! @@ -599,7 +603,7 @@ */ -#define CODE_VERSION "2020.07.21.01" +#define CODE_VERSION "2020.07.22.01" #include #include @@ -881,7 +885,6 @@ byte current_az_speed_voltage = 0; byte perform_screen_redraw = 0; #endif // FEATURE_LCD_DISPLAY - #ifdef FEATURE_ROTARY_ENCODER_SUPPORT #ifdef OPTION_ENCODER_HALF_STEP_MODE // Use the half-step state table (emits a code at 00 and 11) const unsigned char ttable[6][4] = { @@ -1180,6 +1183,12 @@ DebugClass debug; PCF8583 rtc(0xA0); #endif //FEATURE_RTC_PCF8583 +#if defined(FEATURE_SATELLITE_TRACKING) + #include + #include + Plan13 satellite_tracker; +#endif + #ifdef HARDWARE_EA4TX_ARS_USB #undef LCD_COLUMNS #undef LCD_ROWS @@ -1229,6 +1238,26 @@ DebugClass debug; unsigned long last_activity_time_autopark = 0; #endif +#if defined(FEATURE_SATELLITE_TRACKING) + struct kep{ + char satellite[16]; + double YE; + double TE; + double IN; + double RA; + double EC; + double WP; + double MA; + double MM; + double M2; + double RV; + double ALON; + }; + + kep current_satellite; + +#endif + /* ------------------ let's start doing some stuff now that we got the formalities out of the way --------------------*/ @@ -9263,6 +9292,8 @@ void initialize_peripherals(){ nexSerial.begin(NEXTION_SERIAL_BAUD); sendNextionCommand("code_c"); // stop execution of any buffered commands in Nextion sendNextionCommand("rest"); // reset the Nextion unit + delay(200); + service_nextion_display(); #endif //FEATURE_NEXTION_DISPLAY #ifdef FEATURE_AZ_POSITION_HMC5883L @@ -14222,9 +14253,6 @@ Not implemented yet: } -//zzzzzz - - #if defined(FEATURE_MOON_TRACKING) || defined(FEATURE_SUN_TRACKING) || defined(FEATURE_CLOCK) || defined(FEATURE_GPS) || defined(FEATURE_REMOTE_UNIT_SLAVE) || defined(OPTION_DISPLAY_ALT_HHMM_CLOCK_AND_MAIDENHEAD) || defined(OPTION_DISPLAY_CONSTANT_HHMMSS_CLOCK_AND_MAIDENHEAD) // \?GCxxxx xxxx - go to coordinate target (rotate azimuth) if ((input_buffer[2] == 'G') && (input_buffer[3] == 'C')){ @@ -16724,42 +16752,303 @@ void convert_polar_to_cartesian(byte coordinate_conversion,double azimuth_in,dou #endif //FEATURE_NEXTION_DISPLAY +//------------------------------------------------------ +#if defined(FEATURE_SATELLITE_TRACKING) +void dump_kep(kep * kep_to_dump){ +//zzzzzz + control_port->print("YE:"); + control_port->println(kep_to_dump->YE,0); + control_port->print("TE:"); + control_port->println(kep_to_dump->TE,8); + control_port->print("IN:"); + control_port->println(kep_to_dump->IN,4); + control_port->print("RA:"); + control_port->println(kep_to_dump->RA,4); + control_port->print("EC:"); + control_port->println(sci(kep_to_dump->EC,4)); // sci function from MathHelpers.h + control_port->print("WP:"); + control_port->println(kep_to_dump->WP,4); + control_port->print("MA:"); + control_port->println(kep_to_dump->MA,4); + control_port->print("MM:"); + control_port->println(kep_to_dump->MM,9); + control_port->print("M2:"); + control_port->println(kep_to_dump->M2,8); + control_port->print("RV:"); + control_port->println(kep_to_dump->RV,0); + control_port->print("ALON:"); + control_port->println(kep_to_dump->ALON,1); + +} +#endif +//------------------------------------------------------ +#if defined(FEATURE_SATELLITE_TRACKING) +byte parse_tle(char* line1,char* line2,kep * target_kep_struct){ + + // 1 28375U 04025K 09232.55636497 -.00000001 00000-0 12469-4 0 4653 + // 2 28375 098.0531 238.4104 0083652 290.6047 068.6188 14.40649734270229 + + // 1 AAAAAU 00 0 0 BBBBB.BBBBBBBB .CCCCCCCC 00000-0 00000-0 0 DDDZ + // 2 AAAAA EEE.EEEE FFF.FFFF GGGGGGG HHH.HHHH III.IIII JJ.JJJJJJJJKKKKKZ + + // KEY: A-CATALOGNUM B-EPOCHTIME C-DECAY D-ELSETNUM E-INCLINATION F-RAAN + // G-ECCENTRICITY H-ARGPERIGEE I-MNANOM J-MNMOTION K-ORBITNUM Z-CHECKSUM + + // YE TE IN RA EC WP MA MM M2 RV ALON + //setElements(2009, 232.55636497, 98.0531, 238.4104, 83652*1.0e-7, 290.6047, 68.6188, 14.406497342, -0.00000001, 27022, 180.0); + + + char temp_char1[16]; + char temp_char2[16]; + + //strcpy(target_kep_struct->satellite,"AO-7"); + //zzzzzz + + // EPOCHTIME -> YE & TE + strcpy(temp_char1,parse_char_string(line1,4,' ',1)); + + // YE + temp_char2[0] = temp_char1[0]; + temp_char2[1] = temp_char1[1]; + temp_char2[2] = 0; + target_kep_struct->YE = char_to_float(temp_char2) + 2000; + + // TE + for (int x = 0;x < 12;x++){ // only get last 12 chars + temp_char2[x] = temp_char1[x+2]; + } + temp_char2[12] = 0; + target_kep_struct->TE = char_to_float(temp_char2); + + // DECAY -> M2 + strcpy(temp_char1,parse_char_string(line1,5,' ',1)); + target_kep_struct->M2 = char_to_float(temp_char1); + + // INCLINATION -> IN + strcpy(temp_char1,parse_char_string(line2,3,' ',1)); + target_kep_struct->IN = char_to_float(temp_char1); + + // RAAN -> RA + strcpy(temp_char1,parse_char_string(line2,4,' ',1)); + target_kep_struct->RA = char_to_float(temp_char1); + + // ECCENTRICITY -> EC + strcpy(temp_char1,parse_char_string(line2,5,' ',1)); + target_kep_struct->EC = char_to_float(temp_char1) * 1.0e-7; + + // ARGPERIGEE -> WP + strcpy(temp_char1,parse_char_string(line2,6,' ',1)); + target_kep_struct->WP = char_to_float(temp_char1); + + // MNANOM -> MA + strcpy(temp_char1,parse_char_string(line2,7,' ',1)); + target_kep_struct->MA = char_to_float(temp_char1); + + // MNMOTION -> MM + strcpy(temp_char1,parse_char_string(line2,8,' ',1)); + for (int x = 0;x < 11;x++){ // only get first 11 chars + temp_char2[x] = temp_char1[x]; + } + temp_char2[11] = 0; + target_kep_struct->MM = char_to_float(temp_char2); + + // ORBITNUM -> RV + for (int x = 11;x < 16;x++){ // get chars 11 to 15 (starts with 0) + temp_char2[x-11] = temp_char1[x]; + } + temp_char2[5] = 0; + target_kep_struct->RV = char_to_float(temp_char2); + + target_kep_struct->ALON = 180.0; + + + +} + +#endif + +//------------------------------------------------------ + +float char_to_float(char * input_buffer){ + + float temp_float = 0; + byte finished = 0; + float hit_decimal = 0; + byte make_negative = 0; + int x = 0; + + + while (!finished){ + if (input_buffer[x] == '.'){ + hit_decimal = 10; + } else { + if ((input_buffer[x] == ' ') || (input_buffer[x] == 0) || (input_buffer[x] == '\r') || (input_buffer[x] == '\n')){ + finished = 1; + } else { + if(input_buffer[x] == '-'){ + make_negative = 1; + } else { + if (hit_decimal > 0){ + temp_float = temp_float + ((float)(input_buffer[x] - 48) / (float)hit_decimal); + hit_decimal = hit_decimal * 10; + } else { + temp_float = (temp_float * 10) + (input_buffer[x] - 48); + } + } + } + } + x++; + if (x > 254){finished = 1;} + } + + + if (make_negative){temp_float = temp_float * -1.0;} + + return temp_float; + +} + +//------------------------------------------------------ +#if defined(FEATURE_SATELLITE_TRACKING) + char* parse_char_string(char* char_string,byte token_number,char delimiter,byte ignore_consecutive_delimiters){ + + #define PARSE_TEMP_STRING_SIZE 32 + + byte current_token = 1; + byte current_position = 0; + byte temp_char_current_position = 0; + byte last_character_was_delimiter = 0; + byte hit_non_whitespace = 0; + byte finished = 0; + static char temp_char[PARSE_TEMP_STRING_SIZE]; + + while (!finished){ + if (char_string[current_position] == delimiter){ + if (current_token == token_number){ + if ((hit_non_whitespace) || (!ignore_consecutive_delimiters)){ + finished = 1; + } + } else { + if (ignore_consecutive_delimiters){ + if (!last_character_was_delimiter){ + current_token++; + hit_non_whitespace = 0; + } + } else { + current_token++; + hit_non_whitespace = 0; + } + } + last_character_was_delimiter = 1; + } else { + if ((char_string[current_position] == '\r') || (char_string[current_position] == '\n') || (char_string[current_position] == 0)){ + finished = 1; + } else { + if (current_token == token_number){ + temp_char[temp_char_current_position] = char_string[current_position]; + temp_char_current_position++; + if (temp_char_current_position == 20){ + finished = 1; + } + } + last_character_was_delimiter = 0; + hit_non_whitespace = 1; + } + } + current_position++; + if (current_position == 255){ + finished = 1; + } + } // while (!finished){ + + if (temp_char_current_position < PARSE_TEMP_STRING_SIZE){ + temp_char[temp_char_current_position] = 0; + } + + return temp_char; + + } +#endif //FEATURE_SATELLITE_TRACKING + + + + //------------------------------------------------------ void run_this_once(){ - // double x = 0; - // double y = 0; - // int el = 0; - // control_port->println("\r\naz\tel\tx\ty"); - // for (int az = 0;az < 360;az = az + 5){ - // el = 0; - // convert_polar_to_cartesian(COORDINATE_PLANE_UPPER_LEFT_ORIGIN,az,el,&x,&y); - - // control_port->print(az); - // control_port->print("\t"); - // control_port->print(el); - // control_port->print("\t"); - // control_port->print(x); - // control_port->print("\t"); - // control_port->println(y); + #if defined(FEATURE_SATELLITE_TRACKING) + parse_tle("1 28375U 04025K 09232.55636497 -.00000001 00000-0 12469-4 0 4653","2 28375 098.0531 238.4104 0083652 290.6047 068.6188 14.40649734270229",¤t_satellite); + control_port->println("\r\n"); + dump_kep(¤t_satellite); + #endif - // el = 45; - // convert_polar_to_cartesian(COORDINATE_PLANE_UPPER_LEFT_ORIGIN,az,el,&x,&y); - // control_port->print(az); - // control_port->print("\t"); - // control_port->print(el); - // control_port->print("\t"); - // control_port->print(x); - // control_port->print("\t"); - // control_port->println(y); + // control_port->println(); + // control_port->println(parse_char_string(" test1 test2 test3",1,' ',1)); + // control_port->println(parse_char_string("test1 test2 test3",2,' ',1)); + // control_port->println(parse_char_string(",,,test1,test2,,,,,test3",3,',',0)); + // control_port->println(parse_char_string(",,,test1,test2,,,,,test3",4,',',0)); + // control_port->println(parse_char_string(",,,test1,test2,,,,,test3",5,',',0)); + // control_port->println(parse_char_string(",,,test1,test2,,,,,test3",10,',',0)); - //} - + #if defined(FEATURE_SATELLITE_TRACKING) && defined(DEBUG_TEST_PLAN13_LIBRARY) + + + // void setElements(double YE_in, double TE_in, double IN_in, double + // RA_in, double EC_in, double WP_in, double MA_in, double MM_in, + // double M2_in, double RV_in, double ALON_in ); + + // 8/20/2009 Keplarian Elements used to produce setElements data below + // 1 28375U 04025K 09232.55636497 -.00000001 00000-0 12469-4 0 4653 + // 2 28375 098.0531 238.4104 0083652 290.6047 068.6188 14.40649734270229 + + // 1 AAAAAU 00 0 0 BBBBB.BBBBBBBB .CCCCCCCC 00000-0 00000-0 0 DDDZ + // 2 AAAAA EEE.EEEE FFF.FFFF GGGGGGG HHH.HHHH III.IIII JJ.JJJJJJJJKKKKKZ + // KEY: A-CATALOGNUM B-EPOCHTIME C-DECAY D-ELSETNUM E-INCLINATION F-RAAN + // G-ECCENTRICITY H-ARGPERIGEE I-MNANOM J-MNMOTION K-ORBITNUM Z-CHECKSUM + + satellite_tracker.setFrequency(435300000, 145920000); //AO-51 frequency + satellite_tracker.setLocation(-64.375, 45.8958, 32); // Sackville, NB + satellite_tracker.setTime(2009, 10, 1, 19, 5, 0); //Oct 1, 2009 19:05:00 UTC + satellite_tracker.setElements(2009, 232.55636497, 98.0531, 238.4104, 83652*1.0e-7, 290.6047, 68.6188, 14.406497342, -0.00000001, 27022, 180.0); //fairly recent keps for AO-51 //readElements(); + satellite_tracker.initSat(); + satellite_tracker.satvec(); + satellite_tracker.rangevec(); + control_port->println("\r\nPlan13 Satellite Tracking Test"); + control_port->println("Correct data: AZ:57.07 EL:4.05 RX:435301728 TX:145919440"); + control_port->print("Result: "); + satellite_tracker.printdata(); + control_port->println(); + + + + + #endif //defined(FEATURE_SATELLITE_TRACKING) && defined(DEBUG_TEST_PLAN13_LIBRARY) + + + + #if defined(DEBUG_TEST_POLAR_TO_CARTESIAN) + double x = 0; + double y = 0; + int el = 0; + control_port->println("\r\naz\tel\tx\ty"); + for (int az = 0;az < 360;az = az + 5){ + el = 0; + convert_polar_to_cartesian(COORDINATE_PLANE_UPPER_LEFT_ORIGIN,az,el,&x,&y); + control_port->print(az); + control_port->print("\t"); + control_port->print(el); + control_port->print("\t"); + control_port->print(x); + control_port->print("\t"); + control_port->println(y); + } + #endif //DEBUG_TEST_POLAR_TO_CARTESIAN + } diff --git a/k3ng_rotator_controller/rotator_debug_log_activation.h b/k3ng_rotator_controller/rotator_debug_log_activation.h index 5351b39..af2d558 100644 --- a/k3ng_rotator_controller/rotator_debug_log_activation.h +++ b/k3ng_rotator_controller/rotator_debug_log_activation.h @@ -64,3 +64,6 @@ // #define DEBUG_NEXTION_DISPLAY // #define DEBUG_NEXTION_DISPLAY_SERIAL_SEND // #define DEBUG_NEXTION_DISPLAY_SERIAL_RECV +// #define DEBUG_TEST_POLAR_TO_CARTESIAN +// #define DEBUG_TEST_PLAN13_LIBRARY + diff --git a/k3ng_rotator_controller/rotator_features_test.h b/k3ng_rotator_controller/rotator_features_test.h index 40eae4a..f176d64 100755 --- a/k3ng_rotator_controller/rotator_features_test.h +++ b/k3ng_rotator_controller/rotator_features_test.h @@ -28,6 +28,8 @@ #define FEATURE_AUTOCORRECT // #define FEATURE_TEST_DISPLAY_AT_STARTUP +#define FEATURE_SATELLITE_TRACKING // In development + #define LANGUAGE_ENGLISH // all languages customized in rotator_language.h // #define LANGUAGE_SPANISH // #define LANGUAGE_CZECH diff --git a/libraries/MathHelpers/MathHelpers.h b/libraries/MathHelpers/MathHelpers.h new file mode 100755 index 0000000..4f5c9b5 --- /dev/null +++ b/libraries/MathHelpers/MathHelpers.h @@ -0,0 +1,247 @@ +// +// FILE: MathHelpers.h +// AUTHOR: Rob Tillaart +// DATE: 2018-01-21 +// VERSION: 0.1.1 +// +// +// PUPROSE: misc functions for math and time +// +#ifndef MATHHELPERS +#define MATHHELPERS + +#define MATHHELPERS_VERSION (F("0.1.1")) + +// global buffer used by all functions +// so we do not need a static buffer in every function +// not usable in multi-threading environments +// results need to be printed/copied asap +char __mathHelperBuffer[17]; + + +////////////////////////////////////////////////// +// +// FLOAT REPRESENTATION HELPERS +// +char * sci(double number, int digits) +{ + int exponent = 0; + int pos = 0; + + // Handling these costs 13 bytes RAM + // shorten them with N, I, -I ? + if (isnan(number)) + { + strcpy(__mathHelperBuffer, "nan"); + return __mathHelperBuffer; + } + if (isinf(number)) + { + if (number < 0) strcpy(__mathHelperBuffer, "-inf"); + strcpy(__mathHelperBuffer, "inf"); + return __mathHelperBuffer; + } + + // Handle negative numbers + bool neg = (number < 0.0); + if (neg) + { + __mathHelperBuffer[pos++] = '-'; + number = -number; + } + + while (number >= 10.0) + { + number /= 10; + exponent++; + } + while (number < 1 && number != 0.0) + { + number *= 10; + exponent--; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i = 0; i < digits; ++i) + { + rounding *= 0.1; + } + number += rounding; + if (number >= 10) + { + exponent++; + number /= 10; + } + + + // Extract the integer part of the number and print it + uint8_t d = (uint8_t)number; + double remainder = number - d; + __mathHelperBuffer[pos++] = d + '0'; // 1 digit before decimal point + if (digits > 0) + { + __mathHelperBuffer[pos++] = '.'; // decimal point TODO:rvdt CONFIG? + } + + + // Extract digits from the remainder one at a time to prevent missing leading zero's + while (digits-- > 0) + { + remainder *= 10.0; + d = (uint8_t)remainder; + __mathHelperBuffer[pos++] = d + '0'; + remainder -= d; + } + + + // print exponent + __mathHelperBuffer[pos++] = 'E'; + neg = exponent < 0; + if (neg) + { + __mathHelperBuffer[pos++] = '-'; + exponent = -exponent; + } + else __mathHelperBuffer[pos++] = '+'; + + + // 3 digits for exponent; // needed for double + // d = exponent / 100; + // __mathHelperBuffer[pos++] = d + '0'; + // exponent -= d * 100; + + // 2 digits for exponent + d = exponent / 10; + __mathHelperBuffer[pos++] = d + '0'; + d = exponent - d*10; + __mathHelperBuffer[pos++] = d + '0'; + + __mathHelperBuffer[pos] = '\0'; + + return __mathHelperBuffer; +} + + +void sci(Stream &str, float f, uint8_t digits) +{ + str.print(sci(f, digits)); +} + + + +////////////////////////////////////////////////// +// +// TIME HELPERS +// + +// (true) 00:00:00 .. 23:59:59 +// (false) 00:00 .. 23:59 +char * seconds2clock(uint32_t seconds, bool displaySeconds=false) +{ + uint16_t days = seconds / 86400UL; // strips the days + seconds -= (days * 86400UL); + uint8_t hours = seconds / 3600UL; + seconds -= (hours * 3600UL); + uint8_t minutes = seconds / 60UL; + seconds -= (minutes * 60UL); + + uint8_t pos = 0; + __mathHelperBuffer[pos++] = hours/10 + '0'; + __mathHelperBuffer[pos++] = hours%10 + '0'; + __mathHelperBuffer[pos++] = ':'; + + __mathHelperBuffer[pos++] = minutes/10 + '0'; + __mathHelperBuffer[pos++] = minutes%10 + '0'; + if (displaySeconds) + { + __mathHelperBuffer[pos++] = ':'; + __mathHelperBuffer[pos++] = seconds/10 + '0'; + __mathHelperBuffer[pos++] = seconds%10 + '0'; + } + __mathHelperBuffer[pos] = '\0'; + + return __mathHelperBuffer; +} + +char * millis2clock(uint32_t millis) +{ + uint32_t t = millis/1000; + seconds2clock(t, true); + uint16_t m = millis - t*1000; + + __mathHelperBuffer[8] = '.'; + uint8_t pos = 9; + uint8_t d = m/100; + __mathHelperBuffer[pos++] = d + '0'; + m = m - d * 100; + d = m/10; + __mathHelperBuffer[pos++] = d + '0'; + d = m - d * 10; + __mathHelperBuffer[pos++] = d + '0'; + __mathHelperBuffer[pos] = '\0'; + + return __mathHelperBuffer; +} + +float weeks(uint32_t seconds) +{ + return seconds * 1.653439153439e-6; // /604800 +} + +float days(uint32_t seconds) +{ + return seconds * 1.157407407407e-5; // /86400 +} + +float hours(uint32_t seconds) +{ + return seconds * 2.777777777778e-4; // /3600 +} + +float minutes(uint32_t seconds) +{ + return seconds * 1.666666666667e-2; // /60 +} + +////////////////////////////////////////////////// +// +// HEX BIN HELPERS +// +// notes: +// - d should not exceed 16 otherwise __mathHelperBuffer overflows... +// - no 64 bit support + +char * hex(uint32_t value, uint8_t d = 8) +{ + if (d > 16) d = 16; + __mathHelperBuffer[d] = '\0'; + while (d > 0) + { + uint8_t v = value & 0x0F; + value >>= 4; + __mathHelperBuffer[--d] = (v < 10) ? '0' + v : 'A' - 10 + v; + } + return __mathHelperBuffer; +} + + +char * bin(uint32_t value, uint8_t d = 8) +{ + if (d > 16) d = 16; + __mathHelperBuffer[d] = '\0'; + while (d > 0) + { + uint8_t v = value & 0x01; + value >>= 1; + __mathHelperBuffer[--d] = '0' + v; + } + return __mathHelperBuffer; +} + + + + +#endif // MATHHELPERS + +// END OF FILE diff --git a/libraries/MathHelpers/examples/binhex_test/binhex_test.ino b/libraries/MathHelpers/examples/binhex_test/binhex_test.ino new file mode 100755 index 0000000..57deb9b --- /dev/null +++ b/libraries/MathHelpers/examples/binhex_test/binhex_test.ino @@ -0,0 +1,61 @@ +// +// FILE: binhex_test.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2020-02-29 +// (c) : - +// + +#include "MathHelpers.h" + +uint16_t x = 15; + +void setup() +{ + Serial.begin(115200); + for (uint8_t digits = 1; digits < 9; digits += 2) + { + Serial.println(hex(x, digits)); + } + Serial.println(); + + Serial.println(hex(12345, 4)); + Serial.println(); + + for (int i = 0; i < 10; i++) + { + uint32_t z = random(4E9); + Serial.print(hex(z)); + Serial.print('\t'); + Serial.println(z, HEX); + } + Serial.println(); + + for (int i = 0; i < 10; i++) + { + uint8_t z = random(256); + Serial.print(bin(z)); + Serial.print('\t'); + Serial.println(z, BIN); + } + Serial.println(); + + for (int i = 0; i < 10; i++) + { + uint8_t z = random(256); + Serial.print(bin(z, 4)); + Serial.print('\t'); + Serial.println(z, BIN); + } + Serial.println(); + + + Serial.println(bin(-5)); + Serial.println(hex(-16)); +} + +void loop() +{ + +} \ No newline at end of file diff --git a/libraries/MathHelpers/examples/mathHelperTest/mathHelperTest.ino b/libraries/MathHelpers/examples/mathHelperTest/mathHelperTest.ino new file mode 100755 index 0000000..9820b33 --- /dev/null +++ b/libraries/MathHelpers/examples/mathHelperTest/mathHelperTest.ino @@ -0,0 +1,208 @@ +// +// FILE: mathHelperTest.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.0.1 +// PURPOSE: +// +// HISTORY: + +#include "MathHelpers.h" + +uint32_t start; +uint32_t stop; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("MATHHELPERS_VERSION: "); + Serial.println(MATHHELPERS_VERSION); + + test1(); + test2(); + test3(); + test4(); + test5(); + + test10(); + test11(); + test12(); + +} + +void loop() +{} + + +void test1() +{ + Serial.println(); + Serial.println(__FUNCTION__); + + float f = 1; + for (int i = 0; i < 40; i++) + { + f *= 10; + Serial.println(sci(f, 6)); + } + Serial.println(); + + f = 1; + for (int i = 0; i < 50; i++) + { + f /= 10; + Serial.println(sci(f, 6)); + } + Serial.println(); + + f = -1; + for (int i = 0; i < 40; i++) + { + f *= 10; + Serial.println(sci(f, 6)); + } + Serial.println(); + + f = -1; + for (int i = 0; i < 50; i++) + { + f /= 10; + Serial.println(sci(f, 6)); + } + Serial.println(); + Serial.println(); +} + +void test2() +{ + Serial.println(); + Serial.println(__FUNCTION__); + + float f = 1; + for (int i = 0; i < 40; i++) + { + f *= (PI * PI); + Serial.println(sci(f, 6)); + } + Serial.println(); + f = 1; + for (int i = 0; i < 50; i++) + { + f /= (PI * PI); + Serial.println(sci(f, 6)); + } + Serial.println(); + f = -1; + for (int i = 0; i < 40; i++) + { + f *= (PI * PI); + Serial.println(sci(f, 6)); + } + Serial.println(); + f = -1; + for (int i = 0; i < 50; i++) + { + f /= (PI * PI); + Serial.println(sci(f, 6)); + } + Serial.println(); + Serial.println(); +} + +void test3() +{ + Serial.println(); + Serial.println(__FUNCTION__); + + float f = PI; + for (int i = 0; i < 8; i++) + { + Serial.println(sci(f, i)); + } + Serial.println(); +} + +void test4() +{ + Serial.println(); + Serial.println(__FUNCTION__); + + float f = PI; + for (int i = 0; i < 8; i++) + { + sci(Serial, f, i); + Serial.println(); + } + Serial.println(); +} + + +void test5() +{ + Serial.println(); + Serial.println(__FUNCTION__); + + float f = 1.0 / 0.0; + Serial.println(sci(f, 6)); + f = 0.0 / 0.0; + Serial.println(sci(f, 6)); + f = -1.0 / 0.0; + Serial.println(sci(f, 6)); + // TODO find a -inf + + Serial.println(); +} + + +void test10() +{ + Serial.println(); + Serial.println(__FUNCTION__); + + Serial.println("HH:MM:SS"); + for (int i = 0; i < 7; i++) + { + uint32_t now = micros(); + Serial.println(seconds2clock(now)); + } + Serial.println(); +} + +void test11() +{ + Serial.println(); + Serial.println(__FUNCTION__); + + Serial.println("HH:MM:SS.nnn"); + for (int i = 0; i < 7; i++) + { + uint32_t now = micros(); + Serial.println(millis2clock(now)); + } + Serial.println(); +} + +void test12() +{ + Serial.println(); + Serial.println(__FUNCTION__); + + Serial.println("time\tweeks\tdays\thours\tminutes"); + for (int i = 0; i < 7; i++) + { + uint32_t now = micros(); + Serial.print(now); + Serial.print('\t'); + Serial.print(weeks(now), 3); + Serial.print('\t'); + Serial.print(days(now), 3); + Serial.print('\t'); + Serial.print(hours(now), 2); + Serial.print('\t'); + Serial.println(minutes(now), 2); + } + Serial.println(); +} + + +// END OF FILE diff --git a/libraries/MathHelpers/keywords.txt b/libraries/MathHelpers/keywords.txt new file mode 100755 index 0000000..185b898 --- /dev/null +++ b/libraries/MathHelpers/keywords.txt @@ -0,0 +1,26 @@ +####################################### +# Syntax Coloring Map for Troolean +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +sci KEYWORD2 +seconds2clock KEYWORD2 +millis2clock KEYWORD2 +weeks KEYWORD2 +days KEYWORD2 +hours KEYWORD2 +minutes KEYWORD2 +hex KEYWORD2 +bin KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/MathHelpers/library.json b/libraries/MathHelpers/library.json new file mode 100755 index 0000000..ab37e8b --- /dev/null +++ b/libraries/MathHelpers/library.json @@ -0,0 +1,24 @@ +{ + "name": "MathHelpers", + "keywords": "Math, scientific, exponential, notation, clock, weeks, days, hours, minutes, hex, bin", + "description": "Library with representation function for Arduino.", + "authors": + [ + { + "name": "Rob Tillaart", + "email": "Rob.Tillaart@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/RobTillaart/Arduino.git" + }, + "version":"0.1.1", + "frameworks": "arduino", + "platforms": "*", + "export": { + "include": "libraries/MathHelpers" + } +} diff --git a/libraries/MathHelpers/library.properties b/libraries/MathHelpers/library.properties new file mode 100755 index 0000000..f2f2879 --- /dev/null +++ b/libraries/MathHelpers/library.properties @@ -0,0 +1,9 @@ +name=MathHelpers +version=0.1.1 +author=Rob Tillaart +maintainer=Rob Tillaart +sentence=Library with representation functions for Arduino. +paragraph= +category=Uncategorized +url=https://github.com/RobTillaart/Arduino/tree/master/libraries/ +architectures=* \ No newline at end of file diff --git a/libraries/sunpos/sunpos.cpp b/libraries/sunpos/sunpos.cpp index d8f010d..e582068 100755 --- a/libraries/sunpos/sunpos.cpp +++ b/libraries/sunpos/sunpos.cpp @@ -95,7 +95,7 @@ void sunpos(cTime udtTime,cLocation udtLocation, cSunCoordinates *udtSunCoordina dY = cos( dEclipticObliquity ) * dSin_EclipticLongitude; dX = cos( dEclipticLongitude ); dRightAscension = atan2( dY,dX ); - if( dRightAscension < 0.0 ) dRightAscension = dRightAscension + twopi; + if( dRightAscension < 0.0 ) dRightAscension = dRightAscension + TWOPI_SUNPOS; dDeclination = asin( sin( dEclipticObliquity )*dSin_EclipticLongitude ); } @@ -113,9 +113,9 @@ void sunpos(cTime udtTime,cLocation udtLocation, cSunCoordinates *udtSunCoordina 0.0657098283*dElapsedJulianDays + dDecimalHours; dLocalMeanSiderealTime = (dGreenwichMeanSiderealTime*15 - + udtLocation.dLongitude)*rad; + + udtLocation.dLongitude)*RAD_SUNPOS; dHourAngle = dLocalMeanSiderealTime - dRightAscension; - dLatitudeInRadians = udtLocation.dLatitude*rad; + dLatitudeInRadians = udtLocation.dLatitude*RAD_SUNPOS; dCos_Latitude = cos( dLatitudeInRadians ); dSin_Latitude = sin( dLatitudeInRadians ); dCos_HourAngle= cos( dHourAngle ); @@ -125,13 +125,13 @@ void sunpos(cTime udtTime,cLocation udtLocation, cSunCoordinates *udtSunCoordina dX = tan( dDeclination )*dCos_Latitude - dSin_Latitude*dCos_HourAngle; udtSunCoordinates->dAzimuth = atan2( dY, dX ); if ( udtSunCoordinates->dAzimuth < 0.0 ) - udtSunCoordinates->dAzimuth = udtSunCoordinates->dAzimuth + twopi; - udtSunCoordinates->dAzimuth = udtSunCoordinates->dAzimuth/rad; + udtSunCoordinates->dAzimuth = udtSunCoordinates->dAzimuth + TWOPI_SUNPOS; + udtSunCoordinates->dAzimuth = udtSunCoordinates->dAzimuth/RAD_SUNPOS; // Parallax Correction dParallax=(dEarthMeanRadius/dAstronomicalUnit) *sin(udtSunCoordinates->dZenithAngle); udtSunCoordinates->dZenithAngle=(udtSunCoordinates->dZenithAngle - + dParallax)/rad; + + dParallax)/RAD_SUNPOS; } } diff --git a/libraries/sunpos/sunpos.h b/libraries/sunpos/sunpos.h index 427ded3..b4f2f46 100755 --- a/libraries/sunpos/sunpos.h +++ b/libraries/sunpos/sunpos.h @@ -5,9 +5,9 @@ // Declaration of some constants -#define pi 3.14159265358979323846 -#define twopi (2*pi) -#define rad (pi/180) +#define PI_SUNPOS 3.14159265358979323846 +#define TWOPI_SUNPOS (2*PI_SUNPOS) +#define RAD_SUNPOS (PI_SUNPOS/180) #define dEarthMeanRadius 6371.01 // In km #define dAstronomicalUnit 149597890 // In km @@ -36,4 +36,4 @@ struct cSunCoordinates void sunpos(cTime udtTime, cLocation udtLocation, cSunCoordinates *udtSunCoordinates); #endif - +