abeの研究に利用:枚数判別システムを動作させるために必要となる
Dependencies: ros_lib_indigo mbed
main.cpp@0:8589ed2aac65, 2021-02-24 (annotated)
- Committer:
- _ai_
- Date:
- Wed Feb 24 03:02:29 2021 +0000
- Revision:
- 0:8589ed2aac65
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
_ai_ | 0:8589ed2aac65 | 1 | #include "mbed.h" |
_ai_ | 0:8589ed2aac65 | 2 | #include <ros.h> |
_ai_ | 0:8589ed2aac65 | 3 | #include <std_msgs/String.h> |
_ai_ | 0:8589ed2aac65 | 4 | #include <std_msgs/Int8.h> |
_ai_ | 0:8589ed2aac65 | 5 | #include <std_msgs/Float32MultiArray.h> |
_ai_ | 0:8589ed2aac65 | 6 | #include <std_msgs/Float32.h> |
_ai_ | 0:8589ed2aac65 | 7 | |
_ai_ | 0:8589ed2aac65 | 8 | #define photoIC_R 3300 //受光IC抵抗 |
_ai_ | 0:8589ed2aac65 | 9 | |
_ai_ | 0:8589ed2aac65 | 10 | |
_ai_ | 0:8589ed2aac65 | 11 | ////クラス宣言 |
_ai_ | 0:8589ed2aac65 | 12 | AnalogIn intensity_of_light(A0); //受光素子による抵抗変化の電圧値 |
_ai_ | 0:8589ed2aac65 | 13 | DigitalOut light_sw(D8); //発光素子を光らせるためのスイッチング(トランジスタ) |
_ai_ | 0:8589ed2aac65 | 14 | |
_ai_ | 0:8589ed2aac65 | 15 | |
_ai_ | 0:8589ed2aac65 | 16 | ////プロトタイプ宣言 |
_ai_ | 0:8589ed2aac65 | 17 | void init_func(void); //初期化を行うための関数 |
_ai_ | 0:8589ed2aac65 | 18 | void ir_led_switch_cb(const std_msgs::Int8&); |
_ai_ | 0:8589ed2aac65 | 19 | |
_ai_ | 0:8589ed2aac65 | 20 | |
_ai_ | 0:8589ed2aac65 | 21 | ////グローバル変数 |
_ai_ | 0:8589ed2aac65 | 22 | float analogIn_light_voltage=0.0; //マイコンデジタルピンにかかる電圧 |
_ai_ | 0:8589ed2aac65 | 23 | |
_ai_ | 0:8589ed2aac65 | 24 | |
_ai_ | 0:8589ed2aac65 | 25 | ////ros msg |
_ai_ | 0:8589ed2aac65 | 26 | std_msgs::Int8 IR_LED_swich; //ubuntuからIRLEDをON or OFFするかを決定するためのもの |
_ai_ | 0:8589ed2aac65 | 27 | std_msgs::Float32 photo_diode_value; //photo_diodeの値をubuntuへと送るためのもの |
_ai_ | 0:8589ed2aac65 | 28 | |
_ai_ | 0:8589ed2aac65 | 29 | |
_ai_ | 0:8589ed2aac65 | 30 | ////ros sub and pub |
_ai_ | 0:8589ed2aac65 | 31 | ros::NodeHandle n1; |
_ai_ | 0:8589ed2aac65 | 32 | ros::Subscriber<std_msgs::Int8> IR_LED_switch("/micon_ir_led_swich",&ir_led_switch_cb); |
_ai_ | 0:8589ed2aac65 | 33 | ros::Publisher photo_diode_pub("/micon_photo_diode_value", &photo_diode_value); |
_ai_ | 0:8589ed2aac65 | 34 | |
_ai_ | 0:8589ed2aac65 | 35 | |
_ai_ | 0:8589ed2aac65 | 36 | //----------------------- ここまで 設定 ---------------------------------------// |
_ai_ | 0:8589ed2aac65 | 37 | |
_ai_ | 0:8589ed2aac65 | 38 | |
_ai_ | 0:8589ed2aac65 | 39 | //関数・初期値の初期化 |
_ai_ | 0:8589ed2aac65 | 40 | void init_func(void) |
_ai_ | 0:8589ed2aac65 | 41 | { |
_ai_ | 0:8589ed2aac65 | 42 | n1.getHardware()->setBaud(115200); |
_ai_ | 0:8589ed2aac65 | 43 | n1.initNode(); |
_ai_ | 0:8589ed2aac65 | 44 | |
_ai_ | 0:8589ed2aac65 | 45 | n1.advertise(photo_diode_pub); |
_ai_ | 0:8589ed2aac65 | 46 | n1.subscribe(IR_LED_switch); |
_ai_ | 0:8589ed2aac65 | 47 | |
_ai_ | 0:8589ed2aac65 | 48 | light_sw.write(0); |
_ai_ | 0:8589ed2aac65 | 49 | } |
_ai_ | 0:8589ed2aac65 | 50 | |
_ai_ | 0:8589ed2aac65 | 51 | |
_ai_ | 0:8589ed2aac65 | 52 | //LEDのON OFFを入れ替えるcb関数 |
_ai_ | 0:8589ed2aac65 | 53 | void ir_led_switch_cb(const std_msgs::Int8& data_) |
_ai_ | 0:8589ed2aac65 | 54 | { |
_ai_ | 0:8589ed2aac65 | 55 | light_sw = data_.data; |
_ai_ | 0:8589ed2aac65 | 56 | } |
_ai_ | 0:8589ed2aac65 | 57 | |
_ai_ | 0:8589ed2aac65 | 58 | |
_ai_ | 0:8589ed2aac65 | 59 | ///// main program |
_ai_ | 0:8589ed2aac65 | 60 | int main(void) |
_ai_ | 0:8589ed2aac65 | 61 | { |
_ai_ | 0:8589ed2aac65 | 62 | init_func(); |
_ai_ | 0:8589ed2aac65 | 63 | wait(1.0); |
_ai_ | 0:8589ed2aac65 | 64 | |
_ai_ | 0:8589ed2aac65 | 65 | for(;;) { |
_ai_ | 0:8589ed2aac65 | 66 | n1.spinOnce(); |
_ai_ | 0:8589ed2aac65 | 67 | analogIn_light_voltage = intensity_of_light.read()*3.3; //0.0~1.0のレンジを0.0~3.3[V]のレンジに変更 |
_ai_ | 0:8589ed2aac65 | 68 | photo_diode_value.data = analogIn_light_voltage; |
_ai_ | 0:8589ed2aac65 | 69 | photo_diode_pub.publish(&photo_diode_value); |
_ai_ | 0:8589ed2aac65 | 70 | wait(0.01); |
_ai_ | 0:8589ed2aac65 | 71 | } |
_ai_ | 0:8589ed2aac65 | 72 | } |