diff --git a/libraries/data.csv b/libraries/data.csv new file mode 100644 index 0000000..d2b1df1 --- /dev/null +++ b/libraries/data.csv @@ -0,0 +1,601 @@ +time (ms),AGL (in cm),Pressure,Temperature +1429909,3,99301,3640 +1430012,7,99301,3635 +1430112,20,99299,3627 +1430215,28,99298,3624 +1442427,81,99292,3611 +1442530,82,99292,3611 +1442630,92,99291,3611 +1442733,94,99290,3611 +1442833,90,99291,3612 +1444736,89,99291,3615 +1444840,85,99291,3615 +1444940,86,99291,3615 +1445043,86,99291,3616 +1445143,86,99291,3615 +1481981,72,99293,3643 +1482164,95,99290,3642 +1482190,95,99290,3642 +1482200,199,99278,3642 +1482236,385,99256,3642 +1482274,385,99256,3642 +1482284,482,99245,3642 +1482364,856,99200,3643 +1482377,856,99200,3643 +1482387,896,99196,3644 +1482421,896,99196,3644 +1482444,940,99190,3645 +1482454,940,99190,3645 +1482464,1018,99181,3646 +1482496,1018,99181,3646 +1482506,1143,99166,3647 +1482532,1143,99166,3647 +1482542,1305,99147,3648 +1482573,1305,99147,3648 +1482583,1506,99124,3649 +1482622,1506,99124,3649 +1482632,1752,99095,3650 +1482658,1752,99095,3650 +1482668,2061,99058,3651 +1482694,2061,99058,3651 +1482704,2402,99018,3652 +1482736,2402,99018,3652 +1482746,2797,98971,3653 +1482772,2797,98971,3653 +1482782,3233,98920,3654 +1482821,3233,98920,3654 +1482831,3689,98866,3654 +1482857,3689,98866,3654 +1482867,4154,98812,3655 +1482899,4154,98812,3655 +1482909,4622,98757,3655 +1482935,4622,98757,3655 +1482945,5091,98701,3656 +1482976,5091,98701,3656 +1482986,5556,98647,3656 +1483056,6034,98591,3657 +1483067,6034,98591,3657 +1483077,6547,98530,3657 +1483103,6547,98530,3657 +1483113,7074,98468,3657 +1483146,7074,98468,3657 +1483156,7613,98405,3658 +1483251,8188,98338,3658 +1483261,8188,98338,3658 +1483298,8831,98262,3657 +1483308,9468,98188,3657 +1483334,9468,98188,3657 +1483344,10096,98114,3657 +1483425,10721,98041,3658 +1483438,10721,98041,3658 +1483461,11392,97963,3657 +1483471,11392,97963,3657 +1483481,12055,97885,3657 +1483513,12055,97885,3657 +1483523,12721,97807,3657 +1483557,12721,97807,3657 +1483567,13378,97731,3657 +1483593,13378,97731,3657 +1483603,14037,97654,3658 +1483634,14037,97654,3658 +1483644,14686,97578,3658 +1483676,14686,97578,3658 +1483686,15356,97500,3658 +1483712,15356,97500,3658 +1483722,16038,97421,3658 +1483761,16038,97421,3658 +1483771,16721,97341,3659 +1483797,16721,97341,3659 +1483807,17406,97262,3659 +1483839,17406,97262,3659 +1483849,18075,97184,3659 +1483875,18075,97184,3659 +1483885,18720,97109,3659 +1483916,18720,97109,3659 +1483926,19354,97036,3659 +1483958,19354,97036,3659 +1483998,19979,96963,3660 +1484008,19979,96963,3660 +1484018,20612,96890,3660 +1484050,20612,96890,3660 +1484059,21239,96817,3660 +1484097,21239,96817,3660 +1484108,21862,96745,3660 +1484134,21862,96745,3660 +1484144,22476,96674,3660 +1484170,22476,96674,3660 +1484180,23078,96604,3660 +1484206,23078,96604,3660 +1484216,23665,96537,3661 +1484316,24245,96470,3661 +1484336,24878,96397,3660 +1484374,24878,96397,3660 +1484384,25501,96325,3660 +1484464,26115,96254,3660 +1484477,26115,96254,3660 +1484487,26751,96181,3659 +1484521,26751,96181,3659 +1484531,27368,96110,3659 +1484557,27368,96110,3659 +1484567,27975,96040,3659 +1484594,27975,96040,3659 +1484604,28559,95973,3660 +1484630,28559,95973,3660 +1484640,29131,95907,3660 +1484671,29131,95907,3660 +1484681,29699,95842,3661 +1484720,29699,95842,3661 +1484747,30256,95778,3661 +1484757,30256,95778,3661 +1484767,30812,95714,3661 +1484793,30812,95714,3661 +1484803,31356,95652,3662 +1484835,31356,95652,3662 +1484845,31900,95589,3662 +1484871,31900,95589,3662 +1484882,32429,95529,3662 +1484947,32947,95469,3663 +1484957,32947,95469,3663 +1484967,33475,95409,3663 +1484993,33475,95409,3663 +1485003,33993,95350,3663 +1485041,33993,95350,3663 +1485051,34514,95290,3663 +1485078,34514,95290,3663 +1485088,35029,95231,3664 +1485114,35029,95231,3664 +1485124,35531,95174,3664 +1485150,35531,95174,3664 +1485160,36024,95118,3664 +1485191,36024,95118,3664 +1485201,36514,95062,3664 +1485239,36514,95062,3664 +1485249,37006,95006,3665 +1485275,37006,95006,3665 +1485285,37497,94950,3665 +1485395,37976,94895,3665 +1485405,38516,94834,3664 +1485432,38516,94834,3664 +1485442,39047,94773,3664 +1485522,39559,94715,3664 +1485536,39559,94715,3664 +1485559,40085,94655,3663 +1485569,40085,94655,3663 +1485579,40594,94597,3663 +1485611,40594,94597,3663 +1485621,41092,94541,3663 +1485654,41092,94541,3663 +1485664,41579,94485,3663 +1485690,41579,94485,3663 +1485700,42072,94429,3663 +1485731,42072,94429,3663 +1485741,42557,94374,3664 +1485773,42557,94374,3664 +1485783,43040,94320,3664 +1485809,43040,94320,3664 +1485819,43516,94266,3664 +1485889,43998,94211,3664 +1485899,43998,94211,3664 +1485909,44485,94156,3664 +1485935,44485,94156,3664 +1485945,44953,94103,3665 +1486237,45436,94048,3665 +1486247,45436,94048,3665 +1486257,46216,93960,3662 +1486283,46216,93960,3662 +1486293,46953,93877,3661 +1486330,46953,93877,3661 +1486340,47656,93798,3660 +1486366,47656,93798,3660 +1486376,48329,93722,3660 +1486486,48971,93649,3660 +1486496,49621,93576,3659 +1488657,70353,91265,3670 +1488670,70353,91265,3670 +1488680,70722,91224,3669 +1488706,70722,91224,3669 +1488716,71086,91184,3669 +1488742,71086,91184,3669 +1488752,71438,91145,3669 +1488784,71438,91145,3669 +1488794,71776,91108,3669 +1488826,71776,91108,3669 +1488836,72106,91071,3669 +1488862,72106,91071,3669 +1488872,72436,91035,3669 +1488898,72436,91035,3669 +1488908,72759,90999,3670 +1488939,72759,90999,3670 +1488949,73080,90964,3670 +1488981,73080,90964,3670 +1488991,73395,90929,3670 +1489023,73395,90929,3670 +1489033,73706,90895,3670 +1489059,73706,90895,3670 +1489069,74022,90860,3670 +1489095,74022,90860,3670 +1489127,74334,90826,3671 +1489137,74334,90826,3671 +1489147,74651,90791,3671 +1489173,74651,90791,3671 +1489183,74952,90758,3671 +1489222,74952,90758,3671 +1489232,75246,90726,3671 +1489258,75246,90726,3671 +1489268,75532,90694,3671 +1489295,75532,90694,3671 +1489305,75807,90664,3671 +1489331,75807,90664,3671 +1489341,76080,90634,3671 +1489368,76080,90634,3671 +1489378,76345,90605,3672 +1489404,76345,90605,3672 +1489414,76612,90576,3672 +1489446,76612,90576,3672 +1489456,76881,90546,3672 +1489482,76881,90546,3672 +1489492,77151,90517,3673 +1489524,77151,90517,3673 +1489534,77420,90487,3673 +1489560,77420,90487,3673 +1489570,77684,90458,3673 +1491885,90127,89103,3675 +1491904,90127,89103,3675 +1491931,90333,89081,3674 +1491941,90333,89081,3674 +1491951,90533,89059,3673 +1491977,90533,89059,3673 +1491987,90726,89038,3673 +1492014,90726,89038,3673 +1492024,90910,89018,3673 +1492050,90910,89018,3673 +1492060,91092,88999,3673 +1492093,91092,88999,3673 +1492103,91271,88979,3674 +1492129,91271,88979,3674 +1492139,91451,88960,3674 +1492171,91451,88960,3674 +1492202,91624,88941,3674 +1492212,91624,88941,3674 +1492222,91795,88923,3674 +1492249,91795,88923,3674 +1492259,91961,88905,3675 +1492292,91961,88905,3675 +1492302,92125,88887,3675 +1492328,92125,88887,3675 +1492338,92291,88869,3675 +1492370,92291,88869,3675 +1492380,92464,88850,3675 +1492411,92464,88850,3675 +1492421,92637,88832,3675 +1492447,92637,88832,3675 +1492457,92807,88813,3676 +1492483,92807,88813,3676 +1492493,92970,88796,3676 +1492525,92970,88796,3676 +1492535,93129,88779,3676 +1492561,93129,88779,3676 +1492571,93287,88762,3676 +1492604,93287,88762,3676 +1492614,93441,88745,3677 +1492641,93441,88745,3677 +1492651,93591,88729,3677 +1492677,93591,88729,3677 +1493038,93740,88713,3677 +1493048,94005,88684,3674 +1493074,94005,88684,3674 +1493084,94255,88657,3672 +1493116,94255,88657,3672 +1493126,94487,88632,3671 +1493153,94487,88632,3671 +1493163,94709,88608,3670 +1493189,94709,88608,3670 +1493199,94919,88586,3670 +1493230,94919,88586,3670 +1493240,95117,88564,3670 +1493266,95117,88564,3670 +1493276,95300,88545,3671 +1493302,95300,88545,3671 +1493312,95475,88526,3671 +1493381,95640,88508,3672 +1493391,95640,88508,3672 +1493401,95802,88491,3672 +1493433,95802,88491,3672 +1493443,95952,88474,3672 +1493474,95952,88474,3672 +1493484,96101,88458,3673 +1493510,96101,88458,3673 +1493520,96243,88443,3673 +1493546,96243,88443,3673 +1493556,96379,88428,3674 +1493588,96379,88428,3674 +1493598,96514,88414,3674 +1493624,96514,88414,3674 +1493634,96643,88400,3674 +1493670,96643,88400,3674 +1493680,96767,88387,3675 +1493706,96767,88387,3675 +1493716,96890,88374,3675 +1493742,96890,88374,3675 +1493752,97015,88360,3675 +1493784,97015,88360,3675 +1493794,97134,88347,3676 +1493820,97134,88347,3676 +1493830,97248,88335,3676 +1493862,97248,88335,3676 +1493872,97356,88323,3676 +1493898,97356,88323,3676 +1493908,97461,88312,3677 +1493935,97461,88312,3677 +1493945,97564,88301,3677 +1494097,97665,88290,3677 +1494107,97780,88278,3676 +1494133,97780,88278,3676 +1494143,97892,88266,3675 +1494169,97892,88266,3675 +1494179,98004,88254,3674 +1494210,98004,88254,3674 +1494220,98113,88242,3674 +1494252,98113,88242,3674 +1494262,98219,88231,3674 +1494327,98326,88219,3674 +1494337,98326,88219,3674 +1494347,98428,88208,3675 +1494373,98428,88208,3675 +1494383,98523,88198,3675 +1494421,98523,88198,3675 +1494431,98617,88188,3675 +1494457,98617,88188,3675 +1494467,98706,88178,3675 +1494498,98706,88178,3675 +1494508,98796,88169,3676 +1494547,98796,88169,3676 +1494557,98883,88159,3676 +1494583,98883,88159,3676 +1494593,98966,88150,3676 +1494625,98966,88150,3676 +1494635,99051,88141,3676 +1494661,99051,88141,3676 +1494671,99132,88133,3676 +1494698,99132,88133,3676 +1494708,99205,88125,3677 +1494741,99205,88125,3677 +1494751,99279,88117,3677 +1494777,99279,88117,3677 +1494787,99344,88110,3678 +1494813,99344,88110,3678 +1494823,99410,88103,3678 +1494854,99410,88103,3678 +1494864,99471,88096,3678 +1494896,99471,88096,3678 +1494906,99532,88090,3678 +1494939,99532,88090,3678 +1494949,99597,88083,3678 +1494975,99597,88083,3678 +1494985,99660,88076,3679 +1495011,99660,88076,3679 +1495021,99716,88070,3679 +1495178,99776,88064,3679 +1495187,99838,88057,3677 +1495213,99838,88057,3677 +1495223,99905,88050,3676 +1495249,99905,88050,3676 +1495290,99966,88043,3676 +1495300,99966,88043,3676 +1495310,100023,88037,3676 +1495336,100023,88037,3676 +1495346,100081,88031,3676 +1495384,100081,88031,3676 +1495394,100132,88025,3676 +1495420,100132,88025,3676 +1495430,100184,88020,3676 +1495462,100184,88020,3676 +1495472,100231,88015,3676 +1495499,100231,88015,3676 +1495509,100280,88010,3677 +1495535,100280,88010,3677 +1495545,100321,88005,3677 +1495578,100321,88005,3677 +1495588,100361,88001,3678 +1495614,100361,88001,3678 +1495624,100405,87996,3678 +1495650,100405,87996,3678 +1495660,100449,87991,3678 +1495692,100449,87991,3678 +1495702,100491,87987,3679 +1495728,100491,87987,3679 +1495738,100532,87983,3679 +1495770,100532,87983,3679 +1495780,100569,87979,3679 +1495806,100569,87979,3679 +1495816,100605,87975,3680 +1495843,100605,87975,3680 +1495853,100641,87971,3680 +1495886,100641,87971,3680 +1495896,100671,87968,3680 +1495922,100671,87968,3680 +1495932,100699,87965,3681 +1495958,100699,87965,3681 +1495968,100727,87962,3681 +1495999,100727,87962,3681 +1496009,100753,87959,3681 +1496041,100753,87959,3681 +1496051,100774,87957,3681 +1496154,100797,87954,3681 +1496235,100812,87953,3680 +1496246,100812,87953,3680 +1496255,100828,87951,3679 +1496297,100844,87949,3678 +1496329,100844,87949,3678 +1496339,100860,87947,3677 +1496365,100860,87947,3677 +1496375,100878,87945,3677 +1496401,100878,87945,3677 +1496411,98871,88161,3678 +1496444,98871,88161,3678 +1496454,92949,88798,3678 +1496480,92949,88798,3678 +1496490,93668,88721,3679 +1496527,93668,88721,3679 +1496537,94509,88630,3680 +1496563,94509,88630,3680 +1496573,95694,88502,3681 +1496607,95694,88502,3681 +1496617,96769,88387,3681 +1496644,96769,88387,3681 +1496654,97730,88283,3682 +1496680,97730,88283,3682 +1496690,97505,88307,3683 +1496722,97505,88307,3683 +1496732,96985,88363,3683 +1496758,96985,88363,3683 +1497010,96705,88393,3684 +1497037,96705,88393,3684 +1497047,97551,88302,3680 +1497073,97551,88302,3680 +1497083,97974,88257,3678 +1497123,97974,88257,3678 +1497133,97805,88275,3676 +1497197,97608,88296,3675 +1497305,97764,88280,3674 +1497328,98461,88205,3672 +1497354,98461,88205,3672 +1497364,98808,88167,3671 +1497390,98808,88167,3671 +1497400,99198,88126,3670 +1497440,99198,88126,3670 +1497450,99543,88089,3670 +1497476,99543,88089,3670 +1497486,99785,88063,3670 +1497519,99785,88063,3670 +1497529,99708,88071,3669 +1497555,99708,88071,3669 +1497565,99498,88093,3669 +1497592,99498,88093,3669 +1497602,99337,88111,3669 +1497629,99337,88111,3669 +1497639,99332,88111,3670 +1497665,99332,88111,3670 +1497675,99601,88082,3670 +1497707,99601,88082,3670 +1497717,100026,88037,3670 +1497743,100026,88037,3670 +1497753,100205,88018,3670 +1497792,100205,88018,3670 +1497802,100319,88005,3670 +1497829,100319,88005,3670 +1497838,100454,87991,3670 +1497875,100454,87991,3670 +1497885,100584,87977,3669 +1497911,100584,87977,3669 +1497921,100677,87967,3669 +1497953,100677,87967,3669 +1497963,100613,87974,3669 +1497989,100613,87974,3669 +1497999,100564,87979,3669 +1498025,100564,87979,3669 +1498047,100567,87979,3669 +1498073,100567,87979,3669 +1498083,100455,87991,3669 +1498115,100455,87991,3669 +1498125,100171,88021,3668 +1498158,100171,88021,3668 +1498168,99937,88046,3668 +1498195,99937,88046,3668 +1498205,99788,88062,3668 +1498274,99862,88054,3668 +1498355,99913,88049,3667 +1498382,99913,88049,3667 +1498392,99922,88048,3666 +1498428,99975,88042,3665 +1498466,99975,88042,3665 +1498476,99952,88045,3665 +1498502,99952,88045,3665 +1498512,99912,88049,3665 +1498543,99912,88049,3665 +1498553,99877,88053,3665 +1498579,99877,88053,3665 +1498589,99840,88057,3665 +1498615,99840,88057,3665 +1498625,99801,88061,3665 +1498651,99801,88061,3665 +1498661,99771,88064,3665 +1498693,99771,88064,3665 +1498703,99734,88068,3666 +1498734,99734,88068,3666 +1498744,99697,88072,3666 +1498770,99697,88072,3666 +1498780,99666,88075,3666 +1498813,99666,88075,3666 +1498823,99638,88078,3667 +1498849,99638,88078,3667 +1498859,99609,88082,3667 +1498891,99609,88082,3667 +1498913,99598,88083,3667 +1498939,99598,88083,3667 +1498949,99582,88084,3667 +1498975,99582,88084,3667 +1498985,99555,88087,3667 +1499017,99555,88087,3667 +1499027,99515,88092,3667 +1499053,99515,88092,3667 +1499063,99473,88096,3668 +1499100,99473,88096,3668 +1499110,99472,88096,3668 +1499136,99472,88096,3668 +1499146,99496,88094,3668 +1499178,99496,88094,3668 +1499188,99494,88094,3668 +1499215,99494,88094,3668 +1499225,99467,88097,3669 +1499251,99467,88097,3669 +1499261,99447,88099,3669 +1499330,99428,88101,3669 +1499412,99406,88103,3668 +1499438,99406,88103,3668 +1499475,99388,88105,3667 +1499485,99378,88106,3667 +1499511,99378,88106,3667 +1499521,99369,88107,3666 +1499547,99369,88107,3666 +1499557,99364,88108,3667 +1499589,99364,88108,3667 +1499599,99367,88107,3667 +1499625,99367,88107,3667 +1499635,99361,88108,3667 +1499673,99361,88108,3667 +1499683,99307,88114,3667 +1499710,99307,88114,3667 +1499720,99254,88120,3667 +1499746,99254,88120,3667 +1499768,99215,88124,3667 +1499794,99215,88124,3667 +1499804,99177,88128,3667 +1499831,99177,88128,3667 +1499841,99137,88132,3667 +1499867,99137,88132,3667 +1499877,99107,88135,3668 +1499903,99107,88135,3668 +1499913,99084,88138,3668 +1499950,99084,88138,3668 +1499960,99053,88141,3668 +1499987,99053,88141,3668 +1499997,99011,88146,3668 +1500030,99011,88146,3668 +1500040,98968,88150,3668 +1500066,98968,88150,3668 +1500076,98934,88154,3668 +1500107,98934,88154,3668 +1500117,98899,88158,3668 +1500149,98899,88158,3668 +1500159,98869,88161,3668 +1500185,98869,88161,3668 +1500195,98842,88164,3668 +1500233,98842,88164,3668 +1500243,98820,88166,3668 +1500269,98820,88166,3668 +1500279,98800,88168,3668 +1500311,98800,88168,3668 +1500321,98778,88171,3668 +1500685,98748,88174,3668 +1500785,98677,88182,3665 \ No newline at end of file diff --git a/libraries/main.cpp b/libraries/main.cpp new file mode 100644 index 0000000..75eac86 --- /dev/null +++ b/libraries/main.cpp @@ -0,0 +1,18 @@ +#define DESKTOP + +#ifdef DESKTOP +#include +#include +#else +#include "Adafruit_BMP3XX.h" +#include "SD.H" +#endif +#include "mock_altimeter.h" + + +int main(void) +{ + std::cout << "heelo world" << std::endl; + MOCK_ALTIMETER altimeter = MOCK_ALTIMETER::getInstance(); + return 0; +} \ No newline at end of file diff --git a/libraries/mock_altimeter.cpp b/libraries/mock_altimeter.cpp new file mode 100644 index 0000000..9acdb7e --- /dev/null +++ b/libraries/mock_altimeter.cpp @@ -0,0 +1,241 @@ +#include "mock_altimeter.h" + + +#ifdef DESKTOP +#include +#include +#include +#include +#include +#else +#include "Adafruit_BMP3XX.h" +#include "SD.H" +#endif + + +#define BMP_SCK (13) +#define BMP_MISO (12) // SD0 +#define BMP_MOSI (11) // SD1 +#define BMP_CS (10) +#define SEALEVELPRESSURE_HPA (1013.7) +#define BMP_REFRESH_RATE 50 // Hz +#define MAX_ALTITUDE_STEP 100000 // cm + + +#ifdef DESKTOP +std::string MOCK_ALTIMETER::data_filename = std::string("data.csv"); +std::ifstream MOCK_ALTIMETER::data_file; +#else +File data_file; +#endif +int32_t MOCK_ALTIMETER::agl; +int32_t MOCK_ALTIMETER::press; +int16_t MOCK_ALTIMETER::temp; +unsigned long MOCK_ALTIMETER::time; + +MOCK_ALTIMETER& MOCK_ALTIMETER::getInstance() { + static MOCK_ALTIMETER instance; + return instance; +} + + +/* + NOTE to developer: + sensor.readAltitude(SEALEVELPRESSURE_HPA) reads the current altitude above sea level in meters. + It takes one argument, SEALEVELPRESSURE_HPA, which is the reference sea-level pressure that is used to calculate the altitude. + When the function is called, it first reads the current pressure and temperature values from the sensor using the sensor.pressure and sensor.temperature properties. + Then it calculates the altitude using the following equation: + Altitude = 44330.0 * (1.0 - pow(Pressure / SEALEVELPRESSURE_HPA, 0.1903)); + So when the the function is called, pressure, tempetature are `computed ` that time +*/ + + +#ifdef DESKTOP + +void MOCK_ALTIMETER::readSensorData() { + using namespace std::chrono; + auto now = system_clock::now(); + // if (duration_cast(now - last_sensor_read).count() < 1000/BMP_REFRESH_RATE || broken_connection) { + // return; + // } + updateValues(); + int32_t reading = agl + ground_level; + if (abs(reading - cached_altitude_cm) >= MAX_ALTITUDE_STEP) { + return; + } + cached_altitude_cm = reading; + // last_sensor_read = now; +} +#else +void MOCK_ALTIMETER::readSensorData() { + if (millis() - last_sensor_read < 1000/BMP_REFRESH_RATE || broken_connection) { + return; + } + + // int32_t reading = sensor.readAltitude(SEALEVELPRESSURE_HPA) * 100; <-- change this + updateValues(); + int32_t reading = aglCM + ground_level; + if (abs(reading - cached_altitude_cm) >= MAX_ALTITUDE_STEP) { + // Ignore reading if difference is extreme. Probably problem with the sensor. + return; + } + updateValues(); + reading = aglCM + ground_level; + // cached_altitude_cm = sensor.readAltitude(SEALEVELPRESSURE_HPA) * 100; <-- change this + cached_altitude_cm = reading; + last_sensor_read = millis(); +} +#endif + + +int32_t MOCK_ALTIMETER::altitude_cm() { + readSensorData(); + return cached_altitude_cm; +} + + +int32_t MOCK_ALTIMETER::pressure() { + return press; +} + + +int16_t MOCK_ALTIMETER::temperature() { + // not sure if multiplication is needed... + return temp; // original: temperature * 100 +} + + +int32_t MOCK_ALTIMETER::aglCM() { + return altitude_cm() - ground_level; +} + + +void MOCK_ALTIMETER::setGroundLevel() { + ground_level = altitude_cm(); +} + + +int32_t MOCK_ALTIMETER::getGroundLevelCM() { + return ground_level; +} + + +void MOCK_ALTIMETER::setup() { + #ifdef DESKTOP + std::cout << "SETUP READY" << std::endl; + #else + Serial.println("SETUP READY"); + #endif +} + + +void MOCK_ALTIMETER::calibrate() { + #ifdef DESKTOP + std::cout << "CALIBRATION READY" << std::endl; + #else + Serial.println("CALIBRATION READY"); + #endif +} + +#ifdef DESKTOP +void MOCK_ALTIMETER::openFile() { + if (!data_file.is_open()) { + data_file.open(data_filename); + } + std::cout << "opened file" << std::endl; + // skip first line (columns names) + readLine(); +} +#else +void MOCK_ALTIMETER::openFile() { + if (!SD.begin(BUILTIN_SDCARD)) { + Serial.println("SD card not found"); + return; + } + data_file = SD.open(data_filename, FILE_READ); + if (!data_file) { + // error message if file is not found + Serial.print("Mock Data not found: "); + return; + } + // skip first line (columns names) + readLine(); +} +#endif + +void MOCK_ALTIMETER::closeFile() { + data_file.close(); +} + + +#ifdef DESKTOP +std::string MOCK_ALTIMETER::readLine() { + if (!data_file.is_open()){ + openFile(); + } + std::string line; + if (MOCK_ALTIMETER::data_file.good()) { + std::getline(data_file, line); + } else { + std::cout << "not good" << std::endl; + return std::string(""); + } + return line; +} +#else +String MOCK_ALTIMETER::readLine() { + if (!data_file) { + openFile(); + } + String line; + if (data_file.available() && current_line < data_file.size()) { + line = data_file.readStringUntil('\n'); + current_line++; + } else { + return ""; + } + return line; +} +#endif + + +// Reads the next +// after the line is read it splits the line into different values, and updates the class attributes with the parsed values. +#ifdef DESKTOP +void MOCK_ALTIMETER::updateValues() { // desktop version + std::string line = readLine(); + // use std::stringstream to parse the line + std::stringstream line_stream(line); + std::string token; + // use std::getline to extract each token from the line + std::getline(line_stream, token, ','); + time = std::stoi(token); + std::getline(line_stream, token, ','); + agl = std::stoi(token); + std::getline(line_stream, token, ','); + press = std::stoi(token); + std::getline(line_stream, token); + temp = std::stoi(token); +} +#else +void MOCK_ALTIMETER::updateValues() { + + String line = readLine(); + int start_index = 0; + int end_index = line.indexOf(','); + time = line.substring(start_index, end_index).toInt(); + + start_index = end_index + 1; + end_index = line.indexOf(',', start_index); + AGL = line.substring(start_index, end_index).toInt(); + + start_index = end_index + 1; + end_index = line.indexOf(',', start_index); + pressure = line.substring(start_index, end_index).toInt(); + + start_index = end_index + 1; + temperature = line.substring(start_index).toInt(); +} +#endif + +MOCK_ALTIMETER::MOCK_ALTIMETER() {} \ No newline at end of file diff --git a/libraries/mock_altimeter.h b/libraries/mock_altimeter.h new file mode 100644 index 0000000..4e11aa1 --- /dev/null +++ b/libraries/mock_altimeter.h @@ -0,0 +1,72 @@ +#include "altimeter.h" + +#define DESKTOP +#ifdef DESKTOP +#include +#include +#include +#else +#include "Adafruit_BMP3XX.h" +#include "SD.H" +#endif + +/* + MOCK_ALTIMETER mimics BMP3XX + + Difference in implementation between MOCK_ALTIMETER and BMP3XX: + MOCK_ALTIMETER reads time, AGL, Pressure and Temperature from a file which contains data from a previous flight. + It uses the updateValues function to do this. + updateValues is basically the sensor.readAltitude(SEALEVELPRESSURE_HPA) * 100, EXCEPT THAT it does not return anything + however because altitude is needed in the readSensorData method, + the altitude is decoded from the AGL (altitude = aglCM+ground_level) +*/ + + + +class MOCK_ALTIMETER: public Altimeter +{ + #ifdef DESKTOP + static std::ifstream data_file; + static std::string data_filename; + #else + static File data_file; + #endif + static int32_t agl; + static int32_t press; + static int16_t temp; + static unsigned long time; + + public: + // file methods + static void openFile(); + static void closeFile(); // not used anywhere currently <- fix this + static std::string readLine(); + static MOCK_ALTIMETER& getInstance(); + static void updateValues(); + int32_t altitude_cm(); + int32_t pressure(); + int16_t temperature(); + int32_t aglCM(); + int32_t getGroundLevelCM(); + void setup(); + void calibrate(); + MOCK_ALTIMETER(); + private: + unsigned long last_sensor_read; + int32_t cached_altitude_cm; + int32_t ground_level; + void setGroundLevel(); + void readSensorData(); +}; + + + +/* + The Adafruit Feather M0 RFM96 is based on the ATSAMD21G18 chip which is an ARM Cortex-M0+ microcontroller. + The Feather M0 RFM96 also has a built-in SD card slot. + The SAMD21G18 has a speed of 48 MHz, it also has a 32-bit data bus width and 32-bit memory address space which are good numbers to work with an SD card. + The read speed of the SD card itself is also a important factor. + Standard SD card typically have a read speed of around 20MB/s to 25MB/s, while high-speed SD cards can have read speeds of up to 90MB/s. + Additionally, the SD library used can also have an impact on read performance. + Assuming we are working with a standard SD card with a read speed of around 20MB/s, it would take roughly 0.1s to read a 1MB file, and about 2s to read a 20MB file. +*/ \ No newline at end of file