FM-test
Dependencies: MODSERIAL mbed-rtos mbed
Fork of Master by
Revision 3:12e1f116ea42, committed 2014-09-12
- Comitter:
- 9uS7
- Date:
- Fri Sep 12 05:11:39 2014 +0000
- Parent:
- 2:c610e1a7fbcd
- Child:
- 4:aaaadb45cbd9
- Commit message:
- hey
Changed in this revision
--- a/bluetooth.cpp Thu Sep 11 15:09:05 2014 +0000 +++ b/bluetooth.cpp Fri Sep 12 05:11:39 2014 +0000 @@ -2,7 +2,12 @@ #include "bluetooth.h" #include "control.h" +//master Serial bt(p13, p14); // tx, rx +//slave +//Serial bt(p28, p27); + +DigitalOut l1(LED1); void btSetup(int role) { @@ -16,18 +21,131 @@ } else{ //if this device is the slave - ; //nothing to do + bt.attach( slaveRecieve, Serial::RxIrq ); + } } -/* -void btLoop(int role) + +void sync(char option, char* b_data, float* f_data) { - if( role==BT_MASATER ){ - //if this device is the master - + char pac[PACK_SIZE]; + Cvt temp; + + //making pac + pac[0] = option; + if( option==SYNC_MOTOR ){ + //PACK: [option/function/pwm*4] + //function + pac[1]=b_data[0]; + //pwm + temp.fl = f_data[0]; + for( int i=0 ; i<4 ; i++ ){ + pac[2+i] = temp.byte[i]; + } + } + else if( option==SYNC_FM ){ + //PACK: [option/request] + pac[1]=b_data[0]; //request } else{ - //if this device is the slave - + ; + } + + //send pac + for( int i=0 ; i<PACK_SIZE ; i++ ){ + pc.putc( pac[i] ); } } -*/ \ No newline at end of file + +void slaveRecieve(void) +{ + static int i; + char buf[PACK_SIZE]; + char pac[PACK_SIZE]; + float val[PACK_SIZE/4+1]; + Cvt temp; + + wait(1/1000.0); + + l1=1; + wait(0.2); + l1=2; + + for( int i=0 ; i<PACK_SIZE ; i++ ){ + buf[i]=bt.getc(); + } + + if( buf[0]==SYNC_MOTOR ){ + //PACK: [option/function/pwm*4] + //pwm + for( int i=0 ; i<4 ; i++ ){ + temp.byte[i]=buf[1+i]; + } + motor( buf[1], temp.fl ); + } + else if( buf[0]==SYNC_FM ){ + //PACK: [option/request] + ; //not yet + } + else if( buf[0]==SYNC_SENSOR ){ + getSensor( &(val[0]), &(val[1]) ); + //PACK: [option/ir*4/fsr*4]; + //option + pac[0] = SYNC_SENSOR; + //ir + temp.fl = val[0]; + temp.fl = ++i%2; + for( int i=0 ; i<4 ; i++ ){ + pac[1+i] = temp.byte[i]; + } + //fsr + temp.fl = val[1]; + for( int i=0 ; i<4 ; i++ ){ + pac[5+i] = temp.byte[i]; + } + + //send pac + for( int i=0 ; i<PACK_SIZE ; i++ ){ + bt.putc( pac[i] ); + } + } + else if( buf[0]==SYNC_FM ){ + ; //not yet + + //send pac + for( int i=0 ; i<PACK_SIZE ; i++ ){ + bt.putc( pac[i] ); + } + } +} + +void receiveSensor(float* _ir, float* _fsr) +{ + char buf[PACK_SIZE]; + Cvt temp; + + wait(1/1000.0); + + //Read + for( int i=0 ; i<PACK_SIZE ; i++ ){ + buf[i]=bt.getc(); + } + + //PACK: [option/ir*4/fsr*4]; + //option + if( buf[0]!=SYNC_SENSOR ){ + return; + } + + //ir + for( int i=0 ; i<4 ; i++ ){ + temp.byte[i]=buf[1+i]; + } + *_ir = temp.fl; + + //fsr + for( int i=0 ; i<4 ; i++ ){ + temp.byte[i] = buf[5+i]; + } + *_fsr = temp.fl; + +} \ No newline at end of file
--- a/bluetooth.h Thu Sep 11 15:09:05 2014 +0000 +++ b/bluetooth.h Fri Sep 12 05:11:39 2014 +0000 @@ -6,14 +6,28 @@ #define BT_MASTER 0 #define BT_SLAVE -1 +#define SYNC_SENSOR 0x10 +#define SYNC_MIC 0x11 +#define SYNC_MOTOR 0x20 +#define SYNC_FM 0x21 + + + +#define PACK_SIZE 20 + typedef union _cvt{ - char byte[8]; - int in[2]; - float fl[2]; + char byte[4]; + int in; + float fl; } Cvt; void btSetup(int); //setup(role) role:BT_MASTER or BT_SLAVE +void sync(char, char*, float*); //SYN(option,char_data,float_data); only for MASTER + +void slaveRecieve( void ); +void receiveSensor(float*, float*); + //void btLoop(int); //btLoop(role) role:BT_MASTER or BT_SLAVE #endif \ No newline at end of file
--- a/control.cpp Thu Sep 11 15:09:05 2014 +0000 +++ b/control.cpp Fri Sep 12 05:11:39 2014 +0000 @@ -14,6 +14,23 @@ resetPos(); } +void motor( char function , float power ){ + switch(function){ + case MOTOR_PULL : + pull( power ); + break; + case MOTOR_LOOSE : + loose( power ); + break; + case MOTOR_OPEN: + open(); + break; + case MOTOR_BRAKE: + brake(); + break; + } +} + void pull(float buf){ Sig1 = 0; Sig2 = 1; @@ -53,4 +70,10 @@ } if(pos_flag) break; } +} + + +void getSensor(float* _ir,float* _fsr){ + *_ir = IR; + *_fsr = FSR; } \ No newline at end of file
--- a/control.h Thu Sep 11 15:09:05 2014 +0000 +++ b/control.h Fri Sep 12 05:11:39 2014 +0000 @@ -8,6 +8,11 @@ #define late_time 0.3 #define zeroPWM 0.17 +#define MOTOR_PULL 0x30 +#define MOTOR_LOOSE 0x31 +#define MOTOR_OPEN 0x32 +#define MOTOR_BRAKE 0x33 + void motorSetup(void); void pull(float); //pull motor(power) power:0~1 @@ -15,6 +20,10 @@ void brake(void); //brake motor() void open(void); //open motor() +void motor( char, float ); //motor(function,power) function:MOTOR_XX power:0~1 + void resetPos(void); +void getSensor(float*,float*); //getSensor(&ir,&fsr); + #endif \ No newline at end of file
--- a/main.cpp Thu Sep 11 15:09:05 2014 +0000 +++ b/main.cpp Fri Sep 12 05:11:39 2014 +0000 @@ -18,21 +18,21 @@ DigitalOut myled1(LED1); DigitalOut myled2(LED2); -//buf:for bluetooth -Cvt buf; - -void btRead(void); +void masterLoop(void); int main() { //FM_FREQUENCY is defined in fm.h unsigned int fm_frequency = DEVICE_ROLE==BT_MASTER ? FM_FREQUENCY1 : FM_FREQUENCY2; - fmSetup( fm_frequency ); + //fmSetup( fm_frequency ); btSetup(DEVICE_ROLE); - motorSetup(); + //motorSetup(); while(1){ + if( DEVICE_ROLE==BT_MASTER ){ + masterLoop(); + } /*i2c.start(); i2c.write(0x11); i2c.write(0x42); @@ -43,4 +43,11 @@ } } - +void masterLoop(){ + float ir_m,fsr_m; //sensor of master + float ir_s,fsr_s; //sensor of slave + sync(SYNC_SENSOR,NULL,NULL); + receiveSensor( &ir_s, &fsr_s ); + led1 = ( ir_s>0.5 ? 1 : 0 ); + wait(0.5); +} \ No newline at end of file