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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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, byte[] buffer)
  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. else if (header.ToUpper() == DestijlCommandList.HeaderStmImage)
  124. {
  125. Console.WriteLine("Image received");
  126. byte[] image = new byte[buffer.Length-4];
  127. System.Buffer.BlockCopy(buffer, 4, image, 0, image.Length);
  128. drawingareaCameraPixbuf = new Pixbuf(image.Length, image, true);
  129. }
  130. }
  131. protected void OnQuitActionActivated(object sender, EventArgs e)
  132. {
  133. Console.WriteLine("Bye bye 2");
  134. if (cmdManager != null) cmdManager.Close();
  135. this.Destroy();
  136. Application.Quit();
  137. }
  138. protected void OnShowLogWindowActionActivated(object sender, EventArgs e)
  139. {
  140. MessagePopup(MessageType.Info,
  141. ButtonsType.Ok, "Info",
  142. "Logger not yet implemented");
  143. }
  144. protected void OnButtonServerConnectionClicked(object sender, EventArgs e)
  145. {
  146. DestijlCommandManager.CommandStatus statusCmd;
  147. if (buttonServerConnection.Label == "Disconnect")
  148. {
  149. ChangeState(SystemState.NotConnected);
  150. }
  151. else
  152. {
  153. if ((entryServerName.Text == "") || (entryServerPort.Text == ""))
  154. {
  155. MessagePopup(MessageType.Error,
  156. ButtonsType.Ok, "Error",
  157. "Server name or port is invalid");
  158. }
  159. else
  160. {
  161. Console.WriteLine("Connecting to " + entryServerName.Text + ":" + entryServerPort.Text);
  162. bool status = false;
  163. try
  164. {
  165. cmdManager.timeout = Convert.ToDouble(entryTimeout.Text);
  166. }
  167. catch (Exception)
  168. {
  169. cmdManager.timeout = 100;
  170. entryTimeout.Text = cmdManager.timeout.ToString();
  171. }
  172. try
  173. {
  174. status = cmdManager.Open(entryServerName.Text, Convert.ToInt32(entryServerPort.Text));
  175. }
  176. catch (Exception)
  177. {
  178. Console.WriteLine("Something went wrong during connection");
  179. return;
  180. }
  181. if (status != true)
  182. {
  183. MessagePopup(MessageType.Error,
  184. ButtonsType.Ok, "Error",
  185. "Unable to connect to server " + entryServerName.Text + ":" + Convert.ToInt32(entryServerPort.Text));
  186. }
  187. else
  188. {
  189. Console.Write("Send command RobotOpenCom: ");
  190. statusCmd = cmdManager.RobotOpenCom();
  191. Console.WriteLine(statusCmd.ToString());
  192. if (statusCmd == DestijlCommandManager.CommandStatus.Success)
  193. {
  194. ChangeState(SystemState.ServerConnected);
  195. }
  196. else
  197. {
  198. MessagePopup(MessageType.Error,
  199. ButtonsType.Ok, "Error",
  200. "Unable to open communication with robot.\nCheck that supervisor is accepting OPEN_COM_DMB command");
  201. cmdManager.Close();
  202. }
  203. }
  204. }
  205. }
  206. }
  207. protected void OnButtonRobotActivationClicked(object sender, EventArgs e)
  208. {
  209. DestijlCommandManager.CommandStatus status;
  210. if (buttonRobotActivation.Label == "Activate") // activation du robot
  211. {
  212. if (radioButtonWithWatchdog.Active) // Demarrage avec watchdog
  213. {
  214. status = cmdManager.RobotStartWithWatchdog();
  215. }
  216. else // Demarrage sans watchdog
  217. {
  218. status = cmdManager.RobotStartWithoutWatchdog();
  219. }
  220. if (status == DestijlCommandManager.CommandStatus.Success)
  221. {
  222. ChangeState(SystemState.RobotConnected);
  223. }
  224. else
  225. {
  226. if (status == DestijlCommandManager.CommandStatus.CommunicationLostWithServer)
  227. {
  228. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
  229. ChangeState(SystemState.NotConnected);
  230. }
  231. else
  232. {
  233. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Command rejected\nCheck that supervisor accept \nDMB_START_WITH_WD and/or DMB_START_WITHOUT_WD");
  234. }
  235. }
  236. }
  237. else // Reset du robot
  238. {
  239. status = cmdManager.RobotReset();
  240. if (status == DestijlCommandManager.CommandStatus.Success)
  241. {
  242. ChangeState(SystemState.ServerConnected);
  243. }
  244. else
  245. {
  246. if (status == DestijlCommandManager.CommandStatus.CommunicationLostWithServer)
  247. {
  248. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
  249. ChangeState(SystemState.NotConnected);
  250. }
  251. else
  252. {
  253. MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Unknown error");
  254. }
  255. }
  256. }
  257. }
  258. protected void OnButtonMouvClicked(object sender, EventArgs e)
  259. {
  260. if (sender == buttonRight)
  261. {
  262. cmdManager.RobotTurn(90);
  263. }
  264. else if (sender == buttonLeft)
  265. {
  266. cmdManager.RobotTurn(-90);
  267. }
  268. else if (sender == buttonForward)
  269. {
  270. cmdManager.RobotMove(100);
  271. }
  272. else if (sender == buttonDown)
  273. {
  274. cmdManager.RobotMove(-100);
  275. }
  276. else
  277. {
  278. MessagePopup(MessageType.Warning, ButtonsType.Ok, "Abnormal behavior", "Callback OnButtonMouvClicked called by unknown sender");
  279. }
  280. }
  281. void OnBatteryTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
  282. {
  283. DestijlCommandManager.CommandStatus status;
  284. //int batteryLevel;
  285. batteryTimer.Stop();
  286. if (checkButtonGetBattery.Active)
  287. {
  288. //status = cmdManager.RobotGetBattery(out batteryLevel);
  289. status = cmdManager.RobotGetBattery();
  290. switch (status)
  291. {
  292. case DestijlCommandManager.CommandStatus.Success:
  293. /*switch (batteryLevel)
  294. {
  295. case 2:
  296. labelBatteryLevel.Text = "High";
  297. break;
  298. case 1:
  299. labelBatteryLevel.Text = "Low";
  300. break;
  301. case 0:
  302. labelBatteryLevel.Text = "Empty";
  303. break;
  304. default:
  305. labelBatteryLevel.Text = "Unknown";
  306. break;
  307. }*/
  308. batteryTimer.Start();
  309. break;
  310. case DestijlCommandManager.CommandStatus.CommunicationLostWithServer:
  311. //MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with server");
  312. Console.WriteLine("Error: Connection lost with server");
  313. batteryTimer.Stop();
  314. labelBatteryLevel.Text = "Unknown";
  315. ChangeState(SystemState.NotConnected);
  316. break;
  317. case DestijlCommandManager.CommandStatus.CommunicationLostWithRobot:
  318. //MessagePopup(MessageType.Error, ButtonsType.Ok, "Error", "Connection lost with robot");
  319. Console.WriteLine("Error: Connection lost with robot");
  320. batteryTimer.Stop();
  321. labelBatteryLevel.Text = "Unknown";
  322. ChangeState(SystemState.ServerConnected);
  323. break;
  324. default:
  325. labelBatteryLevel.Text = "Unknown";
  326. batteryTimer.Start();
  327. break;
  328. }
  329. }
  330. else batteryTimer.Start();
  331. }
  332. protected void OnCheckButtonCameraOnClicked(object sender, EventArgs e)
  333. {
  334. if (!checkButtonCameraOn.Active)
  335. {
  336. if (cmdManager.CameraClose() != DestijlCommandManager.CommandStatus.Success)
  337. {
  338. //MessagePopup(MessageType.Error,
  339. // ButtonsType.Ok, "Error",
  340. // "Error when closing camera: bad answer for supervisor or timeout");
  341. }
  342. }
  343. else
  344. {
  345. if (cmdManager.CameraOpen() != DestijlCommandManager.CommandStatus.Success)
  346. {
  347. MessagePopup(MessageType.Error,
  348. ButtonsType.Ok, "Error",
  349. "Error when opening camera: bad answer for supervisor or timeout");
  350. checkButtonCameraOn.Active = false;
  351. }
  352. }
  353. }
  354. protected void OnCheckButtonRobotPositionClicked(object sender, EventArgs e)
  355. {
  356. if (!checkButtonRobotPosition.Active)
  357. {
  358. if (cmdManager.CameraStopComputePosition() != DestijlCommandManager.CommandStatus.Success)
  359. {
  360. MessagePopup(MessageType.Error,
  361. ButtonsType.Ok, "Error",
  362. "Error when stopping position reception: bad answer for supervisor or timeout");
  363. }
  364. }
  365. else
  366. {
  367. if (cmdManager.CameraComputePosition() != DestijlCommandManager.CommandStatus.Success)
  368. {
  369. MessagePopup(MessageType.Error,
  370. ButtonsType.Ok, "Error",
  371. "Error when starting getting robot position: bad answer for supervisor or timeout");
  372. checkButtonRobotPosition.Active = false;
  373. }
  374. }
  375. }
  376. protected void OnDrawingAreaCameraRealized(object sender, EventArgs e)
  377. {
  378. Console.WriteLine("Event realized. Args = " + e.ToString());
  379. }
  380. protected void OnDrawingAreaCameraExposeEvent(object o, ExposeEventArgs args)
  381. {
  382. //Console.WriteLine("Event expose. Args = " + args.ToString());
  383. DrawingArea area = (DrawingArea)o;
  384. Gdk.GC gc = area.Style.BackgroundGC(Gtk.StateType.Normal);
  385. area.GdkWindow.GetSize(out int areaWidth, out int areaHeight);
  386. area.GdkWindow.DrawPixbuf(gc, drawingareaCameraPixbuf,
  387. 0, 0,
  388. (areaWidth - drawingareaCameraPixbuf.Width) / 2,
  389. (areaHeight - drawingareaCameraPixbuf.Height) / 2,
  390. drawingareaCameraPixbuf.Width, drawingareaCameraPixbuf.Height,
  391. RgbDither.Normal, 0, 0);
  392. }
  393. protected void OnDrawingAreaCameraConfigureEvent(object o, ConfigureEventArgs args)
  394. {
  395. //Console.WriteLine("Event configure. Args = " + args.ToString());
  396. }
  397. }