all publish

Dependencies:   mbed

Committer:
daikinect
Date:
Mon May 06 00:23:03 2019 +0000
Revision:
0:a9dfabcb2d9a
all publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daikinect 0:a9dfabcb2d9a 1 #include "mbed.h"
daikinect 0:a9dfabcb2d9a 2 //2017/07/07-send
daikinect 0:a9dfabcb2d9a 3
daikinect 0:a9dfabcb2d9a 4 #define Js_X_Margin 0.2
daikinect 0:a9dfabcb2d9a 5 #define Js_Y_Margin 0.2
daikinect 0:a9dfabcb2d9a 6 #define Size_Of_Data 5
daikinect 0:a9dfabcb2d9a 7 #define Plus_F 'A'
daikinect 0:a9dfabcb2d9a 8 #define Minus_F 'X'
daikinect 0:a9dfabcb2d9a 9 double Js_X_Center;
daikinect 0:a9dfabcb2d9a 10 double Js_Y_Center;
daikinect 0:a9dfabcb2d9a 11
daikinect 0:a9dfabcb2d9a 12 int i = 0;
daikinect 0:a9dfabcb2d9a 13 int j;
daikinect 0:a9dfabcb2d9a 14 int xbee_send_data[Size_Of_Data];
daikinect 0:a9dfabcb2d9a 15 double Js_X,Js_Y;
daikinect 0:a9dfabcb2d9a 16 char Speed_X,Speed_Y,Direction_Of_Rotation_X,Direction_Of_Rotation_Y;
daikinect 0:a9dfabcb2d9a 17 //////////////////////////
daikinect 0:a9dfabcb2d9a 18 Serial Xbee(A0,A1);
daikinect 0:a9dfabcb2d9a 19 DigitalOut led(D12);
daikinect 0:a9dfabcb2d9a 20 AnalogIn analog_value_X(A3);
daikinect 0:a9dfabcb2d9a 21 AnalogIn analog_value_Y(A4);
daikinect 0:a9dfabcb2d9a 22 DigitalOut Vcc(A5);
daikinect 0:a9dfabcb2d9a 23 DigitalOut Gnd(A2);
daikinect 0:a9dfabcb2d9a 24 DigitalOut analog_setup_led(D13);
daikinect 0:a9dfabcb2d9a 25 //////////////////////////
daikinect 0:a9dfabcb2d9a 26
daikinect 0:a9dfabcb2d9a 27 void analog_setup()
daikinect 0:a9dfabcb2d9a 28 {
daikinect 0:a9dfabcb2d9a 29 Vcc=1;
daikinect 0:a9dfabcb2d9a 30 Gnd=0;
daikinect 0:a9dfabcb2d9a 31 for(int i=0; i<100; i++) {
daikinect 0:a9dfabcb2d9a 32 analog_setup_led=!analog_setup_led;
daikinect 0:a9dfabcb2d9a 33 Js_X_Center+=analog_value_X.read();
daikinect 0:a9dfabcb2d9a 34 Js_Y_Center+=analog_value_Y.read();
daikinect 0:a9dfabcb2d9a 35 wait_ms(10);
daikinect 0:a9dfabcb2d9a 36 }
daikinect 0:a9dfabcb2d9a 37 Js_X_Center=Js_X_Center/100;
daikinect 0:a9dfabcb2d9a 38 Js_Y_Center=Js_Y_Center/100;
daikinect 0:a9dfabcb2d9a 39 }
daikinect 0:a9dfabcb2d9a 40
daikinect 0:a9dfabcb2d9a 41 void get_data()
daikinect 0:a9dfabcb2d9a 42 {
daikinect 0:a9dfabcb2d9a 43 xbee_send_data[Size_Of_Data-1]=false;
daikinect 0:a9dfabcb2d9a 44 Js_X=analog_value_X.read()-Js_X_Center;
daikinect 0:a9dfabcb2d9a 45 Js_Y=analog_value_Y.read()-Js_Y_Center;
daikinect 0:a9dfabcb2d9a 46 if (Js_X > Js_X_Margin)xbee_send_data[0]=Plus_F; //Direction_Of_Rotation_X
daikinect 0:a9dfabcb2d9a 47 else if(Js_X <-Js_X_Margin)xbee_send_data[0]=Minus_F;//Direction_Of_Rotation_X
daikinect 0:a9dfabcb2d9a 48 else xbee_send_data[0]=0; //Direction_Of_Rotation_X
daikinect 0:a9dfabcb2d9a 49 if (Js_Y > Js_Y_Margin)xbee_send_data[1]=Plus_F; //Direction_Of_Rotation_Y
daikinect 0:a9dfabcb2d9a 50 else if(Js_Y <-Js_Y_Margin)xbee_send_data[1]=Minus_F;//Direction_Of_Rotation_Y
daikinect 0:a9dfabcb2d9a 51 else xbee_send_data[1]=0; //Direction_Of_Rotation_Y
daikinect 0:a9dfabcb2d9a 52 xbee_send_data[2]=char(abs(Js_X)*255);//Speed_X
daikinect 0:a9dfabcb2d9a 53 xbee_send_data[3]=char(abs(Js_Y)*255);//Speed_Y
daikinect 0:a9dfabcb2d9a 54 xbee_send_data[Size_Of_Data-1]=true;
daikinect 0:a9dfabcb2d9a 55 led=!led;
daikinect 0:a9dfabcb2d9a 56 }
daikinect 0:a9dfabcb2d9a 57 int main()
daikinect 0:a9dfabcb2d9a 58 {
daikinect 0:a9dfabcb2d9a 59 analog_setup();
daikinect 0:a9dfabcb2d9a 60 Xbee.baud(9600);
daikinect 0:a9dfabcb2d9a 61 xbee_send_data[Size_Of_Data-1]=true;
daikinect 0:a9dfabcb2d9a 62 Xbee.printf("%c",'s');
daikinect 0:a9dfabcb2d9a 63 while(1) {
daikinect 0:a9dfabcb2d9a 64 if(xbee_send_data[Size_Of_Data-1]) {
daikinect 0:a9dfabcb2d9a 65 led=1;
daikinect 0:a9dfabcb2d9a 66 j=Xbee.getc();
daikinect 0:a9dfabcb2d9a 67 if(j<Size_Of_Data)Xbee.printf("%c",xbee_send_data[j]);
daikinect 0:a9dfabcb2d9a 68 if(j==0)get_data();
daikinect 0:a9dfabcb2d9a 69
daikinect 0:a9dfabcb2d9a 70 }
daikinect 0:a9dfabcb2d9a 71 }
daikinect 0:a9dfabcb2d9a 72 }