From a82fd8f5bed3c058d3deec5497971949e09fd1f7 Mon Sep 17 00:00:00 2001 From: alanbjohnston Date: Wed, 9 Dec 2020 10:52:16 -0500 Subject: [PATCH] added sim_mode variable, set when i2c bus 1 and 3 are off --- afsk/main.c | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/afsk/main.c b/afsk/main.c index 17bef01d..0571824e 100644 --- a/afsk/main.c +++ b/afsk/main.c @@ -125,7 +125,7 @@ float latitude = 39.027702, longitude = -77.078064; float lat_file, long_file; float axis[3], angle[3], volts_max[3], amps_max[3], batt, speed, period, tempS, temp_max, temp_min; -int eclipse; +int eclipse, i2c_bus0 = OFF, i2c_bus1 = OFF, i2c_bus3 = OFF, sim_mode = FALSE; double eclipse_time; int test_i2c_bus(int bus); @@ -399,7 +399,21 @@ else fprintf (stderr, "Unable to open UART: %s\n", strerror (errno)) ; } } + +// test i2c buses + +i2c_bus0 = (test_i2c_bus(0) != -1) ? ON: OFF; +i2c_bus1 = (test_i2c_bus(1) != -1) ? ON: OFF; +i2c_bus3 = (test_i2c_bus(3) != -1) ? ON: OFF; + +if ((i2c_bus1 == OFF) && (i2c_bus3 == OFF)) + sim_mode = TRUE; + +if (sim_mode) +{ +printf("Simulated telemetry mode!\n"); + srand(time(0)); axis[0] = rnd_float(-0.2, 0.2); @@ -424,22 +438,24 @@ amps_max[2] = (amps_avg + rnd_float(-25.0, 25.0)) * (float) cos(angle[1] - angle batt = rnd_float(3.8, 4.3); speed = rnd_float(1.0, 2.5); -//eclipse_time = rnd_float(0, 300); eclipse = (rnd_float(-1, +4) > 0) ? 1 : 0; period = rnd_float(150, 300); tempS = rnd_float(20, 55); temp_max = rnd_float(50, 70); temp_min = rnd_float(10,20); - + +#ifdef DEBUG_LOGGING for(int i=0; i < 3; i++) printf("axis: %f angle: %f v: %f i: %f \n",axis[i], angle[i], volts_max[i], amps_max[i]); printf("batt: %f speed: %f eclipse_time: %f eclipse: %d period: %f temp: %f max: %f min: %f\n", batt, speed, eclipse_time, eclipse, period, tempS, temp_max, temp_min); +#endif time_start = millis(); eclipse_time = millis()/1000.0; if (eclipse == 0) eclipse_time -= period/2; // if starting in eclipse, shorten interval +} int ret; //uint8_t data[1024]; @@ -1028,12 +1044,12 @@ if (firstTime != ON) } fclose(cpuTempSensor); +if (sim_mode) +{ // simulated telemetry double time = (millis() - time_start)/1000.0; - printf("Eclipse time left: %d \n", period - time + eclipse_time); - if ((time - eclipse_time) > period) { eclipse = (eclipse == 1) ? 0 : 1; @@ -1041,9 +1057,6 @@ if (firstTime != ON) printf("\n\nSwitching eclipse mode! \n\n"); } -// double Xi = 10.0 * sin(1.37) * sin(2.0 * 3.14 * time / (46.0 * 2)) + rnd_float(-1, 1); -// double Yi = 8.5 * sin(1.37) * sin((2.0 * 3.14 * time / (46.0 * 2)) + (3.14/2.0)) + rnd_float(-1, 1); -// double Zi = 9.0 * cos(1.37 - 0.2) * sin((2.0 * 3.14 * time / (46.0 * 2)) + 3.14 + 0.785) + rnd_float(-1, 1); /* double Xi = eclipse * amps_max[0] * sin(2.0 * 3.14 * time / (46.0 * speed)) * fabs(sin(2.0 * 3.14 * time / (46.0 * speed))) + rnd_float(-2, 2); double Yi = eclipse * amps_max[1] * sin((2.0 * 3.14 * time / (46.0 * speed)) + (3.14/2.0)) * fabs(sin((2.0 * 3.14 * time / (46.0 * speed)) + (3.14/2.0))) + rnd_float(-2, 2); @@ -1057,9 +1070,9 @@ if (firstTime != ON) double Yv = eclipse * volts_max[1] * sin((2.0 * 3.14 * time / (46.0 * speed)) + (3.14/2.0)) + rnd_float(-0.2, 0.2); double Zv = 2.0 * eclipse * volts_max[2] * sin((2.0 * 3.14 * time / (46.0 * speed)) + 3.14 + angle[2]) + rnd_float(-0.2, 0.2); - printf("Yi: %f Zi: %f %f %f Zv: %f \n", Yi, Zi, amps_max[2], angle[2], Zv); + // printf("Yi: %f Zi: %f %f %f Zv: %f \n", Yi, Zi, amps_max[2], angle[2], Zv); - current[map[PLUS_X]] = ( Xi >= 0) ? Xi: 0; + current[map[PLUS_X]] = ( Xi >= 0) ? Xi: 0; current[map[MINUS_X]] = ( Xi >= 0) ? 0: ((-1.0) * Xi); current[map[PLUS_Y]] = ( Yi >= 0) ? Yi: 0; current[map[MINUS_Y]] = ( Yi >= 0) ? 0: ((-1.0) * Yi); @@ -1073,22 +1086,20 @@ if (firstTime != ON) voltage[map[PLUS_Z]] = ( Zv >= 1) ? Zv: rnd_float(0.9, 1.1); voltage[map[MINUS_Z]] = ( Zv <= -1) ? ((-1.0) * Zv): rnd_float(0.9, 1.1); - printf("temp: %f Time: %f Eclipse: %d : %f %f | %f %f | %f %f\n",tempS, time, eclipse, voltage[map[PLUS_X]], voltage[map[MINUS_X]], voltage[map[PLUS_Y]], voltage[map[MINUS_Y]], current[map[PLUS_Z]], current[map[MINUS_Z]]); + // printf("temp: %f Time: %f Eclipse: %d : %f %f | %f %f | %f %f\n",tempS, time, eclipse, voltage[map[PLUS_X]], voltage[map[MINUS_X]], voltage[map[PLUS_Y]], voltage[map[MINUS_Y]], current[map[PLUS_Z]], current[map[MINUS_Z]]); tempS += (eclipse > 0) ? ((temp_max - tempS)/50.0): ((temp_min - tempS)/50.0); IHUcpuTemp = (int)((tempS + rnd_float(-1.0, 1.0)) * 10 + 0.5); - printf("IHUcpuTemp: %d \n", IHUcpuTemp); - voltage[map[BUS]] = rnd_float(5.0, 5.005); current[map[BUS]] = rnd_float(158, 171); // float charging = current[map[PLUS_X]] + current[map[MINUS_X]] + current[map[PLUS_Y]] + current[map[MINUS_Y]] + current[map[PLUS_Z]] + current[map[MINUS_Z]]; float charging = eclipse * (fabs(amps_max[0] * 0.707) + fabs(amps_max[1] * 0.707) + rnd_float(-4.0, 4.0)); - printf("Charging: %f \n", charging); + current[map[BAT]] = ((current[map[BUS]] * voltage[map[BUS]]) / (batt * 1.0)) - charging; - printf("charging: %f bat curr: %f bus curr: %f bat volt: %f bus volt: %f \n",charging, current[map[BAT]], current[map[BUS]], batt, voltage[map[BUS]]); +// printf("charging: %f bat curr: %f bus curr: %f bat volt: %f bus volt: %f \n",charging, current[map[BAT]], current[map[BUS]], batt, voltage[map[BUS]]); batt -= (batt > 3.5) ? current[map[BAT]]/30000: current[map[BAT]]/3000; if (batt < 3.0) @@ -1099,6 +1110,7 @@ if (firstTime != ON) voltage[map[BAT]] = batt + rnd_float(-0.01, 0.01); // end of simulated telemetry +} memset(rs_frame,0,sizeof(rs_frame)); memset(parities,0,sizeof(parities)); @@ -1145,7 +1157,6 @@ if (firstTime != ON) negZv = (int)(voltage[map[MINUS_Z]] * 100); batt_c_v = (int)(voltage[map[BAT]] * 100); -// batt_c_v = (int)(batt * 100); battCurr = (int)(current[map[BAT]] + 0.5) + 2048; PSUVoltage = (int)(voltage[map[BUS]] * 100);