大概的構想是 : RPI + Arduino
Arduino 因為有類比輸入....很方便.....而且是12 bit....負責去讀取 類比資料
然後RPI 透過 usb 轉 Serial 的方式去和 Arduino 要資料 ...
然後在RPI 上面畫 line plot.....
在RPI 部分....
我找個一個sample -> Ref 1:
他會一直去更新圖表....這就是我們要的....
看了程式...發覺在 class DataGen 的 next
self.data = [self.datagen.next()] ->
def next(self):
self._recalc_data() ->
 def _recalc_data(self):
        delta = random.uniform(-0.5, 0.5)
        r = random.random()
        if r > 0.9:
            self.data += delta * 15
        elif r > 0.8:
            # attraction to the initial value
            delta += (0.5 if self.init > self.data else -0.5)
            self.data += delta
        else:
            self.data += delta
  這裡他是用random 的方式去產生資料 ...然後把資料insert 到  self.data
 所以我們只需要改這裡即可.....
可以參考Ref 2
import serial
//write 方面
ser.write(output) ser.flush()
//read 方面
x=ser.readline()
print x
在 Arduino 部分...這部分比較簡單....大概就是 serial 傳輸加上 讀analogy value....
int firstSensor =0;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
example :
analogValue = analogRead(0);
// print it out in many formats:
Serial.println(analogValue); // print as an ASCII-encoded decimal
Serial.println(analogValue, DEC); // print as an ASCII-encoded decimal
Serial.println(analogValue, HEX); // print as an ASCII-encoded hexadecimal
Serial.println(analogValue, OCT); // print as an ASCII-encoded octal
Serial.println(analogValue, BIN); // print as an ASCII-encoded binary
Reference 1 : https://github.com/eliben/code-for-blog/blob/master/2008/wx_mpl_dynamic_graph.py
Reference 2 : http://owenson.me/build-your-own-quadcopter-autopilot/pyserver.py.txt
Reference 3 : http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/
Reference 4 : http://coopermaa2nd.blogspot.tw/2011/02/arduino-serial-library.html
import serial
          
      
           ser = serial.Serial(
              
               port='/dev/ttyUSB0',
               baudrate = 9600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
           counter=0
          
      
           while 1:
               x=ser.readline()
在 Arduino 部分...這部分比較簡單....大概就是 serial 傳輸加上 讀analogy value....
int firstSensor =0;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
 if (Serial.available() > 0) {
    // get incoming byte:
    //inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    firstSensor = analogRead(A0)/4;
      // send sensor values:
    Serial.write(firstSensor);
   
  }
}
這邊有個例子:
你可以寫兩支程式,一支在 Arduino 上跑,另一支在 PC 上跑,讓 Arduino 跟 PC 端的程式彼此通訊,建立互動。底下有個簡單的範例:
Arduino 這端的程式 (potentiometer.pde) 不斷地讀取 Sensor 並把資料寫到 Serial port (以位元組的格式寫出):
01int potPin = 3; // potentiometer pin 
02 
03void setup() { 
04  Serial.begin(9600); 
05} 
06 
07void loop() { 
08  int sensorValue = analogRead(potPin); 
09  Serial.println(sensorValue/4, BYTE); 
10  delay(150); 
11} 
而 PC 端的 processing 程式 (pRead.pde) 則是把資料從 PC 端的 Serial port 讀取進來:
01import processing.serial.*; 
02 
03Serial myPort;  // The Serial port 
04 
05void setup() 
06{ 
07  // 開啟 Serial port,通訊速率為 9600 bps 
08  String portName = "COM4"; 
09  myPort = new Serial(this, portName, 9600); 
10} 
11 
12void draw() 
13{ 
14  // 檢查 Serial port 是否有資料進來 
15  if ( myPort.available() > 0) { 
16    // 讀取進來的 byte 
17    int incomingByte = myPort.read(); 
18      
19    // 印出收到的資料 
20    println(incomingByte); 
21  } 
22} 
ps : matplotlib 安裝
git clone https://github.com/matplotlib/matplotlib cd matplotlib python setup.py build sudo python setup.py insps : arduino println()
Description
Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms as Serial.print().Syntax
Serial.println(val) Serial.println(val, format)Parameters
val: the value to print - any data typetall
example :
analogValue = analogRead(0);
// print it out in many formats:
Serial.println(analogValue); // print as an ASCII-encoded decimal
Serial.println(analogValue, DEC); // print as an ASCII-encoded decimal
Serial.println(analogValue, HEX); // print as an ASCII-encoded hexadecimal
Serial.println(analogValue, OCT); // print as an ASCII-encoded octal
Serial.println(analogValue, BIN); // print as an ASCII-encoded binary
write()
Description
Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.
Syntax
Serial.write(val)
Serial.write(str)
Serial.write(buf, len)
Serial.write(str)
Serial.write(buf, len)
Arduino Mega also supports: Serial1, Serial2, Serial3 (in place of Serial)
Parameters
val: a value to send as a single byte
str: a string to send as a series of bytes
buf: an array to send as a series of bytes
len: the length of the buffer
Reference 1 : https://github.com/eliben/code-for-blog/blob/master/2008/wx_mpl_dynamic_graph.py
Reference 2 : http://owenson.me/build-your-own-quadcopter-autopilot/pyserver.py.txt
Reference 3 : http://www.instructables.com/id/Read-and-write-from-serial-port-with-Raspberry-Pi/
Reference 4 : http://coopermaa2nd.blogspot.tw/2011/02/arduino-serial-library.html
沒有留言:
張貼留言