private Action <ReplaySelectDialogModel> OnReplaySelectDialogClose(CustomDialog customDialog)
        {
            return(obj =>
            {
                Parent.SyncContext.Send(d =>
                {
                    Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);
                }, null);

                Parent.SyncContext.Post(d =>
                {
                    Task.Factory.StartNew(async() =>
                    {
                        ReplaySelectDialogModel replaySelectDialogModel = obj as ReplaySelectDialogModel;
                        if (replaySelectDialogModel.SelectedFile != null)
                        {
                            RemoteDataStore remoteDataStore = new RemoteDataStore(Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.IpAddress, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Password);

                            string guid = Guid.NewGuid().ToString();
                            string localFile = "";
                            string remoteFile = "";
                            string remoteFolder = string.Format(@"/var/tmp/firefly/{0}", guid);
                            string expactString = string.Format("{0}@{1}:.{{0,}}[$]", Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Hostname);

                            string imuModel = Imu.ConvertImuModelToString(Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.ImuModel);

                            Parent.SyncContext.Send(c =>
                            {
                                localFile = replaySelectDialogModel.SelectedFile.FullPath;
                                remoteFile = string.Format(@"/var/tmp/firefly/{0}/{1}", guid, Path.GetFileName(localFile));
                            }, null);

                            var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Calculating calibration parameter now!", settings: Parent.MetroDialogSettings);
                            controller.SetIndeterminate();
                            controller.SetCancelable(false);

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format("mkdir -p {0}", remoteFolder)
                            }, expactString);

                            remoteDataStore.UploadFile(remoteFile, localFile);

                            remoteDataStore.UploadContentToFile(string.Format(@"{0}/target.yaml", remoteFolder), YamlTranslator.ConvertToYaml(new CalibrationTarget()
                            {
                                TargetType = CalibrationTargetType.Aprilgrid,
                                TagSize = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagSize,
                                TagSpacing = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagSpacingFactor,
                                TagCols = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagsX,
                                TagRows = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagsY
                            }));

                            remoteDataStore.UploadContentToFile(string.Format(@"{0}/imu.yaml", remoteFolder), YamlTranslator.ConvertToYaml(new Imu()
                            {
                                RosTopic = "/imu0",
                                UpdateRate = Parent.SettingContainer.Settings.ImuSettings.UpdateRate,
                                AccelerometerNoiseDensity = Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.AccelerometerNoiseDensity * Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.AccelerometerNoiseDensitySafetyScale,
                                AccelerometerRandomWalk = Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.AccelerometerRandomWalk * Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.AccelerometerRandomWalkSafetyScale,
                                GyroscopeNoiseDensity = Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.GyroscopeNoiseDensity * Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.GyroscopeNoiseDensitySafetyScale,
                                GyroscopeRandomWalk = Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.GyroscopeRandomWalk * Parent.SettingContainer.Settings.CalibrationSettings.ImuCalibration.GyroscopeRandomWalkSafetyScale
                            }));

                            remoteDataStore.UploadContentToFile(string.Format(@"{0}/cam.yaml", remoteFolder), YamlTranslator.ConvertToYaml(new CameraChain()
                            {
                                Cam0 = new Data.Storage.Model.Camera()
                                {
                                    CameraModel = CameraModel.Pinhole,
                                    DistortionModel = DistortionModel.Equidistant,
                                    DistortionCoefficients = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.DistCoeffs.ToArray(),
                                    Fx = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fx,
                                    Fy = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fy,
                                    Cx = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cx,
                                    Cy = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cy,
                                    RosTopic = "/cam0/image_raw",
                                    Height = Parent.SettingContainer.Settings.CameraSettings.Height,
                                    Width = Parent.SettingContainer.Settings.CameraSettings.Width
                                }
                            }));

                            string options = string.Format(CultureInfo.InvariantCulture, "--dont-show-report --reprojection-sigma {0} {1}", Parent.SettingContainer.Settings.CalibrationSettings.ExtrinsicCalibrationSettings.ReprojectionSigma, Parent.SettingContainer.Settings.CalibrationSettings.ExtrinsicCalibrationSettings.TimeCalibration ? "--time-calibration" : "");

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format(@"cd {0}", remoteFolder),
                                string.Format(@"unzip {0} -d {1}", Path.GetFileName(localFile), Path.GetFileNameWithoutExtension(localFile)),
                                @"source ~/kalibr_workspace/devel/setup.bash",
                                string.Format(@"kalibr_bagcreater --folder {0} --output-bag {0}.bag", Path.GetFileNameWithoutExtension(localFile)),
                                string.Format(@"kalibr_calibrate_imu_camera --bag {0}.bag --cams cam.yaml --imu imu.yaml --imu-models {1} --target target.yaml {2}", Path.GetFileNameWithoutExtension(localFile), imuModel, options),
                                string.Format("pdftoppm report-imucam-{0}.pdf result -png", Path.GetFileNameWithoutExtension(localFile))
                            }, expactString);

                            CameraChain cameraChain = YamlTranslator.ConvertFromYaml <CameraChain>(remoteDataStore.DownloadFileToMemory(string.Format("{0}/camchain-imucam-{1}.yaml", remoteFolder, Path.GetFileNameWithoutExtension(localFile))));
                            ImuCain imuChain = YamlTranslator.ConvertFromYaml <ImuCain>(remoteDataStore.DownloadFileToMemory(string.Format("{0}/imu-{1}.yaml", remoteFolder, Path.GetFileNameWithoutExtension(localFile))));

                            List <string> availablePlots = new List <string>();

                            foreach (string file in remoteDataStore.GetAllFileNames(remoteFolder))
                            {
                                if (file.Contains(".csv"))
                                {
                                    availablePlots.Add(file.Replace(string.Format("data-imucam-{0}.", Path.GetFileNameWithoutExtension(localFile)), "").Replace(".csv", ""));
                                }
                            }

                            Dictionary <string, string> plotDataImu = new Dictionary <string, string>();

                            foreach (string plot in availablePlots)
                            {
                                plotDataImu.Add(plot, remoteDataStore.DownloadFileToMemory(string.Format("{0}/data-imucam-{1}.{2}.csv", remoteFolder, Path.GetFileNameWithoutExtension(localFile), plot)));
                            }

                            string outputPath = Path.Combine(Path.GetTempPath(), "firefly", guid);

                            if (!Directory.Exists(outputPath))
                            {
                                Directory.CreateDirectory(outputPath);
                            }

                            foreach (string file in remoteDataStore.GetAllFileNames(remoteFolder))
                            {
                                if (!file.Contains(".bag") && !file.Contains(".ffc"))
                                {
                                    remoteDataStore.DownloadFile(string.Format("{0}/{1}", remoteFolder, file), Path.Combine(outputPath, file));
                                }
                            }

                            CsvToMatlabWritter csvToMatlabWritter = new CsvToMatlabWritter(Path.Combine(outputPath, "plot-data.mat"));
                            csvToMatlabWritter.Write(plotDataImu, "ExtrinsicCalibrationData");
                            csvToMatlabWritter.Save();

                            ShowResults(outputPath, cameraChain, imuChain);

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format(@"rm -r {0}", remoteFolder)
                            }, expactString);

                            await controller.CloseAsync();
                        }
                    });
                }, null);
            });
        }
        private Action <ReplaySelectDialogModel> OnReplaySelectDialogClose(CustomDialog customDialog)
        {
            return(obj =>
            {
                Parent.SyncContext.Send(d =>
                {
                    Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);
                }, null);

                Parent.SyncContext.Post(d =>
                {
                    Task.Factory.StartNew(async() =>
                    {
                        ReplaySelectDialogModel replaySelectDialogModel = obj as ReplaySelectDialogModel;
                        if (replaySelectDialogModel.SelectedFile != null)
                        {
                            RemoteDataStore remoteDataStore = new RemoteDataStore(Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.IpAddress, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Password);

                            string guid = Guid.NewGuid().ToString();
                            string localFile = "";
                            string remoteFile = "";
                            string remoteFolder = string.Format(@"/var/tmp/firefly/{0}", guid);
                            string expactString = string.Format("{0}@{1}:.{{0,}}[$]", Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Hostname);

                            Parent.SyncContext.Send(c =>
                            {
                                localFile = replaySelectDialogModel.SelectedFile.FullPath;
                                remoteFile = string.Format(@"/var/tmp/firefly/{0}/{1}", guid, Path.GetFileName(localFile));
                            }, null);

                            var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Calculating calibration parameter now!", settings: Parent.MetroDialogSettings);
                            controller.SetIndeterminate();
                            controller.SetCancelable(false);

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format("mkdir -p {0}", remoteFolder)
                            }, expactString);

                            remoteDataStore.UploadFile(remoteFile, localFile);

                            remoteDataStore.UploadContentToFile(string.Format(@"{0}/target.yaml", remoteFolder), YamlTranslator.ConvertToYaml(new CalibrationTarget()
                            {
                                TargetType = CalibrationTargetType.Aprilgrid,
                                TagSize = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagSize,
                                TagSpacing = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagSpacingFactor,
                                TagCols = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagsX,
                                TagRows = Parent.SettingContainer.Settings.CalibrationSettings.AprilGridCalibration.TagsY
                            }));

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format(@"cd {0}", remoteFolder),
                                string.Format(@"unzip {0} -d {1}", Path.GetFileName(localFile), Path.GetFileNameWithoutExtension(localFile)),
                                @"source ~/kalibr_workspace/devel/setup.bash",
                                string.Format(@"kalibr_bagcreater --folder {0} --output-bag {0}.bag", Path.GetFileNameWithoutExtension(localFile)),
                                string.Format(@"kalibr_calibrate_cameras --bag {0}.bag --topics /cam0/image_raw --models pinhole-equi --target target.yaml --dont-show-report", Path.GetFileNameWithoutExtension(localFile)),
                                string.Format("pdftoppm report-cam-{0}.pdf result -png", Path.GetFileNameWithoutExtension(localFile))
                            }, expactString);

                            CameraChain cameraChain = YamlTranslator.ConvertFromYaml <CameraChain>(remoteDataStore.DownloadFileToMemory(string.Format("{0}/camchain-{1}.yaml", remoteFolder, Path.GetFileNameWithoutExtension(localFile))));

                            string outputPath = Path.Combine(Path.GetTempPath(), "firefly", guid);

                            if (!Directory.Exists(outputPath))
                            {
                                Directory.CreateDirectory(outputPath);
                            }

                            foreach (string file in remoteDataStore.GetAllFileNames(remoteFolder))
                            {
                                if (!file.Contains(".bag") && !file.Contains(".ffc"))
                                {
                                    remoteDataStore.DownloadFile(string.Format("{0}/{1}", remoteFolder, file), Path.Combine(outputPath, file));
                                }
                            }

                            ShowResults(outputPath, cameraChain);

                            remoteDataStore.ExecuteCommands(new List <string>()
                            {
                                string.Format(@"rm -r {0}", remoteFolder)
                            }, expactString);

                            await controller.CloseAsync();
                        }
                    });
                }, null);
            });
        }
