使用Kotlin简单分析PID算法

前言

老师布置了个任务,用编程实现PID调节,鉴于我们专业都学过C语言和VB,于是我就想拿Kotlin练练手.
上网搜索一番别人怎么用C实现的,学习一番后,自己用Kotlin实现了下,并将PID算法的数据可视化,可以直观的感受到各种算法的优点.

Gradle

buildscript {
    ext.kotlin_version = '1.2.10'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

group 'bishisimo'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'

sourceCompatibility = 1.8
mainClassName='motionControl.PIDKt'
repositories {
    mavenCentral()
    maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.16'
    /*图表*/
    compile 'org.jfree:jfreechart-fx:1.0.1'
    compile 'org.jfree:jfreechart:1.5.0'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
kotlin {
    experimental {
        coroutines "enable"
    }
}

简单的可视化功能

package mJFreeChart

import org.jfree.chart.ChartFactory
import org.jfree.chart.ChartPanel
import org.jfree.chart.StandardChartTheme
import org.jfree.chart.title.TextTitle
import org.jfree.chart.ui.ApplicationFrame
import org.jfree.data.category.DefaultCategoryDataset
import java.awt.BasicStroke
import java.awt.Color
import java.awt.Font
import java.util.*


data class LineDataSet(val value: Double, val xTitle: String, val xPosition: Comparable<Int>)

class MJFreeChart : ApplicationFrame("666") {
   
    var seriesCount=0

    fun lineChart(list: ArrayList<ArrayList<LineDataSet>>) {
   
        val font = Font("宋体", Font.ITALIC, 15)
        val chartTheme = StandardChartTheme("CN")
        // 设置标题字体
        chartTheme.extraLargeFont = font
        // 设置图例的字体
        chartTheme.regularFont = font
        // 设置轴向的字体
        chartTheme.largeFont = font
        chartTheme.smallFont = font
        chartTheme.titlePaint = Color(51, 51, 51)//Paint 可以理解为绘制颜色;标题字体颜色

        chartTheme.legendBackgroundPaint = Color.LIGHT_GRAY// 设置标注背景色
        chartTheme.legendItemPaint = Color.BLACK//设置字体颜色
        chartTheme.chartBackgroundPaint = Color.WHITE//图表背景色
        chartTheme.domainGridlinePaint = Color(120, 255, 201)// X坐标轴垂直网格颜色
//        chartTheme.rangeGridlinePaint = Color(25, 255, 255)// Y坐标轴水平网格颜色
        ChartFactory.setChartTheme(chartTheme)//设置主题样式

        val lineDataSet = DefaultCategoryDataset()
        list.flatMap {
    it }
                .forEach {
   
                    seriesCount++
                    lineDataSet.addValue(it.value, it.xTitle, it.xPosition)//arg1为y值,arg2为线条类,arg3为在x轴上的位置
                }

        val chart = ChartFactory.createLineChart("Title", "Category", "value", lineDataSet)
        val plot = chart.categoryPlot
        plot.domainAxis.isTickLabelsVisible = false
        plot.isDomainGridlinesVisible=true
        plot.isRangeGridlinesVisible = true //是否显示格子线
//        plot.backgroundAlpha = 0.2f //设置背景透明度
        for (i in 0 until  seriesCount){
   
            plot.renderer.setSeriesStroke(i, BasicStroke(2f))
        }
        //设置主标题
        chart.title = TextTitle("PID调节比较", font)
        chart.antiAlias = true//抗锯齿
//        chart.setTextAntiAlias(false)


        val chartPanne = ChartPanel(chart)
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值