Ohnishi_Gundan / Mbed 2 deprecated Master-FM

Dependencies:   MODSERIAL mbed-rtos mbed

Fork of Master by Ohnishi_Gundan

Committer:
9uS7
Date:
Fri Sep 12 05:11:39 2014 +0000
Revision:
3:12e1f116ea42
Parent:
2:c610e1a7fbcd
Child:
8:bfcfda6b38fe
hey

Who changed what in which revision?

UserRevisionLine numberNew contents of line
9uS7 0:4f07ba929908 1 #include "mbed.h"
9uS7 0:4f07ba929908 2 #include "control.h"
9uS7 0:4f07ba929908 3
9uS7 1:e1cfb5850088 4 //sensor
9uS7 1:e1cfb5850088 5 AnalogIn IR(p17);
9uS7 1:e1cfb5850088 6 AnalogIn FSR(p18);
9uS7 1:e1cfb5850088 7
9uS7 1:e1cfb5850088 8 //motor
9uS7 1:e1cfb5850088 9 DigitalOut Sig1(p21);
9uS7 1:e1cfb5850088 10 DigitalOut Sig2(p23);
9uS7 1:e1cfb5850088 11 PwmOut Pwm(p22);
9uS7 1:e1cfb5850088 12
9uS7 2:c610e1a7fbcd 13 void motorSetup(void){
9uS7 2:c610e1a7fbcd 14 resetPos();
9uS7 2:c610e1a7fbcd 15 }
9uS7 2:c610e1a7fbcd 16
9uS7 3:12e1f116ea42 17 void motor( char function , float power ){
9uS7 3:12e1f116ea42 18 switch(function){
9uS7 3:12e1f116ea42 19 case MOTOR_PULL :
9uS7 3:12e1f116ea42 20 pull( power );
9uS7 3:12e1f116ea42 21 break;
9uS7 3:12e1f116ea42 22 case MOTOR_LOOSE :
9uS7 3:12e1f116ea42 23 loose( power );
9uS7 3:12e1f116ea42 24 break;
9uS7 3:12e1f116ea42 25 case MOTOR_OPEN:
9uS7 3:12e1f116ea42 26 open();
9uS7 3:12e1f116ea42 27 break;
9uS7 3:12e1f116ea42 28 case MOTOR_BRAKE:
9uS7 3:12e1f116ea42 29 brake();
9uS7 3:12e1f116ea42 30 break;
9uS7 3:12e1f116ea42 31 }
9uS7 3:12e1f116ea42 32 }
9uS7 3:12e1f116ea42 33
9uS7 0:4f07ba929908 34 void pull(float buf){
9uS7 0:4f07ba929908 35 Sig1 = 0;
9uS7 0:4f07ba929908 36 Sig2 = 1;
9uS7 0:4f07ba929908 37 Pwm = zeroPWM+buf/3;
9uS7 0:4f07ba929908 38 }
9uS7 0:4f07ba929908 39
9uS7 0:4f07ba929908 40 void loose(float buf){
9uS7 0:4f07ba929908 41 Sig1 = 1;
9uS7 0:4f07ba929908 42 Sig2 = 0;
9uS7 0:4f07ba929908 43 Pwm = zeroPWM+buf/3;
9uS7 0:4f07ba929908 44 }
9uS7 0:4f07ba929908 45
9uS7 0:4f07ba929908 46 void brake(void){
9uS7 0:4f07ba929908 47 Sig1 = 0;
9uS7 0:4f07ba929908 48 Sig2 = 0;
9uS7 0:4f07ba929908 49 Pwm = 1;
9uS7 0:4f07ba929908 50 }
9uS7 0:4f07ba929908 51
9uS7 0:4f07ba929908 52 void open(void){
9uS7 0:4f07ba929908 53 Sig1 = 0;
9uS7 0:4f07ba929908 54 Sig2 = 0;
9uS7 0:4f07ba929908 55 Pwm = 0;
9uS7 0:4f07ba929908 56 }
9uS7 0:4f07ba929908 57
9uS7 2:c610e1a7fbcd 58 void resetPos(void){
9uS7 0:4f07ba929908 59 int pos_flag=0;
9uS7 1:e1cfb5850088 60
9uS7 0:4f07ba929908 61 while(1){
9uS7 1:e1cfb5850088 62 if(IR>center_pos){
9uS7 0:4f07ba929908 63 pull(0.3);
9uS7 1:e1cfb5850088 64 }else if(IR<center_pos){
9uS7 0:4f07ba929908 65 loose(0.3);
9uS7 0:4f07ba929908 66 }
9uS7 1:e1cfb5850088 67 if(abs(IR-center_pos)<0.1){
9uS7 1:e1cfb5850088 68 open();
9uS7 1:e1cfb5850088 69 pos_flag=-1;
9uS7 1:e1cfb5850088 70 }
9uS7 1:e1cfb5850088 71 if(pos_flag) break;
9uS7 0:4f07ba929908 72 }
9uS7 3:12e1f116ea42 73 }
9uS7 3:12e1f116ea42 74
9uS7 3:12e1f116ea42 75
9uS7 3:12e1f116ea42 76 void getSensor(float* _ir,float* _fsr){
9uS7 3:12e1f116ea42 77 *_ir = IR;
9uS7 3:12e1f116ea42 78 *_fsr = FSR;
9uS7 0:4f07ba929908 79 }