Example #3
0
        private Task DoPrintBoard(object o)
        {
            return(Task.Factory.StartNew(() =>
            {
                Parent.SyncContext.Post(c =>
                {
                    if (o is string && !string.IsNullOrEmpty(o as string))
                    {
                        switch (o as string)
                        {
                        case "AprilGrid":
                            {
                                CustomDialog customDialog = new CustomDialog()
                                {
                                    Title = "Board Properties"
                                };

                                var dataContext = new PrintAprilGridDialogModel(obj =>
                                {
                                    Parent.SyncContext.Post(d =>
                                    {
                                        Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);

                                        System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
                                        saveFileDialog.Filter = "Portable Document Format (*.pdf) | *.pdf";

                                        if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                        {
                                            RemoteDataStore remoteDataStore = new RemoteDataStore(Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.IpAddress, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Password);

                                            string expactString = string.Format("{0}@{1}:.{{0,}}[$]", Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Username, Parent.SettingContainer.Settings.ConnectionSettings.SelectedConnection.Hostname);
                                            string guid = Guid.NewGuid().ToString();
                                            string remoteFolder = string.Format(@"/var/tmp/firefly/{0}", guid);

                                            remoteDataStore.ExecuteCommands(new List <string>()
                                            {
                                                string.Format("mkdir -p {0}", remoteFolder)
                                            }, expactString);

                                            string res = remoteDataStore.ExecuteCommands(new List <string>()
                                            {
                                                string.Format(@"cd {0}", remoteFolder),
                                                @"source ~/kalibr_workspace/devel/setup.bash",
                                                string.Format(CultureInfo.InvariantCulture, @"kalibr_create_target_pdf --type apriltag --nx {2} --ny {3} --tsize {0} --tspace {1} {4}/{5}", obj.TagSize, obj.TagSpacingFactor, obj.TagsX, obj.TagsY, remoteFolder, Path.GetFileName(saveFileDialog.FileName)),
                                            }, expactString);

                                            remoteDataStore.DownloadFile(string.Format("{0}/{1}", remoteFolder, Path.GetFileName(saveFileDialog.FileName)), saveFileDialog.FileName);

                                            remoteDataStore.ExecuteCommands(new List <string>()
                                            {
                                                string.Format(@"rm -r {0}", remoteFolder)
                                            }, expactString);
                                        }
                                    }, null);
                                },
                                                                                obj =>
                                {
                                    Parent.SyncContext.Post(d =>
                                    {
                                        Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);
                                    }, null);
                                });

                                dataContext.TagSize = TagSize;
                                dataContext.TagSpacingFactor = TagSpacingFactor;
                                dataContext.TagsX = TagsX;
                                dataContext.TagsY = TagsY;

                                customDialog.Content = new PrintAprilGridBoardDialog {
                                    DataContext = dataContext
                                };

                                Parent.DialogCoordinator.ShowMetroDialogAsync(Parent, customDialog);
                            }
                            break;

                        case "ChAruco":
                            {
                                CustomDialog customDialog = new CustomDialog()
                                {
                                    Title = "Board Properties"
                                };

                                var dataContext = new PrintCharucoBoardDialogModel(obj =>
                                {
                                    Parent.SyncContext.Post(d =>
                                    {
                                        Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);

                                        System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
                                        saveFileDialog.Filter = "Portable Network Graphics (*.png) | *.png";

                                        if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                        {
                                            CvInvoke.Imwrite(saveFileDialog.FileName, obj.Image.CvImage, new KeyValuePair <Emgu.CV.CvEnum.ImwriteFlags, int>()
                                            {
                                            });
                                        }
                                    }, null);
                                },
                                                                                   obj =>
                                {
                                    Parent.SyncContext.Post(d =>
                                    {
                                        Parent.DialogCoordinator.HideMetroDialogAsync(Parent, customDialog);
                                    }, null);
                                });

                                dataContext.SquareLength = (int)Math.Round(SquareLength / 0.0254 * 100);
                                dataContext.MarkerLength = (int)Math.Round(MarkerLength / 0.0254 * 100);
                                dataContext.Dictionary = Dictionary;
                                dataContext.SquaresX = SquaresX;
                                dataContext.SquaresY = SquaresY;

                                customDialog.Content = new PrintCharucoBoardDialog {
                                    DataContext = dataContext
                                };

                                Parent.DialogCoordinator.ShowMetroDialogAsync(Parent, customDialog);
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }, null);
            }));
        }