Created
March 17, 2018 08:41
-
-
Save kawasin73/d1bbab4611e2ee9fc05a94eefa717a69 to your computer and use it in GitHub Desktop.
sample.ino がArduinoのコード、sample.rbがRubyのコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo myservo; //initiareze a servo for the connected servo: servoに名付ける | |
/*HS-5070MH Servo | |
Voltage=const:6.0-7.4V | |
Torque:3.1-3.8kg・cm*/ | |
int Angle = 0; //the set point of the angle to zero: Servoへのcommand値 | |
int myangle[3]={10,20,30}; //variable to control | |
int randomindex = 0; //the set point to zero | |
void setup() | |
{ | |
// シリアル通信の確立 | |
Serial.begin(9600); // Srial communixation: シリアルポートを9600 bps[ビット/秒]で初期化 | |
// サーボモーターと接続 | |
myservo.attach(10,750,2250 ); /*attach(pin, min, max ) - Attaches to a pin to servo,setting min and max of PMW(パルス幅) in microseconds*/ | |
Angle= 90; //set the initial posision | |
} | |
void loop() { | |
// Rubyからの通信があるまで何もしないのでここには何も書きません。 | |
// 高速でループが回るのが嫌なのでちょっとスリープして通知が来ていないかを確認する | |
delay(100); | |
} | |
void serialEvent() | |
{ | |
while(Serial.available()) | |
//シリアルポートに届いているdataのバイト数; up to 64byte/何もなければ0 | |
{ | |
// 読み込めた => 次の実験開始の合図 | |
char ch = Serial.read(); //読み込み可能なデータの最初の1byteのみ表示 | |
// サーボモーターの角度の決定 | |
randomindex = rand() % 3; //mod3の値でAngleを識別,値はrandom | |
Angle = myangle[randomindex]; //ランダムに10.20.30度 | |
// サーボモーターに角度を送信 | |
myservo.write(Angle); //ここでServoに送る.Subjectに力伝わる | |
// 角度をRubyへ通知 | |
Serial.println(Angle,DEC); // ServoのAngle の値を 10 進数(DEC)でPCに送信 | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ライブラリのインストール | |
# gem install serialport | |
require 'io/console' | |
require 'serialport' | |
# TODO: ここのnewの引数はシリアルポートの設定値なので僕はよくわかりません。 | |
sp = SerialPort.new('/dev/ttyS0', 9600, 8, 1, 0) # device, rate, data, stop, parity | |
puts "Angle( C-c: exit )\n1. 0°\n2. 90°\n3. 180°" | |
# 実験開始を通知 | |
# 送る文字列は1文字であればなんでもいいです | |
sp.puts("a") | |
# ArduinoからのAngle情報受信ループ | |
while angle_str = sp.readline.chomp.strip | |
# 角度の通知を待つ | |
if angle_str | |
# arduino で決定してサーボモーターに出力された角度が angle_strに入ってくる | |
# 被験者に入力を促す | |
puts "\nAngle( C-c: exit )\n1. 0°\n2. 90°\n3. 180°" | |
# 被験者からの入力を待つ | |
# ユーザのコマンド選択ループ | |
while c = STDIN.getch | |
# もしCtrl + c だったらプログラムを終了 | |
exit if c == ?\C-c | |
# ユーザー入力が想定していたものかを確かめる。 | |
if c == "1" || c == "2" || c == "3" | |
# ファイル書き込み | |
File.open("./data.txt", "a") do |f| | |
f.puts("angle,#{angle_str},feel,#{c}"); | |
end | |
# 実験が終了。次の実験に移る | |
# 実験開始を通知 | |
# 送る文字列は1文字であればなんでもいいです | |
# ここでは、ユーザの入力をArduinoに送信 | |
sp.puts(c) | |
# コマンド選択ループ脱出 | |
break | |
else | |
puts "Prease choose 1-3 number" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment