Writing Vector object into a .txt file
-
Basically two functions one is for updating graphics and other for haptics rendering. Haptic rendering sampling rate is quite large compared to graphics. In the library I'm using there is a thread class basically gives priority to haptic and graphics rendering. But I'm not able to see any section in the code where they access system timing and setting sampling rate etc. I'm thinking about assigning some condition inside data will be written into the file at a rate of 100 samples per second. There is an inbuilt precision clock with the library, so if I've I can get cpu time with precision clock , how can formulate a condition to write data at a rate 100Hz??
As far as I understood you have a haptic device which is some kind of hardware which generates data at a frequency defined by the hardware. If the library does not provide functions to set the sample rate, it is probably fixed. The thread is then activated when new data are available. But again, without knowing the hardware and the library (which acesses the hardware specific driver), it is rather impossible to answer. Do you want to write the data to a file with 100 Hz. This is probably too fast (especially when the file must be opened for each write). It again depends on your requirements (who is reading the file). If the data should be read by another process, you may use some kind of IPC (Inter Process Communication). Regarding timers: Windows has no high resolution timers. While it is possible to measure times with high resolution (QueryPerformanceCounter), the system timers have a resolution of about 10 ms by default and can be tweaked down to 1 ms.
-
As far as I understood you have a haptic device which is some kind of hardware which generates data at a frequency defined by the hardware. If the library does not provide functions to set the sample rate, it is probably fixed. The thread is then activated when new data are available. But again, without knowing the hardware and the library (which acesses the hardware specific driver), it is rather impossible to answer. Do you want to write the data to a file with 100 Hz. This is probably too fast (especially when the file must be opened for each write). It again depends on your requirements (who is reading the file). If the data should be read by another process, you may use some kind of IPC (Inter Process Communication). Regarding timers: Windows has no high resolution timers. While it is possible to measure times with high resolution (QueryPerformanceCounter), the system timers have a resolution of about 10 ms by default and can be tweaked down to 1 ms.
Jochen Arndt wrote:
the system timers have a resolution of about 10 ms by default and can be tweaked down to 1 ms.
What about the waitable timers? SetWaitableTimer[^]
Quote:
pDueTime [in] The time after which the state of the timer is to be set to signaled, in 100 nanosecond intervals.
The difficult we do right away... ...the impossible takes slightly longer.
-
Jochen Arndt wrote:
the system timers have a resolution of about 10 ms by default and can be tweaked down to 1 ms.
What about the waitable timers? SetWaitableTimer[^]
Quote:
pDueTime [in] The time after which the state of the timer is to be set to signaled, in 100 nanosecond intervals.
The difficult we do right away... ...the impossible takes slightly longer.
Windows uses internally 100 ns based times (
FILETIME
structure). But this does not mean that the internal timer tick is running with a resolution of 100 ns. There are undocumented functions to query the range of resolution and set the resolution (NtQueryTimerResolution
,NtSetTimerResolution
). On a typical system, the range is 0.5 to 15.6 ms. -
Windows uses internally 100 ns based times (
FILETIME
structure). But this does not mean that the internal timer tick is running with a resolution of 100 ns. There are undocumented functions to query the range of resolution and set the resolution (NtQueryTimerResolution
,NtSetTimerResolution
). On a typical system, the range is 0.5 to 15.6 ms.How can I configure a periodic thread of particular frequency?
-
How can I configure a periodic thread of particular frequency?
Just call
Sleep()
with a time out value of 1000 / frequency[Hz]. A better implememntation would useWaitForSingleObject()
with the same time out value and a handle to a terminate thread event so that you can terminate the thread:UINT worker_thread_func(LPVOID pParam)
{
// pParam is usually a pointer to a C++ class to which this thread belongs
// that is passed when creating the thread.
// Cast the pointer to get access to member vars.
MyClass *pThis = (MyClass *)pParam;
HANDLE hKillEvent = pThis->m_hKillEvent;
while (1)
{
if (WAIT_OBJECT_0 == WaitForSingleObject(hKillEvent, TIME_OUT_VAL))
break;
// Perform periodic task here
}
return 0;
} -
Just call
Sleep()
with a time out value of 1000 / frequency[Hz]. A better implememntation would useWaitForSingleObject()
with the same time out value and a handle to a terminate thread event so that you can terminate the thread:UINT worker_thread_func(LPVOID pParam)
{
// pParam is usually a pointer to a C++ class to which this thread belongs
// that is passed when creating the thread.
// Cast the pointer to get access to member vars.
MyClass *pThis = (MyClass *)pParam;
HANDLE hKillEvent = pThis->m_hKillEvent;
while (1)
{
if (WAIT_OBJECT_0 == WaitForSingleObject(hKillEvent, TIME_OUT_VAL))
break;
// Perform periodic task here
}
return 0;
}Hi Jochen, I still didn't get idea of using WaitForSingleObject() as a timer. Basically WaitForSingleObject is a predefined function under windows.h? right?. Can you bit explain how exactly this can be embedded with a project?
-
Hi Jochen, I still didn't get idea of using WaitForSingleObject() as a timer. Basically WaitForSingleObject is a predefined function under windows.h? right?. Can you bit explain how exactly this can be embedded with a project?
It is a Windows API function that is used with threads. It will suspend the thread until the event specified by the passed handle occurs or the time out time has elapsed. Using threads is an advanced topic. So you should read about it first. A starting point may be Using Worker Threads[^]. Your requirement was to execute specific tasks in fixed intervals. This can be done by Windows timers from within your main (GUI) thread. But this will block the main thread for the task execution time. With short intervals, this will lead to delayed user input (mouse and keyboard actions are not performed immediately). To avoid this, a worker thread can be used.
-
It is a Windows API function that is used with threads. It will suspend the thread until the event specified by the passed handle occurs or the time out time has elapsed. Using threads is an advanced topic. So you should read about it first. A starting point may be Using Worker Threads[^]. Your requirement was to execute specific tasks in fixed intervals. This can be done by Windows timers from within your main (GUI) thread. But this will block the main thread for the task execution time. With short intervals, this will lead to delayed user input (mouse and keyboard actions are not performed immediately). To avoid this, a worker thread can be used.
There is class precision clock included in the library. So I can use this for getting system time. But my doubt is if I've the 'time', with a conditional statement I should be able down sample data writing to a sampling rate of 100hZ??!. If I've time stamp at rate of 8KHz then how to formulate a conditional statement to write data at 100Hz??
-
There is class precision clock included in the library. So I can use this for getting system time. But my doubt is if I've the 'time', with a conditional statement I should be able down sample data writing to a sampling rate of 100hZ??!. If I've time stamp at rate of 8KHz then how to formulate a conditional statement to write data at 100Hz??
I can't answer this without knowing the library. That depends on the library, the implementation of that 'clock' and if it can generate some kind of signal.
-
I can't answer this without knowing the library. That depends on the library, the implementation of that 'clock' and if it can generate some kind of signal.
Just got some information on data logging for haptic thread. It's basically a data structure for the buffering of data on a haptic thread but I didn't understood completely how to use this for my application. It's basically a header. I'm attaching it here:
/******
*
* Written by Dan Morris
* dmorris@cs.stanford.edu
* http://cs.stanford.edu/~dmorris
*
* You can do anything you want with this file as long as this header
* stays on it and I am credited when it's appropriate.
*
******/#ifndef _BLOCK_LINKED_LIST_H_
#define _BLOCK_LINKED_LIST_H_// This is a custom data structure that I used for buffering data on
// its way out to disk. It is not a random access data structure;
// it just buffers up objects of type T and flushes them out to disk.
//
// If you're using flusher and writer threads, the general protocol
// is to only allow the writer to call the push_back function, and
// let the reader use the safe_flush function to do its flushing.
// Then at some point when the writer is done forever, the reader can
// call flush() to flush the remaining blocks that were currently in use
// by the writer.
//
// This is a lock-free solution to the producer-consumer problem with the
// constraint that immediate consumption is not important.
//
// This addresses a common access pattern for applications that have a time-
// critical thread (e.g. a haptics thread) that generates data that
// needs to be logged to disk. This approach avoids any disk access or
// mutex access on the high-priority thread. I've used this data
// structure for haptics (where a high-priority thread generates forces)
// and neurophysiology/psychophysics (where a high-priority thread
// controls an experiment).
//
// Note that in this mode, the "flusher" thread should _not_ also put
// data into the array; i.e. the writer thread "owns" the push_back
// function.// The node type for the linked list
template class block_linked_list_node {
public:// An array of T's
T* data;
block_linked_list_node* next;
block_linked_list_node() {
data = new T[chunk_size];
next = 0;
}
~block_linked_list_node() {
delete [] data;
}
};// The data structure itself
template class block_linked_list {
public:// The head of the list
block_linked_list_node* head;// The number of T's in the list
size_t total_count;// A pointer to the curren