diff --git a/drv/b15f.cpp b/drv/b15f.cpp index 0c41842..ebcef9f 100644 --- a/drv/b15f.cpp +++ b/drv/b15f.cpp @@ -4,6 +4,7 @@ B15F* B15F::instance = nullptr; B15F::B15F() { + init(); } void B15F::init() @@ -262,7 +263,7 @@ uint16_t B15F::analogRead(uint8_t channel) } } -bool B15F::analogEingabeSequenz(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count) +bool B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count) { discard(); @@ -280,10 +281,10 @@ bool B15F::analogEingabeSequenz(uint8_t channel_a, uint16_t* buffer_a, uint32_t if(aw != MSG_OK) { std::cout << PRE << "Out of sync" << std::endl; - return analogEingabeSequenz(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); + return analogSequence(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); } - uint8_t block[4]; + uint8_t block[5]; // 4 Datenbyte + crc for(uint16_t i = 0; i < count; i++) { bool crc_ok = readBlock(&block[0], 0); @@ -291,7 +292,7 @@ bool B15F::analogEingabeSequenz(uint8_t channel_a, uint16_t* buffer_a, uint32_t if (!crc_ok) { std::cout << PRE << "bad crc" << std::endl; - return analogEingabeSequenz(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); + return analogSequence(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); } @@ -304,12 +305,12 @@ bool B15F::analogEingabeSequenz(uint8_t channel_a, uint16_t* buffer_a, uint32_t return aw; std::cout << PRE << "Da ging etwas verloren" << std::endl; - return analogEingabeSequenz(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); + return analogSequence(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); } catch(DriverException& de) { reconnect(); - return analogEingabeSequenz(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); + return analogSequence(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); } } diff --git a/drv/b15f.h b/drv/b15f.h index aa0c9c8..c5a8790 100644 --- a/drv/b15f.h +++ b/drv/b15f.h @@ -37,7 +37,7 @@ public: bool analogWrite0(uint16_t); bool analogWrite1(uint16_t); uint16_t analogRead(uint8_t); - bool analogEingabeSequenz(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count); + bool analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count); // Serielle Verbindung inline void writeByte(uint8_t); @@ -53,7 +53,7 @@ public: private: int usart = -1; - uint16_t timeout = 32; // ms + uint16_t timeout = 100; // ms uint16_t block_timeout = 1; // ms int TEST = 0; @@ -66,7 +66,7 @@ private: constexpr static uint8_t MSG_FAIL = 0xFE; constexpr static uint16_t RECONNECT_TIMEOUT = 64; // ms constexpr static uint8_t RECONNECT_TRIES = 3; - constexpr static uint32_t BAUDRATE = 115200; + constexpr static uint32_t BAUDRATE = 38400; constexpr static uint8_t CRC7_POLY = 0x91; // REQUESTS diff --git a/drv/b15f.o b/drv/b15f.o index 3e69e16..f95b112 100644 Binary files a/drv/b15f.o and b/drv/b15f.o differ diff --git a/drv/plottyfile.cpp b/drv/plottyfile.cpp index 5259633..a2d3d5a 100644 --- a/drv/plottyfile.cpp +++ b/drv/plottyfile.cpp @@ -189,3 +189,10 @@ void PlottyFile::writeToFile(std::string filename) file.close(); } + +void PlottyFile::startPlotty(std::string filename) +{ + int code = system(("./plotty --in " + filename).c_str()); + if(code) + throw std::runtime_error("Fehler beim Aufruf von plotty"); +} diff --git a/drv/plottyfile.h b/drv/plottyfile.h index 5fc7465..17b02eb 100644 --- a/drv/plottyfile.h +++ b/drv/plottyfile.h @@ -47,6 +47,7 @@ public: std::string getDescPara(void) const; void writeToFile(std::string filename); + void startPlotty(std::string filename); private: void prepStr(std::string& str, uint8_t len); diff --git a/drv/plottyfile.o b/drv/plottyfile.o index ef4c3bb..561a627 100644 Binary files a/drv/plottyfile.o and b/drv/plottyfile.o differ diff --git a/main b/main index 5e14dec..c5917cc 100755 Binary files a/main and b/main differ diff --git a/main.cpp b/main.cpp index 60a41d3..b208336 100644 --- a/main.cpp +++ b/main.cpp @@ -2,11 +2,11 @@ #include "drv/b15f.h" #include "drv/plottyfile.h" -PlottyFile pf; void kennlinieErsterQuadrant() { B15F& drv = B15F::getInstance(); + PlottyFile pf; uint16_t ba[1024]; uint16_t bb[1024]; @@ -34,7 +34,7 @@ void kennlinieErsterQuadrant() { drv.analogWrite1(u_gs); - drv.analogEingabeSequenz(0, &ba[0], 0, 1, &bb[0], 0, 0, delta, sample_count); + drv.analogSequence(0, &ba[0], 0, 1, &bb[0], 0, 0, delta, sample_count); drv.delay(10); drv.discard(); drv.delay(10); @@ -48,11 +48,16 @@ void kennlinieErsterQuadrant() curve++; } + + // speichern und plotty starten + pf.writeToFile("test_plot"); + pf.startPlotty("test_plot"); } void kennlinieZweiterQuadrant() { B15F& drv = B15F::getInstance(); + PlottyFile pf; uint16_t ba[1024]; uint16_t bb[1024]; @@ -81,7 +86,7 @@ void kennlinieZweiterQuadrant() { drv.analogWrite1(u_gs); - drv.analogEingabeSequenz(0, &ba[0], 0, 1, &bb[0], 0, 0, delta, sample_count); + drv.analogSequence(0, &ba[0], 0, 1, &bb[0], 0, 0, delta, sample_count); curve = 0; for(uint16_t k = 0; k < sample_count + 1; k++) @@ -95,28 +100,74 @@ void kennlinieZweiterQuadrant() } std::cout << "u_gs: " << u_gs << std::endl; } + + // speichern und plotty starten + pf.writeToFile("test_plot"); + pf.startPlotty("test_plot"); +} + +void beispielFunktionen() +{ + B15F& drv = B15F::getInstance(); + + /* + for(uint16_t i = 0; i < 256; i++) + { + drv.digitalWrite0(i); + drv.delay(50); + }*/ + + uint16_t schwelle_unten = 1023; + for(uint16_t i = 0; i < 1024; i++) + { + drv.analogWrite0(i); + drv.delay(1); + if(drv.digitalRead0() & 0x01) + { + drv.discard(); + uint16_t val = drv.analogRead(0); + if(val > 1023) + { + std::cout << "Fehler: " << val << std::endl; + continue; + } + if(val < schwelle_unten) + schwelle_unten = val; + } + } + + std::cout << "OK" << std::endl; + + uint16_t schwelle_oben = 0; + for(uint16_t i = 1023; i > 0; i--) + { + drv.analogWrite0(i); + drv.delay(1); + if(!(drv.digitalRead0() & 0x01)) + { + drv.discard(); + uint16_t val = drv.analogRead(0); + if(val > 1023) + { + std::cout << "Fehler: " << val << std::endl; + continue; + } + if(val > schwelle_oben) + schwelle_oben = val; + } + } + + std::cout << "Schwelle für low: " << schwelle_unten << std::endl; + std::cout << "Schwelle für high: " << schwelle_oben << std::endl; + std::cout << "Verbotene Zone: " << (schwelle_oben - schwelle_unten) << std::endl; } int main() { - B15F& drv = B15F::getInstance(); - drv.init(); - int n = 0; - while(1) - { - uint16_t ba[1024]; - uint16_t bb[1024]; - drv.analogEingabeSequenz(0, &ba[0], 0, 1, &bb[0], 0, 0, 1, 1023); - - std::cout << n++ << std::endl; - } + beispielFunktionen(); + - // speichern und plotty starten - pf.writeToFile("test_plot"); - int code = system("./plotty --in test_plot"); - if(code) - std::cout << "Fehler beim plotty Aufruf" << std::endl; std::cout << "Schluss." << std::endl; } diff --git a/main.o b/main.o index 7878d59..d3c8707 100644 Binary files a/main.o and b/main.o differ diff --git a/test_plot b/test_plot index 8eaed19..53376b0 100644 Binary files a/test_plot and b/test_plot differ