public void newTrainEpoch()
        {
            lock (carModelGraphicControl1.trainingModels)
            {
                carModelGraphicControl1.trainingModels = new List <CarModel>();
                lock (carModelGraphicControl1.trainingModels)
                {
                    if (neuralController.trainInnerStates != null)
                    {
                        foreach (GridCarModelState s in neuralController.trainInnerStates)
                        {
                            carModelGraphicControl1.trainingModels.Add(new CarModel(GridCarModelState.ToCarModelState(s)));
                        }
                    }

                    if (neuralController.trainInnerStatesOrig != null)
                    {
                        foreach (GridCarModelState s in neuralController.trainInnerStatesOrig)
                        {
                            carModelGraphicControl1.trainingModels.Add(new CarModel(GridCarModelState.ToCarModelState(s), Color.Brown));
                        }
                    }
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (carRunning)
            {
                if (timerDiv == 0)
                {
                    ICarPositionProvider    carPos;
                    IFinishPositionProvider finishPos;
                    if (simulation)
                    {
                        itemManager.TakeSample();
                        carPos    = itemManager;
                        finishPos = itemManager;
                    }
                    else
                    {
                        cameraCarPosition.TakeSample();
                        carPos    = cameraCarPosition;
                        finishPos = cameraCarPosition;
                    }

                    //leallitas ha beert a celba
                    double errx  = carPos.GetCarState().Position.X - finishPos.GetFinishState(0).Position.X;
                    double erry  = carPos.GetCarState().Position.Y - finishPos.GetFinishState(0).Position.Y;
                    double errox = carPos.GetCarState().Orientation.X - finishPos.GetFinishState(0).Orientation.X;
                    double erroy = carPos.GetCarState().Orientation.Y - finishPos.GetFinishState(0).Orientation.Y;

                    if ((errx * errx + erry * erry < CarModel.SHAFT_LENGTH * CarModel.SHAFT_LENGTH) && (errox * errox + erroy * erroy < 0.2))
                    {
                        buttonStopSim_Click(this, null);
                    }
                    else
                    {
                        carModelGraphicControl1.SetReceiveCommand();
                        GridCarModelInput oi;
                        GridCarModelState os;
                        neuralController.SimulateOneStep(GridCarModelState.FromCarModelState(carPos.GetCarState()), out oi, out os);
                        outState = GridCarModelState.ToCarModelState(os);
                        outInput = new CarModelInput(oi.Angle);

                        //outInput = new CarModelInput(20, 100);
                        if (checkBoxSerial.Checked)
                        {
                            byte leftspd  = (byte)Convert.ToSByte(ComMath.Normal(outInput.LeftSpeed, -180, 180, -128, 127));
                            byte rightspd = (byte)Convert.ToSByte(ComMath.Normal(outInput.RightSpeed, -180, 180, -128, 127)); //-125, 124
                            if (checkBoxCarEnable.Checked)
                            {
                                serialComm.Motor_I2C_Forward(1, leftspd, rightspd);
                            }
                            //Thread.Sleep(200);
                        }
                    }
                }

                timerDiv = (timerDiv + 1) % (long)(CarModel.SIMULATION_TIME_STEP * 1000.0 / timer1.Interval);
                if (simulation)
                {
                    //itemManager.Simulate(new MathModelSimulator(), outInput, timer1.Interval / 1000.0);
                    itemManager.SimualteGrid(new GridMathModelSimulator(), new GridCarModelInput(outInput.LeftSpeed, outInput.RightSpeed), timer1.Interval / 1000.0);
                }
                else
                {
                    cameraCarPosition.Simulate(new MathModelSimulator(), outInput, timer1.Interval / 1000.0);
                }
            }

            carModelGraphicControl1.Invalidate();
        }