No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MonitorUI.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using System;
  2. using Gtk;
  3. using Gdk;
  4. using monitor;
  5. public partial class MainWindow : Gtk.Window
  6. {
  7. private DestijlCommandManager cmdManager;
  8. private Pixbuf drawingareaCameraPixbuf;
  9. enum SystemState
  10. {
  11. NotConnected,
  12. ServerConnected,
  13. RobotConnected
  14. };
  15. private SystemState systemState = SystemState.NotConnected;
  16. private System.Timers.Timer batteryTimer;
  17. public MainWindow() : base(Gtk.WindowType.Toplevel)
  18. {
  19. Build();
  20. cmdManager = new DestijlCommandManager(OnCommandReceivedEvent);
  21. batteryTimer = new System.Timers.Timer(10000.0);
  22. batteryTimer.Elapsed += OnBatteryTimerElapsed;
  23. AdjustControls();
  24. }
  25. public void AdjustControls()
  26. {
  27. ChangeState(SystemState.NotConnected);
  28. drawingareaCameraPixbuf = new Pixbuf((string)null);
  29. drawingareaCameraPixbuf = Pixbuf.LoadFromResource("monitor.ressources.missing_picture.png");
  30. entryServerName.Text = Client.defaultIP;
  31. entryServerPort.Text = Client.defaultPort.ToString();
  32. entryTimeout.Text = "10000";
  33. }
  34. private void ChangeState(SystemState newState)
  35. {
  36. switch (newState)
  37. {
  38. case SystemState.NotConnected:
  39. labelRobot.Sensitive = false;
  40. gtkAlignmentRobot.Sensitive = false;
  41. labelRobotControl.Sensitive = false;
  42. gtkAlignmentRobotControl.Sensitive = false;
  43. boxCamera.Sensitive = false;
  44. buttonServerConnection.Label = "Connect";
  45. buttonRobotActivation.Label = "Activate";
  46. labelBatteryLevel.Text = "Unknown";
  47. checkButtonCameraOn.Active = false;
  48. checkButtonRobotPosition.Active = false;
  49. if (cmdManager != null) cmdManager.Close();
  50. batteryTimer.Stop();
  51. break;
  52. case SystemState.ServerConnected:
  53. buttonServerConnection.Label = "Disconnect";
  54. buttonRobotActivation.Label = "Activate";
  55. labelBatteryLevel.Text = "Unknown";
  56. labelRobot.Sensitive = true;
  57. gtkAlignmentRobot.Sensitive = true;
  58. boxCamera.Sensitive = true;
  59. labelRobotControl.Sensitive = false;
  60. gtkAlignmentRobotControl.Sensitive = false;
  61. batteryTimer.Stop();
  62. break;
  63. case SystemState.RobotConnected:
  64. buttonRobotActivation.Label = "Reset";
  65. labelRobotControl.Sensitive = true;
  66. gtkAlignmentRobotControl.Sensitive = true;
  67. batteryTimer.Start();
  68. break;
  69. default:
  70. labelRobot.Sensitive = false;
  71. gtkAlignmentRobot.Sensitive = false;
  72. labelRobotControl.Sensitive = false;
  73. gtkAlignmentRobotControl.Sensitive = false;
  74. boxCamera.Sensitive = false;
  75. buttonServerConnection.Label = "Connect";
  76. buttonRobotActivation.Label = "Activate";
  77. labelBatteryLevel.Text = "Unknown";
  78. checkButtonCameraOn.Active = false;
  79. checkButtonRobotPosition.Active = false;
  80. systemState = SystemState.NotConnected;
  81. return;
  82. }
  83. systemState = newState;
  84. }
  85. private void MessagePopup(MessageType type, ButtonsType buttons, string title, string message)
  86. {
  87. MessageDialog md = new MessageDialog(this, DialogFlags.DestroyWithParent, type, buttons, message)
  88. {
  89. Title = title
  90. };
  91. md.Run();
  92. md.Destroy();
  93. }
  94. protected void OnDeleteEvent(object sender, DeleteEventArgs a)
  95. {
  96. Console.WriteLine("Bye bye");
  97. if (cmdManager != null) cmdManager.Close();
  98. Application.Quit();
  99. a.RetVal = true;
  100. }
  101. public void OnCommandReceivedEvent(string header, string data)
  102. {
  103. if (header != null) Console.WriteLine("Received header (" + header.Length + "): " + header);
  104. if (data != null) Console.WriteLine("Received data (" + data.Length + "): " + data);
  105. if (header.ToUpper() == DestijlCommandList.HeaderStmBat)
  106. {
  107. switch (data[0])
  108. {
  109. case '2':
  110. labelBatteryLevel.Text = "High";
  111. break;
  112. case '1':
  113. labelBatteryLevel.Text = "Low";
  114. break;
  115. case '0':
  116. labelBatteryLevel.Text = "Empty";
  117. break;
  118. default:
  119. labelBatteryLevel.Text = "Invalid value";
  120. break;
  121. }
  122. }
  123. }
  124. protected void OnQuitActionActivated(object sender, EventArgs e)
  125. {
  126. Console.WriteLine("Bye bye 2");
  127. if (cmdManager != null) cmdManager.Close();
  128. this.Destroy();
  129. Application.Quit();
  130. }
  131. protected void OnShowLogWindowActionActivated(object sender, EventArgs e)
  132. {
  133. MessagePopup(MessageType.Info,
  134. ButtonsType.Ok, "Info",
  135. "Logger not yet implemented");
  136. }
  137. protected void OnButtonServerConnectionClicked(object sender, EventArgs e)
  138. {
  139. DestijlCommandManager.CommandStatus statusCmd;
  140. if (buttonServerConnection.Label == "Disconnect")
  141. {
  142. ChangeState(SystemState.NotConnected);
  143. }
  144. else
  145. {
  146. if ((entryServerName.Text == "") || (entryServerPort.Text == ""))
  147. {
  148. MessagePopup(MessageType.Error,
  149. ButtonsType.Ok, "Error",
  150. "Server name or port is invalid");
  151. }
  152. else
  153. {
  154. Console.WriteLine("Connecting to " + entryServerName.Text + ":" + entryServerPort.Text);
  155. bool status = false;
  156. try
  157. {
  158. cmdManager.timeout = Convert.ToDouble(entryTimeout.Text);
  159. }
  160. catch (Exception)
  161. {
  162. cmdManager.timeout = 100;
  163. entryTimeout.Text = cmdManager.timeout.ToString();
  164. }
  165. try
  166. {
  167. status = cmdManager.Open(entryServerName.Text, Convert.ToInt32(entryServerPort.Text));
  168. }
  169. catch (Exception)
  170. {
  171. Console.WriteLine("Something went wrong during connection");
  172. return;
  173. }
  174. if (status != true)
  175. {
  176. MessagePopup(MessageType.Error,
  177. ButtonsType.Ok, "Error",
  178. "Unable to connect to server " + entryServerName.Text + ":" + Convert.ToInt32(entryServerPort.Text));
  179. }
  180. else
  181. {
  182. Console.Write("Send command RobotOpenCom: ");
  183. statusCmd = cmdManager.RobotOpenCom();
  184. Console.WriteLine(statusCmd.ToString());
  185. if (statusCmd == DestijlCommandManager.CommandStatus.Success)
  186. {
  187. ChangeState(SystemState.ServerConnected);
  188. }
  189. else
  190. {
  191. MessagePopup(MessageType.Error,
  192. ButtonsType.Ok, "Error",
  193. "Unable to open communication with robot.\nCheck that supervisor is accepting OPEN_COM_DMB command");
  194. cmdManager.Close();
  195. }
  196. }
  197. }
  198. }
  199. }
  200. protected void OnButtonRobotActivationClicked(object sender, EventArgs e)
  201. {
  202. DestijlCommandManager.CommandStatus status;
  203. if (buttonRobotActivation.Label == "Activate") // activation du robot
  204. {
  205. if (radioButtonWithWatchdog.Active) // Demarrage avec watchdog
  206. {
  207. status = cmdManager.RobotStartWithWatchdog();
  208. }
  209. else // Demarrage sans watchdog
  210. {
  211. status = cmdManager.RobotStartWithoutWatchdog();
  212. }
  213. if (status == DestijlCommandManager.CommandStatus.Success)
  214. {
  215. ChangeState(SystemState.RobotConnected);
  216. }
  217. else
  218. {
  219. if (status == DestijlCommandManager.CommandStatus.CommunicationLostWithServer)
  220. {
  221. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
  222. ChangeState(SystemState.NotConnected);
  223. }
  224. else
  225. {
  226. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Command rejected\nCheck that supervisor accept \nDMB_START_WITH_WD and/or DMB_START_WITHOUT_WD");
  227. }
  228. }
  229. }
  230. else // Reset du robot
  231. {
  232. status = cmdManager.RobotReset();
  233. if (status == DestijlCommandManager.CommandStatus.Success)
  234. {
  235. ChangeState(SystemState.ServerConnected);
  236. }
  237. else
  238. {
  239. if (status == DestijlCommandManager.CommandStatus.CommunicationLostWithServer)
  240. {
  241. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
  242. ChangeState(SystemState.NotConnected);
  243. }
  244. else
  245. {
  246. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Unknown error");
  247. }
  248. }
  249. }
  250. }
  251. protected void OnButtonMouvClicked(object sender, EventArgs e)
  252. {
  253. if (sender == buttonRight)
  254. {
  255. cmdManager.RobotTurn(90);
  256. }
  257. else if (sender == buttonLeft)
  258. {
  259. cmdManager.RobotTurn(-90);
  260. }
  261. else if (sender == buttonForward)
  262. {
  263. cmdManager.RobotMove(100);
  264. }
  265. else if (sender == buttonDown)
  266. {
  267. cmdManager.RobotMove(-100);
  268. }
  269. else
  270. {
  271. MessagePopup(MessageType.Warning, ButtonsType.Ok, "Abnormal behavior", "Callback OnButtonMouvClicked called by unknown sender");
  272. }
  273. }
  274. void OnBatteryTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
  275. {
  276. DestijlCommandManager.CommandStatus status;
  277. //int batteryLevel;
  278. batteryTimer.Stop();
  279. if (checkButtonGetBattery.Active)
  280. {
  281. //status = cmdManager.RobotGetBattery(out batteryLevel);
  282. status = cmdManager.RobotGetBattery();
  283. switch (status)
  284. {
  285. case DestijlCommandManager.CommandStatus.Success:
  286. /*switch (batteryLevel)
  287. {
  288. case 2:
  289. labelBatteryLevel.Text = "High";
  290. break;
  291. case 1:
  292. labelBatteryLevel.Text = "Low";
  293. break;
  294. case 0:
  295. labelBatteryLevel.Text = "Empty";
  296. break;
  297. default:
  298. labelBatteryLevel.Text = "Unknown";
  299. break;
  300. }*/
  301. batteryTimer.Start();
  302. break;
  303. case DestijlCommandManager.CommandStatus.CommunicationLostWithServer:
  304. //MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
  305. Console.WriteLine("Error: Connection lost with server");
  306. batteryTimer.Stop();
  307. labelBatteryLevel.Text = "Unknown";
  308. ChangeState(SystemState.NotConnected);
  309. break;
  310. case DestijlCommandManager.CommandStatus.CommunicationLostWithRobot:
  311. //MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with robot");
  312. Console.WriteLine("Error: Connection lost with robot");
  313. batteryTimer.Stop();
  314. labelBatteryLevel.Text = "Unknown";
  315. ChangeState(SystemState.ServerConnected);
  316. break;
  317. default:
  318. labelBatteryLevel.Text = "Unknown";
  319. batteryTimer.Start();
  320. break;
  321. }
  322. }
  323. else batteryTimer.Start();
  324. }
  325. protected void OnCheckButtonCameraOnClicked(object sender, EventArgs e)
  326. {
  327. if (!checkButtonCameraOn.Active)
  328. {
  329. if (cmdManager.CameraClose() != DestijlCommandManager.CommandStatus.Success)
  330. {
  331. MessagePopup(MessageType.Error,
  332. ButtonsType.Ok, "Error",
  333. "Error when closing camera: bad answer for supervisor or timeout");
  334. }
  335. }
  336. else
  337. {
  338. if (cmdManager.CameraOpen() != DestijlCommandManager.CommandStatus.Success)
  339. {
  340. MessagePopup(MessageType.Error,
  341. ButtonsType.Ok, "Error",
  342. "Error when opening camera: bad answer for supervisor or timeout");
  343. checkButtonCameraOn.Active = false;
  344. }
  345. }
  346. }
  347. protected void OnCheckButtonRobotPositionClicked(object sender, EventArgs e)
  348. {
  349. if (!checkButtonRobotPosition.Active)
  350. {
  351. if (cmdManager.CameraStopComputePosition() != DestijlCommandManager.CommandStatus.Success)
  352. {
  353. MessagePopup(MessageType.Error,
  354. ButtonsType.Ok, "Error",
  355. "Error when stopping position reception: bad answer for supervisor or timeout");
  356. }
  357. }
  358. else
  359. {
  360. if (cmdManager.CameraComputePosition() != DestijlCommandManager.CommandStatus.Success)
  361. {
  362. MessagePopup(MessageType.Error,
  363. ButtonsType.Ok, "Error",
  364. "Error when starting getting robot position: bad answer for supervisor or timeout");
  365. checkButtonRobotPosition.Active = false;
  366. }
  367. }
  368. }
  369. protected void OnDrawingAreaCameraRealized(object sender, EventArgs e)
  370. {
  371. Console.WriteLine("Event realized. Args = " + e.ToString());
  372. }
  373. protected void OnDrawingAreaCameraExposeEvent(object o, ExposeEventArgs args)
  374. {
  375. //Console.WriteLine("Event expose. Args = " + args.ToString());
  376. DrawingArea area = (DrawingArea)o;
  377. Gdk.GC gc = area.Style.BackgroundGC(Gtk.StateType.Normal);
  378. area.GdkWindow.GetSize(out int areaWidth, out int areaHeight);
  379. area.GdkWindow.DrawPixbuf(gc, drawingareaCameraPixbuf,
  380. 0, 0,
  381. (areaWidth - drawingareaCameraPixbuf.Width) / 2,
  382. (areaHeight - drawingareaCameraPixbuf.Height) / 2,
  383. drawingareaCameraPixbuf.Width, drawingareaCameraPixbuf.Height,
  384. RgbDither.Normal, 0, 0);
  385. }
  386. protected void OnDrawingAreaCameraConfigureEvent(object o, ConfigureEventArgs args)
  387. {
  388. //Console.WriteLine("Event configure. Args = " + args.ToString());
  389. }
  390. }