SDRecord/SDPlayback
SDRecord/SDPlayback
Using the Adafruit microSD card board the user can record and playback their robots movements
Commands Used
if(conditional);else if(conditional);
while(conditional);
Serial.begin(x);
Serial.print(string);
Serial.println(string);
SD.begin(string);
SD.open(string,x);
SD.read(x);
SD.write(x);
robotMove(x,y);
gripperMove(x);
delay(x);
Setup:
Connect your Adafruit microSD card board (or pretty much any other SD card board) as shown.
Download SDRecord.ino for Arduino
Download SDRecord.ino for Arduino
Cut and paste code for SDRecord:
/*
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10 (for MKRZero SD: SDCARD_SS_PIN)
Code reads robot position and writes it to SD card.
*/
#include <SPI.h>
#include <SD.h>
#include <robot.h>
File dance1;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
robotActivate(); //let the robot know commands are comming
robotMode(manual); //set the robot to manual control
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
/*optional code. Uncomment code below to prompt before removing file
Serial.println("remove dance1.txt?");
while (!Serial.available());
while (Serial.read() != 'y');
SD.remove("dance1.txt");
*/
dance1 = SD.open("dance1.txt", FILE_WRITE); //opens the file and prepares to write
Serial.println("three");
delay(1000);
Serial.println("two");
delay(1000);
Serial.println("one");
delay(1000);
Serial.println("go!!!"); //Warn user that recording is coming
}
void loop() {
long timer1 = millis(); //immportatnt to use LONG here to match type of millis()
while ((millis() - timer1) < 159000) {
if ((millis() - timer1) > (159000 - 1000)) Serial.println("one");
else if ((millis() - timer1) > (159000 - 2000)) Serial.println("two");
else if ((millis() - timer1) > (159000 - 3000)) Serial.println("three");
long timer2 = millis();
//record value o 255 twice.
//This is not likely to occur in the data nad makes a good start marker.
dance1.write(0xFF);
dance1.write(0xFF);
//Record each channel in turn.
dance1.write(robotPosition(1));
dance1.write(robotPosition(2));
dance1.write(robotPosition(3));
dance1.write(gripperPosition());
/*
dance1.write(0xFF);
dance1.write(0xFF);
dance1.write(x++);
Serial.println(millis()-timer1);
*/
while ((millis() - timer2) < 30); //wait until 30 milliseconds have past
}
dance1.close();
Serial.println("done");
while(true); //We're done. Wait here forever.
}
SDPlayback Code
/*
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10 (for MKRZero SD: SDCARD_SS_PIN)
Code reads recording created in SDRecord and plays back on robot
*/
#include <SPI.h>
#include <SD.h>
#include <robot.h>
File dance1;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
delay(1000);
robotActivate();
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
delay(1000);
Serial.println("three");
delay(1000);
Serial.println("two");
delay(1000);
Serial.println("one");
delay(1000);
Serial.println("go!!!");
delay(1000);
Serial.println("moving");
robotMode(ARDUINO);
}
void loop() {
dance1 = SD.open("dance1.txt");
//Serial.println(millis()-timer1);
uint8_t temp1 = 0;
uint8_t temp2 = 0;
while (dance1.available()) {
long timer1 = millis();
temp1 = (dance1.read()); //check for first instance of 0xFF
if (temp1 == 0xFF) temp2 = (dance1.read()); //if first instance found look for second instance
//if two 0xFF values are found proceed to read robot positions
if ((temp1 == 0xFF) && (temp2 == 0xFF)) {
robotMove(1, dance1.read());
robotMove(2, dance1.read());
robotMove(3, dance1.read());
gripperMove(dance1.read());
temp1 = 0;
temp2 = 0;
}
while ((millis() - timer1) < 30);
}
dance1.close();
Serial.println("done");
}