⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mavisctl.cpp

📁 机器人程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	}
	catch(...) {
		MessageBox(NULL, TEXT("Can't initialize Mavis - unknown error"), gszTitle, 0);
		return FALSE;
	}

	// set image size
	bih.biWidth  = pMv->getImgWidth();
	bih.biHeight = pMv->getImgHeight();

	// run Mavis in its own thread
	hMavisThread = CreateThread(
		NULL,          //default security attributes
		0,             //default stack size
		RunMavis,      //thread function
		NULL,          //the thread data
		0,             //default creation flags
		NULL           //thread id variable
	);

	//after Mavis is setup, load her communications dll to make sure
	//everything is ready to go, but then unload the dll so it can be
	//used by some other application. The program starts with the dll
	//unloaded, but the user can load it manually from the file menu.
	if( !LoadMavisDll() ) return FALSE; //failed to load
	UnloadMavisDll();

	SetAppState(REMOTE_MODE);


	// display window
	ShowWindow(ghWnd, nCmdShow);
	UpdateWindow(ghWnd);

	return TRUE;
}


//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC    hdc;
	HBRUSH hbrBlack;
	static int nHLineFrames = 1;

	switch (message)
	{
		case WM_COMMAND:
			// possible future commands:
			//    - START_MAVIS: ResumeThread(hMavisThread);
			//    - STOP_MAVIS:  SuspendThread(hMavisThread);
			//    - toggle the debug view on and off

			// Parse the GUI action:
			wmId    = LOWORD(wParam);
			wmEvent = HIWORD(wParam);

			switch (wmId)
			{
				case BTN_LOOK_ONCE:

					// obviously this paradigm is limited....
					if( !strcmp(gObjNameBuf, HOMEBASE_OBJ_NAME) )
						ctlData.objId = HOMEBASE_OBJ_ID;
					else if( !strcmp(gObjNameBuf, MPI_FACE_OBJ_NAME) )
						ctlData.objId = MPI_FACE_OBJ_ID;
					else if( !strcmp(gObjNameBuf, MOTION_DETECTOR_NAME) )
						ctlData.objId = MOTION_DETECTOR_ID;
					else if( !strcmp(gObjNameBuf, "Bruce") )
						ctlData.objId = 100;
					else if(gObjNameBuf, HAAR_FACE_DETECTOR_NAME)
						ctlData.objId = HAAR_FACE_DETECTOR_ID;
					else
						ctlData.objId = INVALID_OBJ_ID;

					// disable button
					EnableWindow(ghwndLookButton, FALSE);
					ctlData.ctlCode = LOOK_ONCE;
					procLookOnce(ctlData.objId, &objLoc);
					// enable button
					EnableWindow(ghwndLookButton, TRUE);

					sprintf(
						gStatusMsgBuf,
						"%s: angle = %.1f, dist = %d, prob = %d, flag = %d",
						gObjNameBuf, objLoc.angle, (int)objLoc.dist, objLoc.prob, objLoc.movFlag
					);
					pStatus->update( gStatusMsgBuf );
					break;


				case BTN_LOOK_CONT:
					if( LOOK_MODE == gAppState )
					{
						// stop looking
						procStopLooking();
					}
					else
					{
						// Prepare to start looking
						if( !strcmp(gObjNameBuf, HOMEBASE_OBJ_NAME) )
							ctlData.objId = HOMEBASE_OBJ_ID;
						//else if( !strcmp(gObjNameBuf, MPI_FACE_OBJ_NAME) )
						//	ctlData.objId = MPI_FACE_OBJ_ID;
						else if( !strcmp(gObjNameBuf, MOTION_DETECTOR_NAME) )
							ctlData.objId = MOTION_DETECTOR_ID;
						else if( !strcmp(gObjNameBuf, "Bruce") )
							ctlData.objId = 100;
						else
							ctlData.objId = INVALID_OBJ_ID;


						if( INVALID_OBJ_ID != ctlData.objId )
						{
							// launch a separate thread to wait for a sighting
							hLookContThread = CreateThread(
								NULL,              //default security attributes
								0,                 //default stack size
								LookUntilSighted,  //thread function
								NULL,              //the thread data
								0,                 //default creation flags
								NULL               //thread id variable
							);
						}
					}

					break;


				case MENU_LOAD_DLL:
					if( !LoadMavisDll() ) SetAppState(REMOTE_MODE); //failed to load
					else SetAppState(WAIT_MODE);
					break;


				case MENU_UNLOAD_DLL:
					UnloadMavisDll();
					SetAppState(REMOTE_MODE);
					break;


				case BTN_CALIBRATE:
					pCH = pMv->getCalibrationHelper();
					SetAppState(CALIBRATE_MODE);
					EnableWindow( ghwndNextButton, TRUE );
					SetWindowText( ghwndInstrText, pCH->getMsg() );

					if( pCH->hasNextStep() )
						EnableWindow( ghwndNextButton, TRUE );
					else
						EnableWindow( ghwndNextButton, FALSE );

						// show the calibration parameters
						sprintf(
							gStatusMsgBuf,
							"Camera Height = %.2f, fwdPosition = %.2f, whiteDiam = %.2f",
							pCH->getCameraZ(), pCH->getCameraY(), pCH->getWhiteDiam()
						);
						pStatus->update( gStatusMsgBuf );

					break;


				case BTN_NEXT:
					if(CALIBRATE_MODE == gAppState)
					{
						if( pCH->nextStep() )
						{
							if( pCH->hasNextStep() )
							{
								SetWindowText( ghwndInstrText, pCH->getMsg() );
								EnableWindow( ghwndNextButton, TRUE );
							}
							else
							{
								SetWindowText( ghwndInstrText, "Calibration finished." );
								EnableWindow( ghwndNextButton, FALSE );
							}
						}
						else
						{
							SetWindowText( ghwndInstrText, pCH->getMsg() );
							EnableWindow( ghwndNextButton, FALSE );
						}
					}
					else if(HLINES_MODE == gAppState)
					{
						// read an integer value from the edit control
						BOOL lpTranslated;
						UINT d = GetDlgItemInt(
							ghWnd,
							EDIT_HLINES,
							&lpTranslated,
							FALSE
						);

						if(lpTranslated)
						{
							// do hlines call
							procLocateHLines((int)d, HL_UNORDERED, &HLinesMetadata);

							// update status bar

							// update static and edit controls
							char msg[250];
							sprintf(
								msg,
								"Frame %d:\n   d = %d mm,\n   %d HLines found.\n\n"
								"To analyze another frame, repeat steps.",
								++nHLineFrames, d, HLinesMetadata.nLines
							);
							SetWindowText(ghwndTravelDist,"");
							SetWindowText(ghwndHLinesText, msg);


							// temporary test code
							/*
							FILE * pFile;
							pFile = fopen("mytestfile.txt", "a");

							if(pFile)
							{
								HLine_t hLine;
								int nLines = 0;
								fprintf(pFile, "\n\n%d HLines\n", HLinesMetadata.nLines);
								do {
									nLines = procNextLine(&hLine);
									fprintf(
										pFile,
										"\nHLine:\n"
										"\tdistance.lo = %.4g\n"
										"\tdistance.hi = %.4g\n"
										"\theight.lo   = %.4g\n"
										"\theight.hi   = %.4g\n"
										"\tangle.lo    = %.4g\n"
										"\tangle.hi    = %.4g\n",
										hLine.distance.lo,
										hLine.distance.hi,
										hLine.height.lo,
										hLine.height.hi,
										hLine.angle.lo,
										hLine.angle.hi
									);
									fprintf(
										pFile,
										"\tpctError     = %.2f\n"
										"\tlineType     = %d\n"
										"\tinPathStatus = %d\n"
										"\tinPathProb   = %d\n",
										hLine.pctError,
										hLine.lineType,
										hLine.inPathStatus,
										hLine.inPathProb
									);
								} while(nLines>0);
								fclose(pFile);
							}
							*/
						}
						else
						{
							SendMessage(ghwndTravelDist, EM_SETSEL, 0, (LPARAM)-1);
							SetWindowText(
								ghwndHLinesText,
								"Invalid travel distance. Enter an unsigned integer value."
							);
						}

						// move forcus to the edit control
						SetFocus(ghwndTravelDist);
					}


					break;


				case BTN_STOP:
					//procStopLooking();      // send Mavis a Pause cmd
					procNextFrame();        // refresh the view area
					SetAppState(WAIT_MODE);
					break;


				case BTN_HLINES:
					procLocateHLines(0, HL_UNORDERED, &HLinesMetadata);
					nHLineFrames = 1;
					SetWindowText(ghwndTravelDist,"");
					SetWindowText(
						ghwndHLinesText,
						"1. Move the robot or camera.\n\n"
						"2. Enter travel distance (in mm) in the text box.\n\n"
						"3. Click the \"Next\" button."
					);
					EnableWindow( ghwndNextButton, TRUE );
					SetAppState(HLINES_MODE);
					break;


				case MENU_ABOUT:
					DialogBox(ghInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
					break;


				case MENU_EXIT:
					DestroyWindow(hWnd);
					break;


				case LIST_OBJS:
					if( LBN_SELCHANGE == wmEvent)
					{
						gListInd = SendMessage(ghWndObjList, LB_GETCURSEL, 0, 0);
						SendMessage(ghWndObjList, LB_GETTEXT, (WPARAM)gListInd, (LPARAM)gObjNameBuf);
					}
					break;


				case WM_CTLCOLORSTATIC:
					SetBkColor( (HDC)wParam, GetBkColor(GetDC(ghWnd)) );
					break;


				default:
					return DefWindowProc(hWnd, message, wParam, lParam);
			}

			break;

		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			hbrBlack = (HBRUSH)GetStockObject(BLACK_BRUSH);
			FillRect(hdc, &rcBmp, hbrBlack);
			EndPaint(hWnd, &ps);

			DisplayBitmap();

			break;

		case WM_SIZE:
			// redisplay the status bar
			pStatus->show();
			break;

		case WM_DESTROY:
			if(pMv) pMv->stop();
			ShowWindow(ghWnd, SW_HIDE);
			PostQuitMessage(0);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}


//
//  FUNCTION: LookUntilSighted(LPVOID)
//
//  PURPOSE:  Thread function for "looking until sighted"
//
DWORD WINAPI LookUntilSighted(LPVOID lpThreadData)
{
	// make sure the confidence level is initialized to zero
	objLoc.prob = 0;

	// update gui
	SetWindowText( ghwndLookContButton, "Stop" );
	SetAppState(LOOK_MODE);

	// Start Looking -- enters a wait state
	procStartLooking(ctlData.objId, &objLoc, INFINITE);


	//We've returned from the wait state

	// restore the gui
	SetWindowText( ghwndLookContButton, "Look Cont." );
	SetAppState(WAIT_MODE);

	if( objLoc.prob )
	{
		sprintf(
			gStatusMsgBuf,
			"%s: angle = %.1f, dist = %d, prob = %d, flag = %d",
			gObjNameBuf, objLoc.angle, (int)objLoc.dist, objLoc.prob, objLoc.movFlag
		);
	}
	else
	{
		sprintf(
			gStatusMsgBuf,
			"No %s sighted",
			gObjNameBuf

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -