Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for USB Serial Communication to FED3 unit #63

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions src/FED3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,29 @@ void FED3::writeHeader() {
logfile.close();
}

void FED3::sendHeaders() {
digitalWrite (MOTOR_ENABLE, LOW); //Disable motor driver and neopixel
// Write data header to file of microSD card


if (tempSensor == false){
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.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)
void FED3::writeConfigFile() {
digitalWrite (MOTOR_ENABLE, LOW); //Disable motor driver and neopixel
Expand All @@ -975,8 +998,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
}
Expand Down
8 changes: 8 additions & 0 deletions src/FED3.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ This device includes hardware and code from:
#define STEPS 2038

extern bool Left;
extern RTC_PCF8523 rtc;

class FED3 {
// Members
Expand All @@ -87,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);
Expand Down Expand Up @@ -143,6 +146,9 @@ class FED3 {
void SelectMode();
void SetDeviceNumber();

//Experement Wipe
void rest_vars();

// Stimuli
void ConditionedStimulus(int duration = 200);
void Click();
Expand Down Expand Up @@ -204,6 +210,7 @@ class FED3 {
unsigned long displayupdate;
String Event = "None"; //What kind of event just happened?


// timing variables
int retInterval = 0;
int leftInterval = 0;
Expand All @@ -220,6 +227,7 @@ class FED3 {
bool EnableSleep = true;
void disableSleep();
void enableSleep();
bool SerialLogging = false;
bool ClassicFED3 = false;
bool FED3Menu = false;
bool tempSensor = false;
Expand Down