Pid graph.py

From wikidb
Jump to: navigation, search

Execute

The command is of the form

 python pid_graph.py <plot name> <plot snapshot for seconds>

For example, to collect 8 seconds of data and save the plot in your account on plot.ly site as 153020pidplot. ^c to create graph or wait for 8 seconds for the program to terminate.

 python pid_graph.py 153020pidplot 8

Code

#!/usr/bin/env python

import sys
import rospy
import signal
import sys
import sched, time
from std_msgs.msg import Float64MultiArray
import plotly.plotly as the_graph
import plotly.graph_objs as the_line
import plotly
plotly.tools.set_credentials_file(username='xxx', api_key='yyy')

the_time = []
wheel0_speeds = []
wheel0_power = []
time_scheduler = sched.scheduler(time.time, time.sleep)

def create_graph():
    print('graph created')
    trace0 = the_line.Scatter(
        x = the_time,
        y = wheel0_speeds,
        mode = 'lines',
        name = 'wheel 0 speed'
    )
    trace1 = the_line.Scatter(
        x = the_time,
        y = wheel0_power,
        mode = 'lines',
        name = 'wheel 0 power'
    )
    data = [trace0,trace1]
    the_graph.plot(data, filename=sys.argv[1])
    #the_graph.plot(data, filename='my-graph')
    sys.exit(0)

def control_c_interupt(signal, frame):
    create_graph()

def scheduler_interupt (i):
    create_graph()

def plot_point(wheels_state):
    #print (wheels_state.data[1], wheels_state.data[3]) 
    the_time.append(wheels_state.data[0])
    wheel0_speeds.append(wheels_state.data[1] * 300.0)
    wheel0_power.append(wheels_state.data[2])
    #print (the_time[0], wheel0_speeds[0])
      
if __name__ == '__main__':
    global i
    i = 0
    print ('Pid Tuning Graph')
    rospy.init_node('pid_graph', anonymous=True)
    rospy.Subscriber("wheels", Float64MultiArray, plot_point)
    signal.signal(signal.SIGINT, control_c_interupt)
    time_scheduler.enter(int(sys.argv[2]), 1, scheduler_interupt, (0,))
    time_scheduler.run()
    rospy.spin()