Newer
Older
Malaurie Bernard
committed
#include "syringefilled.h"
Malaurie Bernard
committed
ISRStepper stepper(AccelStepper::DRIVER, STEP, DIR);
SyringeFilled syringe_filled(stepper);
Malaurie Bernard
committed
SyringeFilled :: SyringeFilled (MotorHardware_t& stepper): Motor(stepper)
Malaurie Bernard
committed
{
set_exchange_throughput_uL_per_sec(1);
set_exchange_volume_mL(1);
set_remaining_volume_mL(1);
set_push(true);
Malaurie Bernard
committed
set_name_syringe("BD_10mL");
Malaurie Bernard
committed
set_clockwise_equals_push(true);
set_lead_screw_pitch_mm(0.7);
set_syringe_pump_length_mm(50);
set_microstep_configuration(16);
set_motor_steps(200);
Malaurie Bernard
committed
set_emergency(false);
}
Malaurie Bernard
committed
Malaurie Bernard
committed
void SyringeFilled :: set_exchange_throughput_uL_per_sec(float exchange_throughput_uL_per_sec)
{
Malaurie Bernard
committed
syringe_filled_json["exchange_throughput_uL_per_sec"] = exchange_throughput_uL_per_sec;
Malaurie Bernard
committed
}
void SyringeFilled :: set_exchange_volume_mL(float exchange_volume_mL)
{
Malaurie Bernard
committed
syringe_filled_json["exchange_volume_mL"] = exchange_volume_mL;
Malaurie Bernard
committed
}
void SyringeFilled :: set_remaining_volume_mL(float remaining_volume_mL)
{
Malaurie Bernard
committed
syringe_filled_json["remaining_volume_mL"] = remaining_volume_mL;
Malaurie Bernard
committed
}
void SyringeFilled :: set_push(bool push)
{
Malaurie Bernard
committed
syringe_filled_json["push"] = push;
Malaurie Bernard
committed
}
void SyringeFilled :: set_name_syringe(String name_syringe)
{
syringe_filled_json["name_syringe"] = name_syringe;
}
void SyringeFilled :: set_clockwise_equals_push(bool clockwise_equals_push)
{
Malaurie Bernard
committed
syringe_filled_json["clockwise_equals_push"] = clockwise_equals_push;
void SyringeFilled :: set_syringe_pump_length_mm(float syringe_pump_length_mm)
{
syringe_filled_json["syringe_pump_length_mm"] = syringe_pump_length_mm;
}
void SyringeFilled :: set_lead_screw_pitch_mm(float lead_screw_pitch_mm)
{
syringe_filled_json["lead_screw_pitch_mm"] = lead_screw_pitch_mm;
}
void SyringeFilled :: set_microstep_configuration(int microstep_configuration)
{
syringe_filled_json["microstep_configuration"] = microstep_configuration;
}
void SyringeFilled :: set_motor_steps(int motor_steps)
{
syringe_filled_json["motor_steps"] = motor_steps;
}
void SyringeFilled :: set_emergency(bool emergency)
{
Malaurie Bernard
committed
syringe_filled_json["emergency"] = emergency;
}
Malaurie Bernard
committed
Malaurie Bernard
committed
float SyringeFilled :: get_exchange_throughput_uL_per_sec()
{
Malaurie Bernard
committed
return syringe_filled_json["exchange_throughput_uL_per_sec"].as<float>();
Malaurie Bernard
committed
}
float SyringeFilled :: get_exchange_volume_mL()
{
Malaurie Bernard
committed
return syringe_filled_json["exchange_volume_mL"].as<float>();
Malaurie Bernard
committed
}
float SyringeFilled :: get_remaining_volume_mL()
{
Malaurie Bernard
committed
return syringe_filled_json["remaining_volume_mL"].as<float>();
Malaurie Bernard
committed
}
bool SyringeFilled :: get_push()
{
Malaurie Bernard
committed
return syringe_filled_json["push"].as<bool>();
Malaurie Bernard
committed
}
Malaurie Bernard
committed
String SyringeFilled :: get_name_syringe()
Malaurie Bernard
committed
{
Malaurie Bernard
committed
return syringe_filled_json["name_syringe"].as<String>();
Malaurie Bernard
committed
}
bool SyringeFilled :: get_clockwise_equals_push()
{
return syringe_filled_json["clockwise_equals_push"].as<bool>();
}
float SyringeFilled :: get_syringe_pump_length_mm()
{
return syringe_filled_json["syringe_pump_length_mm"].as<float>();
}
float SyringeFilled :: get_lead_screw_pitch_mm()
{
return syringe_filled_json["lead_screw_pitch_mm"].as<float>();
}
int SyringeFilled :: get_microstep_configuration()
{
return syringe_filled_json["microstep_configuration"].as<int>();
}
int SyringeFilled :: get_motor_steps()
{
return syringe_filled_json["motor_steps"].as<int>();
}
bool SyringeFilled :: get_emergency()
{
Malaurie Bernard
committed
return syringe_filled_json["emergency"].as<bool>();
Malaurie Bernard
committed
const StaticJsonDocument<200>& SyringeFilled :: get_syringe_filled_data()
{
Malaurie Bernard
committed
return syringe_filled_json;
Malaurie Bernard
committed
//CONVERSIONS
Malaurie Bernard
committed
StaticJsonDocument<200> syringe_filled_json; //200 = RAM allocated to this document
float SyringeFilled::volume_mL_to_distance_mm(float volume_mL)
/***
-Argument : Volume in mL.
-Return : The distance to cover associated in mm.
-Action : /
***/
{
float volume_mm3 = 1000 * volume_mL; //mL->mm3
float piston_radius_mm = (syringe.get_syringe_database()[syringe_filled_json[syringe_filled.get_name_syringe()]]["internal_diameter"]).as<int>()/2;
float piston_surface_mm2 = M_PI * piston_radius_mm * piston_radius_mm;
float distance_mm = volume_mm3 / piston_surface_mm2;
return distance_mm;
}
float SyringeFilled::distance_mm_to_volume_mL(float distance_mm)
Malaurie Bernard
committed
/***
-Argument : distance in mm.
-Return : The volume associated in mL.
-Action : /
***/
{
float piston_radius_mm = (syringe.get_syringe_database()[syringe_filled_json[syringe_filled.get_name_syringe()]]["internal_diameter"]).as<int>()/2;
float piston_surface_mm2 = M_PI * piston_radius_mm * piston_radius_mm;
float volume_mm3 = distance_mm * piston_surface_mm2;
Malaurie Bernard
committed
Malaurie Bernard
committed
Malaurie Bernard
committed
}
Malaurie Bernard
committed
//MOVEMENTS
Malaurie Bernard
committed
bool SyringeFilled :: is_running () //return true if the motor is currently running
/***
-Argument : /
-Return : A boolean to indicates if the motor is running.
-Action : /
***/
{
return motor_is_running();
}
Malaurie Bernard
committed
void SyringeFilled :: start_exchange()
/***
-Argument : / (We're using informations stored in the class itself)
-Return : /
-Action : Move the syringe filled to deliver or recover the volume expected
***/
{
//Conversions of volumes
float exchange_volume_mm3 = get_exchange_volume_mL()*1000;
//Find the section
Malaurie Bernard
committed
float radius = syringe.get_syringe_database()[syringe_filled_json["name_syringe"]]["internal_diameter_mm"];
float section_mm2 = M_PI*radius*radius;
set_speed_mm_per_sec(get_exchange_throughput_uL_per_sec()/section_mm2);
Malaurie Bernard
committed
if (get_push()) //If we want to deliver some liquid
{
Malaurie Bernard
committed
move_mm_limit_switch(_clockwise_equals_push? exchange_volume_mm3/section_mm2: -exchange_volume_mm3/section_mm2);
}
Malaurie Bernard
committed
else //If we want to recover some liquid
{
Malaurie Bernard
committed
move_to_mm_limit_switch(_clockwise_equals_push? -exchange_volume_mm3/section_mm2: exchange_volume_mm3/section_mm2);
Malaurie Bernard
committed
}
}
Malaurie Bernard
committed
void SyringeFilled :: go_to_zero()
/***
-Argument : / (We're using informations stored in the class itself)
-Return : /
-Action : Move the syringe filled to deliver or recover the volume expected
***/
{
if (get_emergency())
else
{
set_speed_mm_per_sec(-1);
reset_position();
Malaurie Bernard
committed
move_to_mm_limit_switch(_clockwise_equals_push? -200: 200);
}
}
//LIMIT SWITCH
void SyringeFilled::manage_emergency (bool pressed, bool at_zero)
/***
-Argument : two boolean to know the position of the limit switch and if we are a the zero
-Return : /
-Action : Manage different emergency cases (move the pump if necessary)
***/
{
if (pressed) //if the limit switch is pressed
{
set_emergency(true);
if (at_zero) //if we are at te initialisation place
{
Serial.printf("EMERGENCY: Pressed by someone\n");
}
}
else
{
set_emergency(false);
Serial.printf("EMERGENCY: released\n");
stop();
reset_position(); //zero is here again
Serial.printf("ZERO: reset\n");
}
}
Malaurie Bernard
committed
void SyringeFilled::run_from_emergency()
/***
-Argument : /
-Return : /
-Action : Move the pump slowly until the limit switch is released
***/
{
if (get_emergency())
{
Serial.printf("EMERGENCY: running away slowly\n");
set_speed_mm_per_sec(-1);
reset_position();
Malaurie Bernard
committed
move_to_mm_limit_switch(_clockwise_equals_push? 200: -200);
Malaurie Bernard
committed
}
//CONFIGURATION
Malaurie Bernard
committed
bool SyringeFilled :: check_configuration()
/***
-Argument : /
-Return : A boolean true(everything is fine), false(at least one attribut is'nt correct)
-Action : Check if the values are correct.
***/
{
Malaurie Bernard
committed
if (syringe.get_syringe_database()[syringe_filled_json["name_syringe"]]["internal_diameter"] <= 0)
Malaurie Bernard
committed
return false;
if (get_exchange_volume_mL() < 0)
return false;
if (get_remaining_volume_mL() <= 0)
return false;
if (get_exchange_throughput_uL_per_sec() <= 0)
return false;
return true;
}
Malaurie Bernard
committed
void SyringeFilled :: show_configuration()
/***
-Argument : /
-Return : /
-Action : Displays syringe filled attributes
***/
{
//Motor::show_configuration();
Malaurie Bernard
committed
auto step = where_step();
Malaurie Bernard
committed
Serial.printf("# push clockwise: %s\n"
//"# diameter: %g mm\n"
//"# capacity: %g uL\n"
Malaurie Bernard
committed
"# rate: %g uL/s = %g mm/s\n"
"# volume remaining: %g uL (target)\n"
"# direction: %s\n"
"# emergency: %d\n"
"# current position: %g mm\n",
get_clockwise_equals_push()? "yes": "no",
Malaurie Bernard
committed
//syringe.get_syringe_database()[syringe_filled_json["name_syringe"]]["internal_diameter"],
//syringe.get_syringe_database()[syringe_filled_json["name_syringe"]]["total_volume_mL"],
Malaurie Bernard
committed
get_exchange_throughput_uL_per_sec(),
volume_mL_to_distance_mm(get_exchange_throughput_uL_per_sec()),
Malaurie Bernard
committed
get_remaining_volume_mL(),
get_push()? "infuse": "withdraw",
get_emergency(),
step_to_mm(step));
Malaurie Bernard
committed
}
Malaurie Bernard
committed
Malaurie Bernard
committed
//Example : File f = ({ InterruptLock lock; filesystem->open(filename, "w"); }); and the f becomes the argument input_stream
Malaurie Bernard
committed
//useful in web.cpp via a button to save data
Malaurie Bernard
committed
void SyringeFilled :: write_Json ()
Malaurie Bernard
committed
/***
Malaurie Bernard
committed
-Argument : input_stream (= serial or a file)
Malaurie Bernard
committed
-Return : /
-Action : Save the Json structure in a file (convert it to text)
***/
Malaurie Bernard
committed
{
Malaurie Bernard
committed
InterruptLock lock; //useful thanks to its constructor (so a is not visible in the code)
Malaurie Bernard
committed
File f = filesystem->open("syringe_filled_json", "w"); //Creation du fichier
size_t a = serializeJson(syringe_filled_json, f);
Serial.println("Taille de syringe_filled_json");
Serial.println(a);
f.close();
Malaurie Bernard
committed
}
//useful in setup of pousse-seringue.cpp(to check at the begining what has already been saved) and web.cpp via a button to recover data
Malaurie Bernard
committed
void SyringeFilled :: read_Json ()
Malaurie Bernard
committed
/***
Malaurie Bernard
committed
-Argument : output_stream (= serial or a file)
Malaurie Bernard
committed
-Return : /
-Action : Check if a Json Document alredy exist or not, if it does it is load
Malaurie Bernard
committed
***/
Malaurie Bernard
committed
{
Malaurie Bernard
committed
InterruptLock lock; //useful thanks to its constructor (so a is not visible in the code)
Malaurie Bernard
committed
File f = filesystem->open("syringe_filled_json", "r"); //Creation du fichier
deserializeJson(syringe_filled_json, f);
Malaurie Bernard
committed
}