1/23 操舵 レシーバ

Dependencies:   ADXL345_I2C INA226_ver1 mbed

Fork of RS485R_2 by albatross

Committer:
taurin
Date:
Sat Dec 05 03:19:30 2015 +0000
Revision:
1:563b7d0b896f
Parent:
0:fafac0045ad3
Child:
2:2b98a651b0a1
????????????????????????;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taurin 0:fafac0045ad3 1 //相互通信確認用
taurin 0:fafac0045ad3 2 #include "mbed.h"
taurin 1:563b7d0b896f 3 #include "ADXL345_I2C.h"
taurin 0:fafac0045ad3 4 #define BUFFER 30
taurin 0:fafac0045ad3 5
taurin 1:563b7d0b896f 6 Serial rs485(p13,p14);
taurin 0:fafac0045ad3 7 Serial pc(USBTX,USBRX);
taurin 0:fafac0045ad3 8 DigitalOut Receiver(p5);
taurin 1:563b7d0b896f 9 ADXL345_I2C accelerometer(p9, p10);
taurin 0:fafac0045ad3 10 PwmOut servo1(p21);
taurin 0:fafac0045ad3 11 PwmOut servo2(p22);
taurin 0:fafac0045ad3 12
taurin 0:fafac0045ad3 13 int counter = 0;
taurin 0:fafac0045ad3 14 int servo_deg1 = 0;
taurin 0:fafac0045ad3 15 int servo_deg2 = 0;
taurin 0:fafac0045ad3 16
taurin 1:563b7d0b896f 17 int acc[3] = {0,0,0};
taurin 1:563b7d0b896f 18
taurin 0:fafac0045ad3 19 void rs485_rx()
taurin 0:fafac0045ad3 20 {
taurin 0:fafac0045ad3 21 int rec_data = rs485.getc();
taurin 1:563b7d0b896f 22 switch(rec_data) {
taurin 1:563b7d0b896f 23 case 'A':
taurin 1:563b7d0b896f 24 servo_deg1 = rs485.getc();
taurin 1:563b7d0b896f 25 pc.printf("counter1:%d\n\r",servo_deg1);
taurin 1:563b7d0b896f 26 break;
taurin 1:563b7d0b896f 27 case 'B':
taurin 1:563b7d0b896f 28 servo_deg2 = rs485.getc();
taurin 1:563b7d0b896f 29 pc.printf("counter2:%d\n\r",servo_deg2);
taurin 1:563b7d0b896f 30 break;
taurin 1:563b7d0b896f 31 case 'C':
taurin 1:563b7d0b896f 32 Receiver = 1;
taurin 1:563b7d0b896f 33 wait_ms(1);
taurin 1:563b7d0b896f 34 rs485.putc('X');
taurin 1:563b7d0b896f 35 rs485.putc(acc[0]);
taurin 1:563b7d0b896f 36 rs485.putc('Y');
taurin 1:563b7d0b896f 37 rs485.putc(acc[1]);
taurin 1:563b7d0b896f 38 rs485.putc('Z');
taurin 1:563b7d0b896f 39 rs485.putc(acc[2]);
taurin 1:563b7d0b896f 40 wait_ms(2);
taurin 1:563b7d0b896f 41 Receiver = 0;
taurin 1:563b7d0b896f 42 break;
taurin 1:563b7d0b896f 43 default:
taurin 1:563b7d0b896f 44 wait_us(5);
taurin 1:563b7d0b896f 45 pc.printf("%d\n\r",rec_data);
taurin 0:fafac0045ad3 46 }
taurin 0:fafac0045ad3 47 }
taurin 0:fafac0045ad3 48
taurin 1:563b7d0b896f 49 void init()
taurin 1:563b7d0b896f 50 {
taurin 1:563b7d0b896f 51 Receiver = 0;
taurin 1:563b7d0b896f 52 pc.printf("Receiver\n\r");
taurin 1:563b7d0b896f 53
taurin 1:563b7d0b896f 54 rs485.baud(38400);
taurin 1:563b7d0b896f 55 rs485.attach(rs485_rx, Serial::RxIrq);
taurin 1:563b7d0b896f 56
taurin 1:563b7d0b896f 57 servo1.period_ms(20);
taurin 1:563b7d0b896f 58 servo2.period_ms(20);
taurin 1:563b7d0b896f 59
taurin 1:563b7d0b896f 60 accelerometer.setPowerControl(0x00);
taurin 1:563b7d0b896f 61 accelerometer.setDataFormatControl(0x0B);
taurin 1:563b7d0b896f 62 accelerometer.setDataRate(ADXL345_3200HZ);
taurin 1:563b7d0b896f 63 accelerometer.setPowerControl(0x08);
taurin 1:563b7d0b896f 64 }
taurin 1:563b7d0b896f 65
taurin 0:fafac0045ad3 66 int main()
taurin 0:fafac0045ad3 67 {
taurin 1:563b7d0b896f 68 init();
taurin 1:563b7d0b896f 69 while(1) {
taurin 1:563b7d0b896f 70 accelerometer.getOutput(acc);
taurin 0:fafac0045ad3 71 servo1.pulsewidth(0.00093 + (servo_deg1 / 180.0) * (0.00235 - 0.00077));
taurin 0:fafac0045ad3 72 servo2.pulsewidth(0.00093 + (servo_deg2 / 180.0) * (0.00235 - 0.00077));
taurin 1:563b7d0b896f 73 wait_ms(100);
taurin 0:fafac0045ad3 74 }
taurin 0:fafac0045ad3 75 }