1/23 操舵 レシーバ
Dependencies: ADXL345_I2C INA226_ver1 mbed
Fork of RS485R_2 by
main.cpp@0:fafac0045ad3, 2015-12-04 (annotated)
- Committer:
- taurin
- Date:
- Fri Dec 04 22:42:20 2015 +0000
- Revision:
- 0:fafac0045ad3
- Child:
- 1:563b7d0b896f
??????????????;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
taurin | 0:fafac0045ad3 | 1 | //相互通信確認用 |
taurin | 0:fafac0045ad3 | 2 | #include "mbed.h" |
taurin | 0:fafac0045ad3 | 3 | #define BUFFER 30 |
taurin | 0:fafac0045ad3 | 4 | |
taurin | 0:fafac0045ad3 | 5 | Serial rs485(p9,p10); |
taurin | 0:fafac0045ad3 | 6 | Serial pc(USBTX,USBRX); |
taurin | 0:fafac0045ad3 | 7 | DigitalOut Receiver(p5); |
taurin | 0:fafac0045ad3 | 8 | PwmOut servo1(p21); |
taurin | 0:fafac0045ad3 | 9 | PwmOut servo2(p22); |
taurin | 0:fafac0045ad3 | 10 | |
taurin | 0:fafac0045ad3 | 11 | int counter = 0; |
taurin | 0:fafac0045ad3 | 12 | int servo_deg1 = 0; |
taurin | 0:fafac0045ad3 | 13 | int servo_deg2 = 0; |
taurin | 0:fafac0045ad3 | 14 | |
taurin | 0:fafac0045ad3 | 15 | void rs485_rx() |
taurin | 0:fafac0045ad3 | 16 | { |
taurin | 0:fafac0045ad3 | 17 | int rec_data = rs485.getc(); |
taurin | 0:fafac0045ad3 | 18 | switch(rec_data){ |
taurin | 0:fafac0045ad3 | 19 | case 'A': |
taurin | 0:fafac0045ad3 | 20 | servo_deg1 = rs485.getc(); |
taurin | 0:fafac0045ad3 | 21 | pc.printf("counter1:%d\n\r",servo_deg1); |
taurin | 0:fafac0045ad3 | 22 | break; |
taurin | 0:fafac0045ad3 | 23 | case 'B': |
taurin | 0:fafac0045ad3 | 24 | servo_deg2 = rs485.getc(); |
taurin | 0:fafac0045ad3 | 25 | pc.printf("counter2:%d\n\r",servo_deg2); |
taurin | 0:fafac0045ad3 | 26 | break; |
taurin | 0:fafac0045ad3 | 27 | case 'C': |
taurin | 0:fafac0045ad3 | 28 | Receiver = 1; |
taurin | 0:fafac0045ad3 | 29 | wait_ms(1); |
taurin | 0:fafac0045ad3 | 30 | rs485.putc(counter++); |
taurin | 0:fafac0045ad3 | 31 | wait_ms(1); |
taurin | 0:fafac0045ad3 | 32 | Receiver = 0; |
taurin | 0:fafac0045ad3 | 33 | break; |
taurin | 0:fafac0045ad3 | 34 | default: |
taurin | 0:fafac0045ad3 | 35 | wait_us(5); |
taurin | 0:fafac0045ad3 | 36 | pc.printf("%d\n\r",rec_data); |
taurin | 0:fafac0045ad3 | 37 | } |
taurin | 0:fafac0045ad3 | 38 | } |
taurin | 0:fafac0045ad3 | 39 | |
taurin | 0:fafac0045ad3 | 40 | int main() |
taurin | 0:fafac0045ad3 | 41 | { |
taurin | 0:fafac0045ad3 | 42 | Receiver = 0; |
taurin | 0:fafac0045ad3 | 43 | pc.printf("Receiver\n\r"); |
taurin | 0:fafac0045ad3 | 44 | rs485.baud(38400); |
taurin | 0:fafac0045ad3 | 45 | servo1.period_ms(20); |
taurin | 0:fafac0045ad3 | 46 | servo2.period_ms(20); |
taurin | 0:fafac0045ad3 | 47 | rs485.attach(rs485_rx, Serial::RxIrq); |
taurin | 0:fafac0045ad3 | 48 | while(1){ |
taurin | 0:fafac0045ad3 | 49 | servo1.pulsewidth(0.00093 + (servo_deg1 / 180.0) * (0.00235 - 0.00077)); |
taurin | 0:fafac0045ad3 | 50 | servo2.pulsewidth(0.00093 + (servo_deg2 / 180.0) * (0.00235 - 0.00077)); |
taurin | 0:fafac0045ad3 | 51 | } |
taurin | 0:fafac0045ad3 | 52 | } |