Skip to content
Snippets Groups Projects
Commit 8ca63bd9 authored by Alain BUTEAU's avatar Alain BUTEAU
Browse files

TANGODEVIC-1149

parent dd29ae7f
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,10 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>fr.soleil.lib</groupId>
<artifactId>YAT4Tango-${aol}-${library}-${mode}</artifactId>
</dependency>
</dependencies>
<scm>
<connection>${scm.connection.svn.tango-ds}/DeviceClasses/MeasureInstruments/Femto/trunk/</connection>
......
// ============================================================================
//
// = CONTEXT
// TANGO Project - Magnet Manager Class
//
// = FILENAME
// AutoSearchGainTask.cpp
//
// = AUTHOR
// X. Elattaoui
// ============================================================================
#include <cmath>
#include "AutoSearchGainTask.h"
// ----------------------------------------------------------------------------
const size_t kPERIODIC_TMOUT_MS = 30000; //- task period = in ms
//---------------------------------------------------------
//- the YAT user messages : switch magnet sequence
//const size_t SWICTH_MAGNET_SEQ = yat::FIRST_USER_MSG + 1;
//---------------------------------------------------------
//- the YAT user messages
const size_t AUTO_SEARCH_GAIN = yat::FIRST_USER_MSG + 1;
namespace FemtoCurrentAmplifier_ns
{
// ============================================================================
// AutoSearchGainTask::AutoSearchGainTask
// ============================================================================
AutoSearchGainTask::AutoSearchGainTask (Tango::DeviceImpl* host_device)
: yat4tango::DeviceTask(host_device)
{
_host_dev= (FemtoCurrentAmplifier* )host_device;
this->enable_timeout_msg(false);
// no need to get periodic activity
this->enable_periodic_msg(false);
this->set_periodic_msg_period(kPERIODIC_TMOUT_MS);
}
// ============================================================================
// AutoSearchGainTask::~AutoSearchGainTask
// ============================================================================
AutoSearchGainTask::~AutoSearchGainTask (void)
{
DEBUG_STREAM << "DTOR : AutoSearchGainTask::~AutoSearchGainTask " << std::endl;
}
//-----------------------------------------------
//- the user core of the Task -------------------
void AutoSearchGainTask::process_message (yat::Message& _msg)
throw (Tango::DevFailed)
{
//- The DeviceTask's lock_ -------------
DEBUG_STREAM << "AutoSearchGainTask::process_message::receiving msg " << _msg.to_string() << std::endl;
//- handle msg
switch (_msg.type())
{
//- THREAD_INIT =======================
case yat::TASK_INIT:
{
DEBUG_STREAM << "AutoSearchGainTask::handle_message::THREAD_INIT::thread is starting up" << std::endl;
}
break;
//- TASK_EXIT =======================
case yat::TASK_EXIT:
{
//- "release" code goes here
DEBUG_STREAM << "AutoSearchGainTask::handle_message handling TASK_EXIT thread is quitting ..." << std::endl;
}
break;
//- TASK_PERIODIC ===================
case yat::TASK_PERIODIC:
{
DEBUG_STREAM << "AutoSearchGainTask::handle_message handling TASK_PERIODIC msg ..." << std::endl;
}
break;
//- WRITE_SET_POINT =======================
case AUTO_SEARCH_GAIN:
{
this->autosearch_gain();
}
break;
} //- switch (_msg.type())
} //- AutoSearchGainTask::process_message
//-----------------------------------------------
//- Internal Method to search the gain
//-----------------------------------------------
void AutoSearchGainTask::autosearch_gain (void)
{
//- Start the Loop on Gains
for(int i = _host_dev->get_max_gain_index();i > -1; i--)
{
//- set the gain
_host_dev->write_gain((short)i);
omni_thread::sleep(100);
//- read Overload
_host_dev-> always_executed_hook();
//- Check if overload happen: 1->overload , 0->not overload
if(_host_dev->get_overload() == 0)
{
INFO_STREAM << "Gain Found: gain value= " << i << endl;
_host_dev->internal_set_state(Tango::ON);
_host_dev->internal_set_status("Femto is in NORMAL operation");
}
else
INFO_STREAM << "Gain not Found, try with next one" << endl;
}
WARN_STREAM << "Gain Not Found: Maybe signal is too High" << endl;
_host_dev->internal_set_state(Tango::ON);
_host_dev->internal_set_status("Femto is in NORMAL operation");
}
//-----------------------------------------------
//- external Method to search the gain
//-----------------------------------------------
void AutoSearchGainTask::search_gain (void)
{
//- create and post msg
yat::Message* msg = new yat::Message(AUTO_SEARCH_GAIN, MAX_USER_PRIORITY,true);
if ( !msg )
{
ERROR_STREAM << "ABC1260Task::get_battery_charge-> yat::Message allocation failed." << std::endl;
Tango::Except::throw_exception ("OUT_OF_MEMORY",
"yat::Message allocation failed",
"ABC1260Task::get_battery_charge");
}
//- wait till the message is processed !!
this->wait_msg_handled(msg);
}
} //- end namespace
// ============================================================================
//
// = CONTEXT
// TANGO Project - Magnet Manager Class
//
// = FILENAME
// AutoSearchGainTask.hpp
//
// = AUTHOR
// A. Buteau
// ============================================================================
#ifndef _AUTOSEARCH_GAIN_TASK_H_
#define _AUTOSEARCH_GAIN_TASK_H_
#include <tango.h>
#include <string>
#include <yat4tango/DeviceTask.h>
#include <FemtoCurrentAmplifier.h>
/**
* \brief Visible class from the DServer
*
* \author Xavier Elattaoui
* \date 08-2010
*/
namespace FemtoCurrentAmplifier_ns
{
class AutoSearchGainTask : public yat4tango::DeviceTask
{
public:
/**
* \brief Initialization.
*/
AutoSearchGainTask ( Tango::DeviceImpl * host_device);
/**
* \brief Release resources.
*/
virtual ~AutoSearchGainTask ();
void search_gain() ;
protected:
//- process_message (implements yat4tango::DeviceTask pure virtual method)
virtual void process_message (yat::Message& msg)
throw (Tango::DevFailed);
private:
//- the host device
//- the host device
class FemtoCurrentAmplifier* _host_dev;
void autosearch_gain();
};
} //- end namespace
#endif //_AUTOSEARCH_GAIN_TASK_H_
......@@ -51,6 +51,7 @@ static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/Instrumentatio
#include <FemtoCurrentAmplifier.h>
#include <FemtoCurrentAmplifierClass.h>
#include <PogoHelper.h>
#include "AutoSearchGainTask.h"
namespace FemtoCurrentAmplifier_ns
{
......@@ -148,20 +149,16 @@ namespace FemtoCurrentAmplifier_ns
delete dio_board_proxy;
dio_board_proxy = 0;
}
if (auto_search_gain_thread !=0)
// Delete device allocated objects
if ( this->_auto_search_task )
{
delete auto_search_gain_thread;
auto_search_gain_thread= 0;
//- ask the task to quit
this->_auto_search_task->exit();
//- !!!!! NEVER TRY TO <delete> a yat4tango::DeviceTask, it commits suicide
//- upon return of its main function (i.e. entry point)!!!!!!
this->_auto_search_task = 0;
}
if(mutex)
{
delete mutex;
mutex = 0;
}
}
//+----------------------------------------------------------------------------
//
// method : FemtoCurrentAmplifier::init_device()
......@@ -188,7 +185,10 @@ namespace FemtoCurrentAmplifier_ns
CREATE_DEVSTRING_ATTRIBUTE(attr_gainSelected_read,MAX_STRING_LENGTH,"Not initialised");
attr_gain_write = 0;
auto_search_gain_thread=0;
//- instanciate and start thread
_auto_search_task = new AutoSearchGainTask(this);
_auto_search_task->go(3000);
is_dio_proxy_created = false;
creates_dio_proxy();
......@@ -207,11 +207,10 @@ namespace FemtoCurrentAmplifier_ns
}
else
{
set_state(Tango::FAULT);
set_status("Error during init device, possible values for FemtoType property are: 1,2 or 3");
internal_set_state(Tango::FAULT);
internal_set_status("Error during init device, possible values for FemtoType property are: 1,2 or 3");
}
mutex = new omni_mutex();
//- Force gain mode to LN
if(femtoType != 3 )
......@@ -229,8 +228,8 @@ namespace FemtoCurrentAmplifier_ns
catch(Tango::DevFailed& e)
{
ERROR_STREAM << "Error during init device: " << string(e.errors[0].desc) << "; try to correct and retry init" << ENDLOG;
set_state(Tango::FAULT);
set_status("Error during init device, check DIO device and retry to Init");
internal_set_state(Tango::FAULT);
internal_set_status("Error during init device, check DIO device and retry to Init");
}
}
......@@ -419,12 +418,15 @@ namespace FemtoCurrentAmplifier_ns
//-----------------------------------------------------------------------------
void FemtoCurrentAmplifier::always_executed_hook()
{
yat::AutoMutex<> guard(this->_always_executed_hook_mutex);
if(!is_dio_proxy_created)
{
creates_dio_proxy();
}
//- force a read to overload to refresh the state
//- force a read to overload to refresh the value
Tango::Attribute &overload = dev_attr->get_attr_by_name("overload");
read_overload(overload);
......@@ -569,17 +571,8 @@ void FemtoCurrentAmplifier::read_gainSelectedNum(Tango::Attribute &attr)
attr.get_write_value(attr_gain_write);
if (attr_gain_write > MAX_GAIN_INDEX)
attr_gain_write = MAX_GAIN_INDEX;
this->write_gain(attr_gain_write);
if (attr_gain_write <0)
attr_gain_write = 0;
//- Avoid the wheelswith to go above or under the max-min
Tango::WAttribute &gain = dev_attr->get_w_attr_by_name("gain");
gain.set_write_value(attr_gain_write);
this->set_gain_on_dio_board(attr_gain_write);
}
//+----------------------------------------------------------------------------
......@@ -708,13 +701,13 @@ void FemtoCurrentAmplifier::read_gainSelectedNum(Tango::Attribute &attr)
if(*attr_overload_read == 1)
{
attr.set_quality(Tango::ATTR_ALARM);
set_state(Tango::ALARM);
set_status("Femto is in OVERLOAD, please adjust gain");
internal_set_state(Tango::ALARM);
internal_set_status("Femto is in OVERLOAD, please adjust gain");
}
else
{
set_state(Tango::ON);
set_status("Femto is in NORMAL operation");
internal_set_state(Tango::ON);
internal_set_status("Femto is in NORMAL operation");
}
}
......@@ -723,8 +716,8 @@ void FemtoCurrentAmplifier::read_gainSelectedNum(Tango::Attribute &attr)
string msg = "Error in read_overload. Reason: " + string(e.errors[0].desc);
ERROR_STREAM << "FemtoCurrentAmplifier::read_overload: " << msg << ENDLOG;
set_state(Tango::ALARM);
set_status("OVERLOAD is not responding: error = " + string(e.errors[0].desc));
internal_set_state(Tango::ALARM);
internal_set_status("OVERLOAD is not responding: error = " + string(e.errors[0].desc));
}
}
......@@ -982,26 +975,43 @@ void FemtoCurrentAmplifier::read_gainSelectedNum(Tango::Attribute &attr)
*/
//+------------------------------------------------------------------
void FemtoCurrentAmplifier::auto_search_gain()
DEBUG_STREAM << "FemtoCurrentAmplifier::auto_search_gain(): entering... !" << endl;
{
DEBUG_STREAM << "FemtoCurrentAmplifier::auto_search_gain(): entering... !" << endl;
// Add your own code to control device here
set_state(Tango::RUNNING);
set_status("Femto Device is automatically searching the Gain");
_auto_search_task->search_gain();
//- Delete existing thread
if (auto_search_gain_thread !=0)
}
//+------------------------------------------------------------------
/**
* method: FemtoCurrentAmplifier::write_gain
*
*
*/
//+------------------------------------------------------------------
void FemtoCurrentAmplifier::write_gain(short new_gain )
{
delete auto_search_gain_thread;
auto_search_gain_thread= 0;
DEBUG_STREAM << "FemtoCurrentAmplifier::write_gain(): entering... !" << endl;
if (new_gain > MAX_GAIN_INDEX)
{
attr_gain_write= MAX_GAIN_INDEX;
new_gain = MAX_GAIN_INDEX;
}
// start a new thread
auto_search_gain_thread = new AutoSearchGainThread(this, mutex);
else
if (new_gain <0)
{
attr_gain_write = 0;
new_gain=0;
}
//- Avoid the wheelswith to go above or under the max-min
Tango::WAttribute &gain_w = dev_attr->get_w_attr_by_name("gain");
gain_w.set_write_value(attr_gain_write);
this->set_gain_on_dio_board(attr_gain_write);
}
// **************************************************************************
......
......@@ -30,6 +30,7 @@
#include <TangoExceptionsHelper.h>
#include <DeviceProxyHelper.h>
#include <Xstring.h>
#include <yat4tango/DeviceTask.h>
//using namespace Tango;
......@@ -72,6 +73,9 @@ public :
// Add your own data members here
//-----------------------------------------
short get_max_gain_index() {return MAX_GAIN_INDEX;};
void write_gain(short gain);
bool get_overload() {return *attr_overload_read;};
// Here is the Start of the automatic code generation part
//-------------------------------------------------------------
......@@ -316,7 +320,6 @@ public :
* @exception DevFailed
*/
void auto_search_gain();
/**
* Read the device properties from database
*/
......@@ -331,7 +334,7 @@ public :
*/
void creates_dio_proxy();
protected :
private :
// Add your own data members here
//-----------------------------------------
void set_gain_on_dio_board(short gain_value);
......@@ -341,9 +344,6 @@ protected :
double gain_value_of_the_proxy;
Tango::DeviceProxyHelper *dio_board_proxy;
//Tango::Attr* UBWLattribute;
static string femto_100_LN_gains[MAX_GAIN_INDEX_FEMTO_100];
static double femto_100_LN_gains_num[MAX_GAIN_INDEX_FEMTO_100];
......@@ -363,36 +363,28 @@ protected :
short MAX_GAIN_INDEX;
bool is_dio_proxy_created;
class AutoSearchGainTask* _auto_search_task;
yat::Mutex _state_mutex;
yat::Mutex _status_mutex;
yat::Mutex _always_executed_hook_mutex;
//- thread
omni_mutex* mutex;
omni_thread* auto_search_gain_thread;
friend class AutoSearchGainThread;
};
/**
* Class Description:
* Thread which search automatically the gain to not overload
* It uses the SingleShotDIO generic Device to communicate with the Femto.
*/
class AutoSearchGainThread : public omni_thread, public Tango::LogAdapter
{
public :
AutoSearchGainThread (FemtoCurrentAmplifier* femto_device, omni_mutex* m);
private :
FemtoCurrentAmplifier* femto_device_;
omni_mutex* mutex;
Tango::DeviceProxy* SSDIO_device;
void internal_set_state(Tango::DevState new_state)
{ yat::AutoMutex<> guard(this->_state_mutex);
// call Tango method
set_state(new_state);
};
void internal_set_status(std::string new_status)
{ yat::AutoMutex<> guard(this->_status_mutex);
// call Tango method
set_status(new_status);
};
void *run_undetached(void *arg);
};
} // namespace
#endif // _FEMTOCURRENTAMPLIFIER_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment