Skip to content

Commit 848c85b

Browse files
initial commit
0 parents  commit 848c85b

23 files changed

+1566
-0
lines changed

LICENSE.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SparkFun License Information
2+
=============================
3+
4+
This product is open source!
5+
The hardware is released under [Creative Commons ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/).
6+
The code is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round!
7+
Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license.
8+
9+
10+
Distributed as-is; no warranty is given.
11+
12+
- Your friends at SparkFun.
13+
14+
_Any collaboration credit should appear here._

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<<<<<<< HEAD
2+
SparkFun <PRODUCT NAME>
3+
=======
4+
SparkFun <PRODUCT NAME> Arduino Library
5+
>>>>>>> fd5b4cdbaea461f0a29699b11aef869223593d73
6+
========================================
7+
8+
![SparkFun Part Name](URL for picture of part)
9+
10+
[*SparkFun Part Name (SKU)*](URL for product on Sparkfun.com)
11+
12+
<<<<<<< HEAD
13+
<Basic description of the part.>
14+
=======
15+
<Basic description of the library.>
16+
>>>>>>> fd5b4cdbaea461f0a29699b11aef869223593d73
17+
18+
Repository Contents
19+
-------------------
20+
21+
<<<<<<< HEAD
22+
* **/Enclosure** - Enclosure files
23+
* **/Firmware** - Example code
24+
* **/Hardware** - Eagle design files (.brd, .sch)
25+
* **/Libraries** - Libraries for use with the <PRODUCT NAME>
26+
* **/Production** - Production panel files (.brd)
27+
* **/Software** - Related software for the <PRODUCT NAME>
28+
29+
Documentation
30+
--------------
31+
* **[Library](GitHub library URL)** - <LANGUAGE> library for the <PRODUCT NAME>.
32+
* **[Hookup Guide](Learn.SparkFun URL)** - Basic hookup guide for the <PRODUCT NAME>.
33+
* **[SparkFun Fritzing repo](https://github.com/sparkfun/Fritzing_Parts)** - Fritzing diagrams for SparkFun products.
34+
* **[SparkFun 3D Model repo](https://github.com/sparkfun/3D_Models)** - 3D models of SparkFun products.
35+
=======
36+
* **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE.
37+
* **/extras** - Additional documentation for the user. These files are ignored by the IDE.
38+
* **/src** - Source files for the library (.cpp, .h).
39+
* **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE.
40+
* **library.properties** - General library properties for the Arduino package manager.
41+
42+
Documentation
43+
--------------
44+
45+
* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
46+
* **[Product Repository](GitHub Product URL)** - Main repository (including hardware files) for the <PRODUCT NAME>.
47+
* **[Hookup Guide](Learn.SparkFun URL)** - Basic hookup guide for the <PRODUCT NAME>.
48+
49+
Products that use this Library
50+
---------------------------------
51+
>>>>>>> fd5b4cdbaea461f0a29699b11aef869223593d73
52+
53+
* [Part SKU](SparkFun part URL)- Basic part and short description here
54+
* [Part SKU](SparkFun part URL)- Basic part and short description here
55+
56+
Version History
57+
---------------
58+
<<<<<<< HEAD
59+
=======
60+
61+
>>>>>>> fd5b4cdbaea461f0a29699b11aef869223593d73
62+
* [vExxFxxZxxHxxLxxSxx](URL for tag specific to this version) - Description
63+
* [vEyyFyyZyyHyyLyySyy](URL for tag specific to this version) - Description
64+
65+
License Information
66+
-------------------
67+
<<<<<<< HEAD
68+
This product is _**open source**_!
69+
70+
The **hardware** is released under [Creative Commons ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/).
71+
=======
72+
73+
This product is _**open source**_!
74+
>>>>>>> fd5b4cdbaea461f0a29699b11aef869223593d73
75+
76+
The **code** is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round!
77+
78+
Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license.
79+
80+
Distributed as-is; no warranty is given.
81+
82+
- Your friends at SparkFun.
83+
84+
_<COLLABORATION CREDIT>_
85+
<<<<<<< HEAD
86+
87+
=======
88+
>>>>>>> fd5b4cdbaea461f0a29699b11aef869223593d73
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#include "chartools.h"
2+
#include "Wire.h"
3+
#include "sensorbar.h"
4+
#include <RedBot.h>
5+
6+
//Protos
7+
void updateDriveState( void );
8+
9+
10+
// Uncomment one of the four lines to match your SX1509's address
11+
// pin selects. SX1509 breakout defaults to [0:0] (0x3E).
12+
const uint8_t SX1509_ADDRESS = 0x3E; // SX1509 I2C address (00)
13+
//const byte SX1509_ADDRESS = 0x3F; // SX1509 I2C address (01)
14+
//const byte SX1509_ADDRESS = 0x70; // SX1509 I2C address (10)
15+
//const byte SX1509_ADDRESS = 0x71; // SX1509 I2C address (11)
16+
17+
SensorBar mySensorBar(SX1509_ADDRESS);
18+
19+
CircularStack positionHistory;
20+
RedBotMotors motors; // Instantiate the motor control object. This only needs
21+
// to be done once.
22+
23+
24+
// State machine states
25+
#define IDLE_STATE 0
26+
#define READ_LINE 1
27+
#define HOLD 2
28+
29+
// Drive states
30+
#define DRIVE_STOP 0
31+
#define DRIVE_GO 1
32+
#define DRIVE_GO_HARD 2
33+
#define DRIVE_LEFT 3
34+
#define DRIVE_LEFT_HARD 4
35+
#define DRIVE_RIGHT 5
36+
#define DRIVE_RIGHT_HARD 6
37+
38+
uint8_t state;
39+
uint8_t driveState;
40+
uint8_t targetDriveState;
41+
42+
int8_t centerThreshold = 15;
43+
int8_t wayOutThreshold = 65;
44+
uint16_t holdTime = 3;
45+
46+
uint16_t msTick = 0;
47+
uint16_t holdTicks = 0;
48+
49+
int8_t lastAverage = 0;
50+
51+
52+
void setup()
53+
{
54+
Wire.begin(); // join i2c bus (address optional for master)
55+
Serial.begin(9600); // start serial for output
56+
}
57+
58+
void loop()
59+
{
60+
uint8_t densityOK = 0;
61+
//Wait 1 ms
62+
delay(1);
63+
64+
//Increment the tick
65+
msTick++;
66+
67+
//Do ms related activities
68+
//update drive state
69+
updateDriveState();
70+
int temp = mySensorBar.getDensity();
71+
if( (temp < 4)&&(temp > 0) )
72+
{
73+
positionHistory.pushElement( mySensorBar.getPosition());
74+
}
75+
76+
//If the tick is too high, decrement it and do long scale activities
77+
if( msTick > 20 )
78+
{
79+
Serial.println( state );
80+
msTick -= 20;
81+
uint8_t nextState = state; //default state
82+
switch (state) {
83+
case IDLE_STATE:
84+
targetDriveState = DRIVE_STOP; // Stops both motors
85+
nextState = READ_LINE;
86+
break;
87+
case READ_LINE:
88+
if( (temp < 4)&&(temp > 0) )
89+
{
90+
//Density OK
91+
densityOK = 1;
92+
lastAverage = positionHistory.averageLast( 50 );
93+
}
94+
if( densityOK == 1 )
95+
{
96+
if( (lastAverage < centerThreshold)&&(lastAverage > -centerThreshold) )
97+
{
98+
//In the center-- Go hard!
99+
targetDriveState = DRIVE_GO_HARD;
100+
}
101+
else
102+
{
103+
if(lastAverage > 0 )
104+
{
105+
//in the left side
106+
if( (lastAverage < wayOutThreshold)&&(lastAverage > centerThreshold) )
107+
{
108+
//not that far out left
109+
targetDriveState = DRIVE_LEFT;
110+
}
111+
else
112+
{
113+
//Way out!
114+
targetDriveState = DRIVE_LEFT_HARD;
115+
nextState = HOLD;
116+
}
117+
}
118+
else
119+
{
120+
//in the right side
121+
if( (lastAverage > -wayOutThreshold)&&(lastAverage < -centerThreshold) )
122+
{
123+
//not that far out right
124+
targetDriveState = DRIVE_RIGHT;
125+
}
126+
else
127+
{
128+
//Way out!
129+
targetDriveState = DRIVE_RIGHT_HARD;
130+
nextState = HOLD;
131+
}
132+
}
133+
}
134+
}
135+
case HOLD:
136+
holdTicks++;
137+
if( holdTicks > holdTime )
138+
{
139+
holdTicks = 0;
140+
nextState = READ_LINE;
141+
}
142+
break;
143+
default:
144+
targetDriveState = DRIVE_STOP;
145+
break;
146+
}
147+
state = nextState;
148+
}
149+
150+
}
151+
152+
153+
void updateDriveState( void )
154+
{
155+
if( driveState != targetDriveState )
156+
{
157+
driveState = targetDriveState;
158+
switch (driveState) {
159+
case DRIVE_STOP:
160+
motors.stop();
161+
break;
162+
case DRIVE_GO:
163+
motors.rightMotor(100);
164+
motors.leftMotor(-100);
165+
break;
166+
case DRIVE_GO_HARD:
167+
motors.rightMotor(200);
168+
motors.leftMotor(-200);
169+
break;
170+
case DRIVE_LEFT:
171+
motors.rightMotor(80);
172+
motors.leftMotor(-50);
173+
break;
174+
case DRIVE_LEFT_HARD:
175+
motors.rightMotor(100);
176+
motors.leftMotor(-0);
177+
break;
178+
case DRIVE_RIGHT:
179+
motors.rightMotor(50);
180+
motors.leftMotor(-80);
181+
break;
182+
case DRIVE_RIGHT_HARD:
183+
motors.rightMotor(0);
184+
motors.leftMotor(-100);
185+
break;
186+
default:
187+
motors.stop(); // Stops both motors
188+
break;
189+
}
190+
}
191+
}
192+
193+
194+
195+
196+
197+
198+
199+
200+
201+
202+
203+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "chartools.h"
2+
3+
//This function changes an integer to a string, null terminated
4+
//
5+
// You better not provide a string of length shorter than length + 1....
6+
//
7+
void hexString(char* opString, int input, int length)
8+
{
9+
int i;
10+
for(i = 0; i < length; i++)
11+
{
12+
opString[i] = hex2char((input >> (4 * (length - 1 -i))) & 0xF);
13+
}
14+
opString[length] = 0x00;
15+
16+
return;
17+
}
18+
19+
// This takes a int input and converts to char
20+
//
21+
// The output will be an char if the int
22+
// is between zero and 0xF
23+
//
24+
// Otherwise, the output is $.
25+
//
26+
char hex2char(int hexin)
27+
{
28+
int charout;
29+
charout = 0x24; // default $ state
30+
if(hexin >= 0x00)
31+
{
32+
if(hexin <= 0x09)
33+
{
34+
charout = hexin + 0x30;
35+
}
36+
}
37+
if(hexin >= 0x0A)
38+
{
39+
if(hexin <= 0x0F)
40+
{
41+
charout = hexin -10 + 0x41;
42+
}
43+
}
44+
return charout;
45+
}
46+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void hexString(char* opString, int input, int length);
2+
char hex2char(int hexin);

0 commit comments

Comments
 (0)