Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import "./App.css";
  3. import CanvasJSReact from "./canvasjs.react";
  4. import "antd/dist/antd.css";
  5. import { Button, Slider, InputNumber } from "antd";
  6.  
  7. var CanvasJSChart = CanvasJSReact.CanvasJSChart;
  8.  
  9. class App extends React.Component {
  10.     state = {
  11.         isStopped: true,
  12.         integral: 0,
  13.         derivative: 0,
  14.         setpoint: 0,
  15.         kp: 1.2,
  16.         ki: 0.003,
  17.         kd: 0
  18.     };
  19.  
  20.     componentDidMount() {
  21.         setInterval(this.updateChart, updateInterval);
  22.     }
  23.  
  24.     onToggleClick = () => {
  25.         this.setState({
  26.             isStopped: !this.state.isStopped
  27.         });
  28.     };
  29.  
  30.     onResetClick = () => {
  31.         this.setState({
  32.             isStopped: true,
  33.             derivative: 0,
  34.             integral: 0
  35.         });
  36.  
  37.         velocities = [
  38.             {
  39.                 x: 0.1,
  40.                 y: 0
  41.             }
  42.         ];
  43.  
  44.         errors = [
  45.             {
  46.                 x: 0.1,
  47.                 y: velocities[0].y
  48.             }
  49.         ];
  50.  
  51.         outputs = [
  52.             {
  53.                 x: 0.1,
  54.                 y: 0
  55.             }
  56.         ];
  57.  
  58.         this.velocitiesChart.options.data[0].dataPoints = velocities;
  59.         this.errorsChart.options.data[0].dataPoints = errors;
  60.         this.outputsChart.options.data[0].dataPoints = outputs;
  61.  
  62.         this.velocitiesChart.render();
  63.         this.errorsChart.render();
  64.         this.outputsChart.render();
  65.     };
  66.  
  67.     onSetpointChange = value => {
  68.         this.setState({
  69.             setpoint: value
  70.         });
  71.     };
  72.  
  73.     onKpChange = value => {
  74.         this.setState({
  75.             kp: value
  76.         });
  77.     };
  78.  
  79.     onKiChange = value => {
  80.         this.setState({
  81.             ki: value
  82.         });
  83.     };
  84.  
  85.     onKdChange = value => {
  86.         this.setState({
  87.             kd: value
  88.         });
  89.     };
  90.  
  91.     updateChart = () => {
  92.         if (!this.state.isStopped) {
  93.             this.calculate();
  94.  
  95.             this.velocitiesChart.render();
  96.             this.errorsChart.render();
  97.             this.outputsChart.render();
  98.         }
  99.     };
  100.  
  101.     calculate = () => {
  102.         this.calculateErrorAndFactors();
  103.         this.calculateOutput();
  104.         this.calculateVelicity();
  105.  
  106.         this.shiftArrays();
  107.     };
  108.  
  109.     calculateErrorAndFactors = () => {
  110.         const previousError = errors[errors.length - 1].y;
  111.  
  112.         let errorX = errors[errors.length - 1].x + updateInterval / 1000;
  113.         let errorY = this.state.setpoint - velocities[velocities.length - 1].y;
  114.  
  115.         this.setState({
  116.             integral: this.state.integral + errorY * updateInterval
  117.         });
  118.         this.setState({
  119.             derivative:
  120.                 this.state.derivative +
  121.                 (errorY - previousError) / updateInterval
  122.         });
  123.  
  124.         errors.push({ x: errorX, y: errorY });
  125.     };
  126.  
  127.     calculateOutput = () => {
  128.         let outputX = outputs[outputs.length - 1].x + 0.1;
  129.         let outputY =
  130.             this.state.kp * errors[errors.length - 1].y +
  131.             this.state.ki * this.state.integral +
  132.             this.state.kd * this.state.derivative;
  133.  
  134.         outputs.push({ x: outputX, y: outputY });
  135.     };
  136.  
  137.     calculateVelicity = () => {
  138.         const previousVelocity = velocities[velocities.length - 1].y;
  139.  
  140.         let velocityX = velocities[velocities.length - 1].x + 0.1;
  141.         let velocityY =
  142.             previousVelocity +
  143.             outputs[outputs.length - 1].y * 0.03 -
  144.             previousVelocity * 0.15;
  145.  
  146.         if (velocityY < 0) velocityY = 0;
  147.  
  148.         velocities.push({ x: velocityX, y: velocityY });
  149.     };
  150.  
  151.     shiftArrays = () => {
  152.         if (velocities.length > maxChartLength) velocities.shift();
  153.         if (errors.length > maxChartLength) errors.shift();
  154.         if (outputs.length > maxChartLength) outputs.shift();
  155.     };
  156.  
  157.     render() {
  158.         return (
  159.             <div className="container">
  160.                 <div className="charts">
  161.                     <CanvasJSChart
  162.                         options={errorOptions}
  163.                         onRef={ref => (this.errorsChart = ref)}
  164.                     />
  165.                     <CanvasJSChart
  166.                         options={outputOptions}
  167.                         onRef={ref => (this.outputsChart = ref)}
  168.                     />
  169.                     <CanvasJSChart
  170.                         options={velocityOptions}
  171.                         onRef={ref => (this.velocitiesChart = ref)}
  172.                     />
  173.                 </div>
  174.                 <div className="controls">
  175.                     <div className="controls__toggle">
  176.                         {this.state.isStopped ? (
  177.                             <Button
  178.                                 type="primary"
  179.                                 onClick={this.onToggleClick}
  180.                                 className="toggle__button"
  181.                             >
  182.                                 Start
  183.                             </Button>
  184.                         ) : (
  185.                             <Button
  186.                                 type="danger"
  187.                                 onClick={this.onToggleClick}
  188.                                 className="toggle__button"
  189.                             >
  190.                                 Stop
  191.                             </Button>
  192.                         )}
  193.                         <Button
  194.                             onClick={this.onResetClick}
  195.                             className="toggle__reset"
  196.                         >
  197.                             Reset
  198.                         </Button>
  199.                     </div>
  200.                     <div className="controls__inputs">
  201.                         <div className="inputs__group">
  202.                             <span>Prędkość zadana:</span>
  203.                             <Slider
  204.                                 min={0}
  205.                                 max={200}
  206.                                 onChange={this.onSetpointChange}
  207.                                 value={this.state.setpoint}
  208.                                 style={{ width: "100%" }}
  209.                             />
  210.                             <InputNumber
  211.                                 min={0}
  212.                                 max={200}
  213.                                 onChange={this.onSetpointChange}
  214.                                 value={this.state.setpoint}
  215.                                 formatter={value => `${value} km/h`}
  216.                                 parser={value => value.replace(" km/h", "")}
  217.                             />
  218.                         </div>
  219.                         <div className="inputs__group">
  220.                             <span>Kp:</span>
  221.                             <Slider
  222.                                 min={0}
  223.                                 max={10}
  224.                                 step={0.001}
  225.                                 onChange={this.onKpChange}
  226.                                 value={this.state.kp}
  227.                                 style={{ width: "100%" }}
  228.                             />
  229.                             <InputNumber
  230.                                 min={0}
  231.                                 max={10}
  232.                                 value={this.state.kp}
  233.                                 step={0.01}
  234.                                 onChange={this.onKpChange}
  235.                             />
  236.                         </div>
  237.                         <div className="inputs__group">
  238.                             <span>Ki:</span>
  239.                             <Slider
  240.                                 min={0}
  241.                                 max={0.1}
  242.                                 step={0.001}
  243.                                 onChange={this.onKiChange}
  244.                                 value={this.state.ki}
  245.                                 style={{ width: "100%" }}
  246.                             />
  247.                             <InputNumber
  248.                                 min={0}
  249.                                 max={0.1}
  250.                                 value={this.state.ki}
  251.                                 step={0.001}
  252.                                 onChange={this.onKiChange}
  253.                             />
  254.                         </div>
  255.                         <div className="inputs__group">
  256.                             <span>Kd:</span>
  257.                             <Slider
  258.                                 min={0}
  259.                                 max={30}
  260.                                 step={0.1}
  261.                                 onChange={this.onKdChange}
  262.                                 value={this.state.kd}
  263.                                 style={{ width: "100%" }}
  264.                             />
  265.                             <InputNumber
  266.                                 min={0}
  267.                                 max={30}
  268.                                 value={this.state.kd}
  269.                                 step={0.1}
  270.                                 onChange={this.onKdChange}
  271.                             />
  272.                         </div>
  273.                     </div>
  274.                 </div>
  275.             </div>
  276.         );
  277.     }
  278. }
  279.  
  280. export default App;
  281.  
  282. var updateInterval = 100;
  283. var maxChartLength = 500;
  284.  
  285. var velocities = [
  286.     {
  287.         x: 0.1,
  288.         y: 0
  289.     }
  290. ];
  291.  
  292. var errors = [
  293.     {
  294.         x: 0.1,
  295.         y: velocities[0].y
  296.     }
  297. ];
  298.  
  299. var outputs = [
  300.     {
  301.         x: 0.1,
  302.         y: 0
  303.     }
  304. ];
  305.  
  306. const velocityOptions = {
  307.     animationEnabled: true,
  308.     zoomEnabled: true,
  309.     theme: "light2",
  310.     title: {
  311.         text: "Tempomat"
  312.     },
  313.     axisX: {
  314.         title: "Czas",
  315.         suffix: "s"
  316.     },
  317.     axisY: {
  318.         title: "Prędkość",
  319.         suffix: "km/h"
  320.     },
  321.     data: [
  322.         {
  323.             type: "line",
  324.             xValueFormatString: "##.#s",
  325.             yValueFormatString: "###km/h",
  326.             dataPoints: velocities
  327.         }
  328.     ]
  329. };
  330.  
  331. const errorOptions = {
  332.     animationEnabled: true,
  333.     zoomEnabled: true,
  334.     theme: "light2",
  335.     title: {
  336.         text: "Błąd"
  337.     },
  338.     axisX: {
  339.         title: "Czas",
  340.         suffix: "s"
  341.     },
  342.     data: [
  343.         {
  344.             type: "line",
  345.             xValueFormatString: "##.#s",
  346.             dataPoints: errors
  347.         }
  348.     ]
  349. };
  350.  
  351. const outputOptions = {
  352.     animationEnabled: true,
  353.     zoomEnabled: true,
  354.     theme: "light2",
  355.     title: {
  356.         text: "Sterowanie"
  357.     },
  358.     axisX: {
  359.         title: "Czas",
  360.         suffix: "s"
  361.     },
  362.     data: [
  363.         {
  364.             type: "line",
  365.             xValueFormatString: "##.#s",
  366.             dataPoints: outputs
  367.         }
  368.     ]
  369. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement