The Program derives motor bots getting instructions(speed, turn, stop,etc) from accelerometer.

Dependencies:   Motordriver mbed

Committer:
samgang
Date:
Sun Oct 27 16:42:08 2013 +0000
Revision:
0:c5c22fd71be8
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samgang 0:c5c22fd71be8 1 /* This program uses Christopher Hasler motor library to derive DC motors */
samgang 0:c5c22fd71be8 2
samgang 0:c5c22fd71be8 3 #include "mbed.h"
samgang 0:c5c22fd71be8 4 #include "motordriver.h"
samgang 0:c5c22fd71be8 5
samgang 0:c5c22fd71be8 6 // Array of Motor type objects to define 4 motors for two bots.
samgang 0:c5c22fd71be8 7 Motor M[]= {Motor(p25, p9, p8, 1), Motor(p26, p11, p10, 1), Motor(p21, p13, p12, 1), Motor(p22, p15, p14, 1)};
samgang 0:c5c22fd71be8 8 // Array acc defines 4 analogin type objects to detect inputs of accelerometer.
samgang 0:c5c22fd71be8 9 AnalogIn acc[]= {p16, p17, p19, p18};
samgang 0:c5c22fd71be8 10 /*@update reads recent values from accelerometer and uses them to control motors
samgang 0:c5c22fd71be8 11 @param 'a' tell for which bot the vales should be updated( a=0 for 1st bot and a=2 for 2nd bot)
samgang 0:c5c22fd71be8 12 */
samgang 0:c5c22fd71be8 13 int update(int a)
samgang 0:c5c22fd71be8 14 {
samgang 0:c5c22fd71be8 15 float offx=0, offy=0, map=0, turnp=0, turnn=0;
samgang 0:c5c22fd71be8 16 /*output votage of accelerometer at x and y outs when x=0 and y=0;
samgang 0:c5c22fd71be8 17 turnp and turnn specifies the positive and negative voltage difference(CurrentVoltage - offset) detected from offset value when accelerometer is tilted.
samgang 0:c5c22fd71be8 18 I will be using turnp and turnn as the minimum required voltage difference(ie, the minimum required tilt) to turn the bot.
samgang 0:c5c22fd71be8 19 The value below are default ones for my accelerometer
samgang 0:c5c22fd71be8 20 */
samgang 0:c5c22fd71be8 21 if(a==0) {
samgang 0:c5c22fd71be8 22 offx=0.340;
samgang 0:c5c22fd71be8 23 offy=0.340;
samgang 0:c5c22fd71be8 24 turnp=0.032;
samgang 0:c5c22fd71be8 25 turnn=0.055;
samgang 0:c5c22fd71be8 26 map=14.68;
samgang 0:c5c22fd71be8 27 } else if(a==2) {
samgang 0:c5c22fd71be8 28 offx=0.350;
samgang 0:c5c22fd71be8 29 offy=0.440;
samgang 0:c5c22fd71be8 30 turnp=0.040;
samgang 0:c5c22fd71be8 31 turnn=0.045;
samgang 0:c5c22fd71be8 32 map=13.40;
samgang 0:c5c22fd71be8 33 } else
samgang 0:c5c22fd71be8 34 return(-1); // return -1 as error when update is called with unexpected argument value
samgang 0:c5c22fd71be8 35 float s=0, st=0;
samgang 0:c5c22fd71be8 36 s=acc[a+1].read();
samgang 0:c5c22fd71be8 37 s=(s-offy)*map; // multiply by 'map' to linearly map difference (s-offy) to euivalent scale on 0 to 1 so that s can be used as speed
samgang 0:c5c22fd71be8 38 // (s=0 for (s-offy)=0 and s=1 for max[(s-offy)] )
samgang 0:c5c22fd71be8 39 st=acc[a].read();
samgang 0:c5c22fd71be8 40 st=st-offx;
samgang 0:c5c22fd71be8 41 if(st>0 && abs(st)>turnp) { // checking whether to turn or not ( st>0: right turn and st<0: left trun)
samgang 0:c5c22fd71be8 42 M[a].speed(0);
samgang 0:c5c22fd71be8 43 M[a+1].speed(1);
samgang 0:c5c22fd71be8 44 } else if(st<0 && abs(st)>turnn) {
samgang 0:c5c22fd71be8 45 M[a].speed(1);
samgang 0:c5c22fd71be8 46 M[a+1].speed(0);
samgang 0:c5c22fd71be8 47 } else { // if no turn input then run both motors with speed s
samgang 0:c5c22fd71be8 48 M[a].speed(s);
samgang 0:c5c22fd71be8 49 M[a+1].speed(s);
samgang 0:c5c22fd71be8 50 }
samgang 0:c5c22fd71be8 51 return 1;
samgang 0:c5c22fd71be8 52 }
samgang 0:c5c22fd71be8 53
samgang 0:c5c22fd71be8 54 int main()
samgang 0:c5c22fd71be8 55 {
samgang 0:c5c22fd71be8 56 while(1) {
samgang 0:c5c22fd71be8 57 update(0); // Calling update for first bot(a=0) and second bot(a=2)
samgang 0:c5c22fd71be8 58 update(2);
samgang 0:c5c22fd71be8 59 wait(0.2);
samgang 0:c5c22fd71be8 60 }
samgang 0:c5c22fd71be8 61 }