Commit 532a7a5a authored by Cristina Cocho's avatar Cristina Cocho

Modifications on MSH150Driver after tests

parent 360367bc
......@@ -66,6 +66,7 @@ void MSH150Driver::execute(const std::string& aCommand) {
if (aCommand == driver::START_COMMAND) {
//begin reading
currentState->start();
commandProgression = 10;
} else if (aCommand == driver::STOP_COMMAND) {
currentState->stop();
......
......@@ -28,6 +28,8 @@ RealMSH150Driver::RealMSH150Driver(MSH150Driver* owner) :
ctx = nullptr;
dev_handle = nullptr;
devs = nullptr;
isInit = false;
}
RealMSH150Driver::~RealMSH150Driver() {
......@@ -46,40 +48,60 @@ void RealMSH150Driver::init() {
if (r < 0) {
std::cout << "Init Error " << r << std::endl; //there was an error
}
/*
int32 cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
if (cnt < 0) {
std::cout << "Get Device Error" << std::endl; //there was an error
}
std::cout << cnt << " Devices in list." << std::endl; //print total number of usb devices
ssize_t i; //for iterating through the list
for (i = 0; i < cnt; i++) {
int b=libusb_open(devs[i],&dev_handle);
if (b < 0) {
std::cout << " Error opening " << i << std::endl;
}
}
*/
// Open the device using the vendorID and the productID
std::cout << "--> init " << owner()->vendorID() << " " << owner()->productID() << std::endl;
dev_handle = libusb_open_device_with_vid_pid(ctx, owner()->vendorID(), owner()->productID());
if (dev_handle == nullptr) {
std::cout << "Cannot open the device" << std::endl;
}
} else {
// Claim the interface -- needed?
r = libusb_claim_interface(dev_handle, 0);
if (r < 0) {
std::cout << "Cannot Claim Interface" << std::endl;
// Test here detaching
r = libusb_detach_kernel_driver(dev_handle, 0);
if (r == 0) {
std::cout << "Detach successful... claim again?" << std::endl;
// Claim the interface -- needed?
r = libusb_claim_interface(dev_handle, 0);
if (r < 0) {
std::cout << "Cannot Claim Interface" << std::endl;
// Test here detaching
r = libusb_detach_kernel_driver(dev_handle, 0);
if (r == 0) {
std::cout << "Detach successful... claim again?" << std::endl;
}
}
isInit = true;
}
}
void RealMSH150Driver::start() {
// Set system in remote mode
writeCommand(SET_SYSTEM_REMOTE);
// Set wavelength
std::ostringstream stream;
stream << SET_MONO_WAVE << owner()->wavelength.setpoint() << std::endl;
writeCommand(stream.str());
writeCommand(SET_MONO_MOVE);
// Set filter position
std::ostringstream stream2;
stream2 << SET_MONO_FILTER_POSITION << owner()->filterPosition.setpoint() << std::endl;
writeCommand(stream2.str());
writeCommand(SET_MONO_MOVE);
if (isInit) {
// Set system in remote mode
writeCommand(SET_SYSTEM_REMOTE);
// Set wavelength
std::ostringstream stream;
stream << SET_MONO_WAVE << owner()->wavelength.setpoint() << std::endl;
writeCommand(stream.str());
writeCommand(SET_MONO_MOVE);
// Set filter position
std::ostringstream stream2;
stream2 << SET_MONO_FILTER_POSITION << owner()->filterPosition.setpoint() << std::endl;
writeCommand(stream2.str());
writeCommand(SET_MONO_MOVE);
}
}
......@@ -88,25 +110,28 @@ void RealMSH150Driver::stop() {
}
void RealMSH150Driver::read() {
writeCommand(MONO_WAVE_REQUEST);
std::string answer = readCommand();
if (answer.find(",") != std::string::npos ){
msh150::CurrentTargetAnswer fields = getFields(answer);
owner()->wavelength = std::stof(fields.current);
}
usleep(1e5);
if (isInit) {
writeCommand(MONO_WAVE_REQUEST);
std::string answer = readCommand();
if (answer.find(",") != std::string::npos) {
msh150::CurrentTargetAnswer fields = getFields(answer);
owner()->wavelength = std::stof(fields.current);
}
usleep(1e5);
writeCommand(MONO_FILTER_POSITION_REQUEST);
std::string answer2 = readCommand();
if (answer.find(",") != std::string::npos) {
msh150::CurrentTargetAnswer fields2 = getFields(answer2);
owner()->filterPosition = std::stof(fields2.current);
writeCommand(MONO_FILTER_POSITION_REQUEST);
std::string answer2 = readCommand();
if (answer.find(",") != std::string::npos) {
msh150::CurrentTargetAnswer fields2 = getFields(answer2);
owner()->filterPosition = std::stof(fields2.current);
// owner()->filterPosition.setpoint = std::stof(fields2.target);
}
}
// Unblock when setpoint of both wavelength and filter position is reached
if ((owner()->wavelength() == owner()->wavelength.setpoint()) && (owner()->filterPosition() == owner()->filterPosition.setpoint())) {
owner()->commandProgression = PROGRESSION_END_DEVICE_CONTAINER;
// Unblock when setpoint of both wavelength and filter position is reached
if ((owner()->wavelength() == owner()->wavelength.setpoint())
&& (owner()->filterPosition() == owner()->filterPosition.setpoint())) {
owner()->commandProgression = PROGRESSION_END_DEVICE_CONTAINER;
}
}
}
......
......@@ -39,6 +39,9 @@ public:
private:
libusb_context *ctx ;
libusb_device_handle *dev_handle;
libusb_device **devs;
bool isInit;
std::string readCommand();
void writeCommand(std::string message);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment