source: trunk/MagicSoft/AMC/activemirrorcontrol/activemirrorcontrol/motorthread.cpp@ 3401

Last change on this file since 3401 was 3401, checked in by merck, 21 years ago
nitial checkin of AMC project
File size: 2.9 KB
Line 
1// MotorThread.cpp: Implementierung der Klasse MotorThread.
2//
3//////////////////////////////////////////////////////////////////////
4
5#include "motorthread.h"
6#include "motorthreaddialog.h"
7#include "threadevent.h"
8#include "amcmirrorpanel.h"
9#include "amcmotor.h"
10#include "amcserialport.h"
11#include "amcerror.h"
12#include <qmessagebox.h>
13#include <qevent.h>
14#include <qthread.h>
15#include <qstring.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <math.h>
19
20#define MAX(a,b) ((a>b) ? a : b )
21
22extern AMCSerialPort* g_pPort[];
23
24//////////////////////////////////////////////////////////////////////
25// Konstruktion/Destruktion
26//////////////////////////////////////////////////////////////////////
27
28MotorThread::MotorThread( QObject* p_pReceiver )
29 : m_pPanel( NULL ), m_pReceiver( p_pReceiver )
30{
31}
32
33MotorThread::~MotorThread()
34{
35}
36
37void MotorThread::run()
38{
39 QString qsMsg;
40 qsMsg.sprintf( "Panel (%d,%d) Port: %d, Box:%d, Driver:%d",
41 m_pPanel->i(), m_pPanel->j(), m_pPanel->port(), m_pPanel->box(), m_pPanel->driver() );
42 QThread::postEvent( m_pReceiver, new ThreadEvent( qsMsg ) );
43
44 QTime t;
45 t.start(); // start clock
46
47
48 // We use only one motor instance and reuse it for each panel
49 AMCSerialPort* pPort = g_pPort[ m_pPanel->port()-1 ];
50 AMCMotor* pMotor = new AMCMotor( pPort );
51
52 bool zLaser = false;
53 for( int iRetry = 0; iRetry < 2; iRetry++ )
54 {
55 try
56 {
57 pMotor->unselectBox();
58 pMotor->selectBox( m_pPanel->box() );
59 pMotor->selectDriver( m_pPanel->driver() );
60 pMotor->switchLaser( true ); // only for show
61 zLaser = true;
62 pMotor->getFrequency();
63
64 pMotor->centerMotors();
65 pMotor->waitForMotors( pMotor->calcTimeout( 17000 )+2 );
66 int x = m_pPanel->getRefX();
67 int y = m_pPanel->getRefY();
68 int steps = MAX( abs(x), abs(y) );
69 if( ! ((x == 0) && (y == 0)) ) // only move is necesary
70 {
71 pMotor->moveMotors(x,y);
72 pMotor->waitForMotors( pMotor->calcTimeout(steps) );
73 m_pPanel->setX( x );
74 m_pPanel->setY( y );
75 }
76 pMotor->switchLaser( false ); // only for show
77 }
78 catch( AMCError& e )
79 {
80 if( iRetry >= 1 )
81 {
82 qsMsg.sprintf( "AMCError: Panel (%d,%d) Port: %d, Box:%d, Driver:%d -- ",
83 m_pPanel->i(), m_pPanel->j(), m_pPanel->port(), m_pPanel->box(), m_pPanel->driver() );
84 qsMsg.append( e.getErrorText() );
85 QThread::postEvent( m_pReceiver, new ThreadErrorEvent( qsMsg, e ) );
86 }
87
88 // try to switch of laser if we switched it on.
89 if( zLaser )
90 {
91 try
92 {
93 pMotor->switchLaser( false ); // only for show
94 }
95 catch( AMCError& e )
96 {
97 }
98 }
99 continue;
100 }
101 break;
102
103 }
104 delete pMotor;
105
106 qsMsg.sprintf( "Time: %dms", t.elapsed() );
107 QThread::postEvent( m_pReceiver, new ThreadEvent( qsMsg ) );
108 QThread::postEvent( m_pReceiver, new QCustomEvent( PROGRESS_EVENT ) );
109
110}
111
112void MotorThread::setPanel(AMCMirrorPanel* p_pPanel)
113{
114 m_pPanel = p_pPanel;
115}
Note: See TracBrowser for help on using the repository browser.