From 273133f4647de44eedd19b0ee6ce107e1a30db5a Mon Sep 17 00:00:00 2001 From: Colin <44309126+cxider@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:38:13 -0600 Subject: [PATCH 1/3] Added USB Serial Logging Support to FED3 Library Adds support for using sending data over the serial connection in the fed3 to a computer --- src/FED3.cpp | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/FED3.h | 6 +++ 2 files changed, 138 insertions(+) diff --git a/src/FED3.cpp b/src/FED3.cpp index 7251ebc..177a95e 100644 --- a/src/FED3.cpp +++ b/src/FED3.cpp @@ -975,8 +975,140 @@ void FED3::writeConfigFile() { configfile.close(); } +//Reset Experiment Variables +void FED3::rest_vars(){ + LeftCount = 0; + RightCount = 0; + PelletCount = 0; + BlockPelletCount = 0; + retInterval = 0; + leftInterval = 0; + rightInterval = 0; + leftPokeTime = 0; + rightPokeTime = 0; + pelletTime = 0; + lastPellet = 0; + unixtime = 0; + interPelletInterval = 0; +} +// Log Data to Serial Connection +void FED3::logserial(){ + DateTime now = rtc.now(); + + Serial.print(now.month()); + Serial.print("/"); + Serial.print(now.day()); + Serial.print("/"); + Serial.print(now.year()); + Serial.print(" "); + Serial.print(now.hour()); + Serial.print(":"); + if (now.minute() < 10) + Serial.print('0'); // Trick to add leading zero for formatting + Serial.print(now.minute()); + Serial.print(":"); + if (now.second() < 10) + Serial.print('0'); // Trick to add leading zero for formatting + Serial.print(now.second()); + Serial.print(","); + + if(tempSensor == true) { + sensors_event_t humidity, temp; + aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data + Serial.print (temp.temperature); + Serial.print(","); + Serial.print (humidity.relative_humidity); + Serial.print(","); + } + + Serial.print(VER); // Print library version + Serial.print(","); + + Serial.print(sessiontype); //print Sketch identifier + Serial.print(","); + + Serial.print(FED); // + Serial.print(","); + + Serial.print(measuredvbat); // + Serial.print(","); + + if (Event != "Pellet"){ + Serial.print(sqrt (-1)); // print NaN if it's not a pellet Event + Serial.print(","); + } + else { + Serial.print(numMotorTurns+1); // Print the number of attempts to dispense a pellet + Serial.print(","); + } + + Serial.print(FR); + Serial.print(","); + + Serial.print(Event); + Serial.print(","); + + if (activePoke == 0) Serial.print("Right"); // + if (activePoke == 1) Serial.print("Left"); // + Serial.print(","); + + Serial.print(LeftCount); // Print Left poke count + Serial.print(","); + + Serial.print(RightCount); // Print Right poke count + Serial.print(","); + + Serial.print(PelletCount); // print Pellet counts + Serial.print(","); + + Serial.print(BlockPelletCount); // print Block Pellet counts + Serial.print(","); + + if (Event != "Pellet"){ + Serial.print(sqrt (-1)); // print NaN if it's not a pellet Event + } + else if (retInterval < 60000 ) { // only log retrieval intervals below 1 minute (FED should not record any longer than this) + Serial.print(retInterval/1000.000); // print interval between pellet dispensing and being taken + } + else if (retInterval >= 60000) { + Serial.print("Timed_out"); // print "Timed_out" if retreival interval is >60s + } + else { + Serial.print("Error"); // print error if value is < 0 (this shouldn't ever happen) + } + Serial.print(","); + + if ((Event != "Pellet") or (PelletCount < 2)){ + Serial.print(sqrt (-1)); // print NaN if it's not a pellet Event + } + else { + Serial.print (interPelletInterval); + } + Serial.print(","); + + if (Event == "Pellet"){ + Serial.println(sqrt (-1)); // print NaN + } + + else if ((Event == "Left") or (Event == "LeftShort") or (Event == "LeftWithPellet") or (Event == "LeftinTimeout") or (Event == "LeftDuringDispense")) { // + Serial.println(leftInterval/1000.000); // print left poke timing + } + + else if ((Event == "Right") or (Event == "RightShort") or (Event == "RightWithPellet") or (Event == "RightinTimeout") or (Event == "RightDuringDispense")) { // + Serial.println(rightInterval/1000.000); // print left poke timing + } + + else { + Serial.println(sqrt (-1)); // print NaN + } + Serial.print('\0'); +} + //Write to SD card void FED3::logdata() { + + if(SerialLogging == true) logserial(); //preform a Serial Log if enabled + if (EnableSleep==true){ digitalWrite (MOTOR_ENABLE, LOW); //Disable motor driver and neopixel } diff --git a/src/FED3.h b/src/FED3.h index 055850d..36ba3a3 100644 --- a/src/FED3.h +++ b/src/FED3.h @@ -66,6 +66,7 @@ This device includes hardware and code from: #define STEPS 2038 extern bool Left; +extern RTC_PCF8523 rtc; class FED3 { // Members @@ -143,6 +144,9 @@ class FED3 { void SelectMode(); void SetDeviceNumber(); + //Experement Wipe + void rest_vars(); + // Stimuli void ConditionedStimulus(int duration = 200); void Click(); @@ -204,6 +208,7 @@ class FED3 { unsigned long displayupdate; String Event = "None"; //What kind of event just happened? + // timing variables int retInterval = 0; int leftInterval = 0; @@ -220,6 +225,7 @@ class FED3 { bool EnableSleep = true; void disableSleep(); void enableSleep(); + bool SerialLogging = false; bool ClassicFED3 = false; bool FED3Menu = false; bool tempSensor = false; From e64eac9815233108251d2f285a0405af99b86927 Mon Sep 17 00:00:00 2001 From: Colin <44309126+cxider@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:41:30 -0600 Subject: [PATCH 2/3] Support for Sending Headers Over Serial This allows you to send the headers for the log file over serial connection --- src/FED3.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/FED3.cpp b/src/FED3.cpp index 177a95e..ef9a0fb 100644 --- a/src/FED3.cpp +++ b/src/FED3.cpp @@ -965,6 +965,19 @@ void FED3::writeHeader() { logfile.close(); } +void FED3::sendHeader() { + digitalWrite (MOTOR_ENABLE, LOW); //Disable motor driver and neopixel + // Write data header to file of microSD card + + + if (tempSensor == false){ + Serial.println("MM:DD:YYYY hh:mm:ss,Library_Version,Session_type,Device_Number,Battery_Voltage,Motor_Turns,FR,Event,Active_Poke,Left_Poke_Count,Right_Poke_Count,Pellet_Count,Block_Pellet_Count,Retrieval_Time,InterPelletInterval,Poke_Time"); + } else { + Serial.println("MM:DD:YYYY hh:mm:ss,Temp,Humidity,Library_Version,Session_type,Device_Number,Battery_Voltage,Motor_Turns,FR,Event,Active_Poke,Left_Poke_Count,Right_Poke_Count,Pellet_Count,Block_Pellet_Count,Retrieval_Time,InterPelletInterval,Poke_Time"); + } + Serial.print('\0'); +} + //write a configfile (this contains the FED device number) void FED3::writeConfigFile() { digitalWrite (MOTOR_ENABLE, LOW); //Disable motor driver and neopixel @@ -1106,7 +1119,7 @@ void FED3::logserial(){ //Write to SD card void FED3::logdata() { - + if(SerialLogging == true) logserial(); //preform a Serial Log if enabled if (EnableSleep==true){ From 34bfc3bac93cd61b46d2ca49f0cad3708f1e8ac4 Mon Sep 17 00:00:00 2001 From: Colin <44309126+cxider@users.noreply.github.com> Date: Tue, 15 Nov 2022 14:56:34 -0600 Subject: [PATCH 3/3] Fixed Delay Issues When Sending Headers --- src/FED3.cpp | 20 +++++++++++++++----- src/FED3.h | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/FED3.cpp b/src/FED3.cpp index ef9a0fb..8623bf8 100644 --- a/src/FED3.cpp +++ b/src/FED3.cpp @@ -965,17 +965,27 @@ void FED3::writeHeader() { logfile.close(); } -void FED3::sendHeader() { +void FED3::sendHeaders() { digitalWrite (MOTOR_ENABLE, LOW); //Disable motor driver and neopixel // Write data header to file of microSD card if (tempSensor == false){ - Serial.println("MM:DD:YYYY hh:mm:ss,Library_Version,Session_type,Device_Number,Battery_Voltage,Motor_Turns,FR,Event,Active_Poke,Left_Poke_Count,Right_Poke_Count,Pellet_Count,Block_Pellet_Count,Retrieval_Time,InterPelletInterval,Poke_Time"); + Serial.print("Unix Time(s),Library_Version,Session_type,Device_Number,"); + Serial.print("Battery_Voltage,Motor_Turns,FR,Event,Active_Poke,"); + Serial.write('\0'); + delay(200); + Serial.print("Left_Poke_Count,Right_Poke_Count,Pellet_Count,"); + Serial.print("Block_Pellet_Count,Retrieval_Time,InterPelletInterval,Poke_Time"); } else { - Serial.println("MM:DD:YYYY hh:mm:ss,Temp,Humidity,Library_Version,Session_type,Device_Number,Battery_Voltage,Motor_Turns,FR,Event,Active_Poke,Left_Poke_Count,Right_Poke_Count,Pellet_Count,Block_Pellet_Count,Retrieval_Time,InterPelletInterval,Poke_Time"); - } - Serial.print('\0'); + Serial.print("Unix Time(s),Temp,Humidity,Library_Version,Session_type,Device_Number,"); + Serial.print("Battery_Voltage,Motor_Turns,FR,Event,Active_Poke,"); + Serial.write('\0'); + delay(200); + Serial.print("Left_Poke_Count,Right_Poke_Count,Pellet_Count,"); + Serial.print("Block_Pellet_Count,Retrieval_Time,InterPelletInterval,Poke_Time"); + } + Serial.write('\0'); } //write a configfile (this contains the FED device number) diff --git a/src/FED3.h b/src/FED3.h index 36ba3a3..9ee4079 100644 --- a/src/FED3.h +++ b/src/FED3.h @@ -88,9 +88,11 @@ class FED3 { File stopfile; // Create another file object char filename[21]; // Array for file name data logged to named in setup void logdata(); + void logserial(); void CreateFile(); void CreateDataFile (); void writeHeader(); + void sendHeaders(); void writeConfigFile(); void writeFEDmode(); void error(uint8_t errno);