Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
121 pfowler 1
 
2
/* 
3
 433Mhz Reviever Connections
4
     View from front
5
  __________________________
6
 |                          |
7
 |_A_G_G_V__________V_D_D_G_|
8
   | | | |          | | | |
9
                    ^ ^   ^
10
                   5V D2  Gnd 
11
 
12
 MotorShield Connections
13
 
14
    M1___G__M2_
15
   |o|o||o||o|o|
16
    ^ ^     ^ ^
17
    | |     | |
18
   +R -R   -L +L
19
 R = Right motor, L = Left motor
20
*/
21
 
22
#include <VirtualWire.h>
23
#include <AFMotor.h>
24
 
25
AF_DCMotor motor1(1, MOTOR12_2KHZ);  //Right
26
AF_DCMotor motor2(2, MOTOR12_2KHZ);  //Left
27
 
28
const int rheIntPin = 0;
29
const int ledPin = 13;
30
 
31
const int  rxPin    = 2;
32
const int  rxBps    = 2000;
33
 
34
const int  MRIGHT   = 0;
35
const int  MLEFT    = 1;
36
 
37
const int  MFORWARD = 1;
38
const int  MREVERSE = 0;
39
 
40
int options = 0;         // Options
41
int mspd[2] = {0, 0};    //Speed of right track
42
 
43
 
44
volatile byte rpmcount = 0;
45
unsigned int rpm = 0;
46
unsigned long timeold = 0;
47
 
48
void setup()
49
{  
50
 
51
    Serial.begin(9600);	// Debugging only
52
    Serial.println("setup");
53
 
54
    pinMode(ledPin, OUTPUT);
55
    digitalWrite(ledPin, false);
56
 
57
    // Startup radio receiver
58
    vw_set_rx_pin(rxPin);
59
    vw_setup(rxBps);	 // Bits per sec
60
    vw_rx_start();       // Start the receiver PLL running
61
 
62
    // Hall effect intterupt setup
63
    //pinMode(rhePin, INPUT);
64
    attachInterrupt(rheIntPin, rpm_rhe, FALLING);
65
 
66
   // Hall effect debugging
67
   //mspd[MRIGHT] = 50;   
68
 
69
   update_motors();   
70
}
71
 
72
void loop()
73
{
74
    uint8_t buf[VW_MAX_MESSAGE_LEN];
75
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
76
 
77
    if (vw_get_message(buf, &buflen)) // Non-blocking
78
    {
79
        // Flash on msg get
80
        //digitalWrite(13, true);
81
        //digitalWrite(13, false);
82
	/*
83
        int i;
84
        Serial.print("Got: ");
85
	for (i = 0; i < buflen; i++)
86
	{
87
	    Serial.print(buf[i], HEX);
88
	    Serial.print(" ");
89
	}
90
	Serial.println("");
91
        */
92
 
93
 
94
 
95
        if (buf[0])      // Read the options
96
          options = buf[0] - 1;
97
 
98
       if (buf[1])      // Right speed
99
          mspd[MRIGHT] = buf[1] -1;     
100
 
101
       if (buf[2])      // Left speed
102
          mspd[MLEFT] = buf[2] -1;
103
 
104
 
105
       Serial.print("options "); Serial.print(options, BIN);
106
       Serial.print(" r_spd "); Serial.print(mspd[MRIGHT], DEC);
107
       Serial.print(" l_spd "); Serial.print(mspd[MLEFT], DEC);
108
       Serial.println(""); 
109
 
110
       update_motors();       
111
    }
112
 
113
    if (rpmcount >= 5) {
114
      rpm = 30*1000/(millis() - timeold)*rpmcount;
115
      timeold = millis();
116
      rpmcount = 0;
117
      Serial.println(rpm,DEC);
118
    }
119
}
120
 
121
void rpm_rhe() {
122
  rpmcount++;
123
  digitalWrite(13, true);
124
  digitalWrite(13, false);
125
}
126
 
127
void update_motors() {       
128
       // Start the tracks rolling
129
       if (mspd[MRIGHT] > 0) {
130
         motor1.setSpeed(mspd[MRIGHT]);
131
         if (bitRead(options, MRIGHT))
132
           motor1.run(FORWARD);
133
         else
134
           motor1.run(BACKWARD);
135
       } else
136
         motor1.run(RELEASE);
137
 
138
       if (mspd[MLEFT] > 0) {
139
         motor2.setSpeed(mspd[MLEFT]);
140
         if (bitRead(options, MLEFT))
141
           motor2.run(FORWARD);
142
         else
143
           motor2.run(BACKWARD);
144
       } else
145
         motor2.run(RELEASE);
146
}