python graphTerminal

all about robots

python graphTerminal

Postby chris on Tue Jan 01, 2013 3:04 pm

This python program is a graphical terminal which can be controlled by the serial line.
For instance an Arduino may send comands by the serial line and draw a graph.

This program is based on "pygame".
To run it you need at least installed:
- Python 2.7 ( http://www.python.org/getit/ )
- pygame ( http://www.pygame.org/download.shtml )
if you use windows, you need additionally installed
- pyserial ( http://pyserial.sourceforge.net/pyserial.html )
possibly you may set the PATH-Variable in windows ( Systemsteuerung->Erweitert->PATH ) to the location where you have python installed ( e.g. C:\python2.7 )
In the python script you should set up the COM-Port from which you want to receive the data
e.g. Linux: ser = serial.Serial('/dev/ttyACM0', 9600) #if your Arduino is attached to /dev/ttyACM0
e.g. windows: ser = serial.Serial('COM17', 9600) #if your Arduino is attached to COM17



The comands implemented in this version are:

cls : clear screen
plot x y : plot a point at x,y. Example: plot 100 200
color <color> : set the color for the next drawing. Example: color red
pos x y : set the position for the next drawing. Example: pos 100 150
line x y : plot a line from the last position to x,y. Example: line 200 300
print <string> : print a string at the current position. Example: print hello world
w2file <string> : writes the string to a file ( usefull for data logging )

Here is an Arduino example for drawing a graph with analog values from the AD-converter:
Code: Select all
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;       

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}
void set(char* text,int a,int b)
{
    Serial.print(text);
    Serial.print(" ");
    Serial.print(a);
    Serial.print(" ");
    Serial.println(b);
    Serial.flush();
}

int x=641;

void loop() {
  if(x>640)
  {
    Serial.println("cls"); // clear screen
    Serial.println("pos 0 460");  // set cursor position to 0,460
    Serial.println("color white");
    Serial.println("print this is an analog graph");
    Serial.println("print _______________________");
    Serial.println("pos 0 0"); // set cursor position to 0,0
    Serial.println("color yellow");
    x=0;
  }
  // read the analog in value:
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 460); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  set("line",x,outputValue);
  x++;             
}
Attachments
graphTerminal_20130101.zip
(2.82 KiB) Downloaded 485 times
chris
 
Posts: 289
Joined: Sun May 25, 2008 7:21 am

Re: python graphTerminal

Postby chris on Fri Jan 04, 2013 7:37 pm

Arduino Programm for drawing a graph with axis:
Code: Select all
// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}
void set(char* text,int a,int b)
{
    Serial.print(text);
    Serial.print(" ");
    Serial.print(a);
    Serial.print(" ");
    Serial.println(b);
    Serial.flush();
}

int x=641;
#define XOFFSET 50
#define XMAX 550
#define YOFFSET 100
#define YMAX 450
#define XTICKS 10
#define YTICKS 5
#define TICKLENGTH 20
#define AXISCOLOR "WHITE"
#define TEXTSIZE 20
#define SAMPLETIME_MS 100UL // time resolution in milli seconds
// draw xAxis
void xAxis()
{
  Serial.print("color ");Serial.println(AXISCOLOR);
  set("pos",XOFFSET,YOFFSET);
  set("line",XMAX,YOFFSET);
  set("pos",XMAX+TEXTSIZE,YOFFSET+TEXTSIZE/2);
  Serial.println("print [s]");
  int n;
  for(n=0;n<XTICKS+1;n++)
  {
    set("pos",XOFFSET+(XMAX-XOFFSET)/XTICKS*n,YOFFSET-TICKLENGTH/2);
    set("line",XOFFSET+(XMAX-XOFFSET)/XTICKS*n,YOFFSET+TICKLENGTH/2);
    set("pos",XOFFSET+(XMAX-XOFFSET)/XTICKS*n,YOFFSET-TICKLENGTH);
    Serial.print("print ");Serial.println((XMAX-XOFFSET)/XTICKS*n*SAMPLETIME_MS/1000);
  }
}
// draw yAxis
void yAxis()
{
  Serial.print("color ");Serial.println(AXISCOLOR);
  set("pos",XOFFSET,YOFFSET);
  set("line",XOFFSET,YMAX);
  set("pos",XOFFSET-TEXTSIZE/2,YMAX+TEXTSIZE);
  Serial.println("print [V]");
  int n;
  for(n=0;n<YTICKS+1;n++)
  {
    set("pos",XOFFSET-TICKLENGTH/2,YOFFSET+(YMAX-YOFFSET)/YTICKS*n);
    set("line",XOFFSET+TICKLENGTH/2,YOFFSET+(YMAX-YOFFSET)/YTICKS*n);
    set("pos",XOFFSET-TICKLENGTH/2-20,YOFFSET+(YMAX-YOFFSET)/YTICKS*n);
    Serial.print("print ");Serial.println(5/YTICKS*n);
  }
}
boolean logFlag=false;

unsigned long oldTime=0;
unsigned long tm=0;
void loop() {
  if(x>XMAX)
  {
    Serial.println("cls"); // clear screen
    Serial.println("pos 260 460");  // set cursor position to 0,460
    Serial.println("color white");
    Serial.println("print analog chanel 0 over time");
    xAxis();
    yAxis();
    Serial.println("pos 50 50"); // set cursor position to 0,0
    Serial.println("color green");
    Serial.print("print sampling time ");Serial.print(SAMPLETIME_MS);Serial.println(" ms");
    Serial.println("color white");
    Serial.println("print press any key to log in data.txt");
    Serial.println("color yellow");
    x=XOFFSET;
    oldTime=millis();
  }
  while(millis()<oldTime);
  oldTime+=SAMPLETIME_MS;
  // read the analog in value:
  sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, YOFFSET, YMAX);         
  if (x==XOFFSET) set("pos",x,outputValue);
  set("line",x,outputValue);
  if(logFlag) Serial.print("w2file ");Serial.print(x);Serial.print(" ");Serial.println(outputValue);
  x++;
  if (Serial.available()>0)
  {
    char c=Serial.read();
    logFlag=!logFlag;
    if(logFlag)
    {
      Serial.println("color green");
      Serial.print("print ");Serial.println("logOn");
      if(logFlag) Serial.println("w2file ====== new log =============");
    }
    else
    {
      Serial.println("color yellow");
      Serial.print("print ");Serial.println("logOff");
    }
  }   
}
Attachments
arduGraphTermnal.png
arduGraphTermnal.png (32.77 KiB) Viewed 2701 times
chris
 
Posts: 289
Joined: Sun May 25, 2008 7:21 am


Return to robots

Who is online

Users browsing this forum: No registered users and 1 guest

cron