I just started working on this and have a 'ways to go' ~gotta sleep at some point, though. I've decided to drop the idea of a smooth gait until more important things are taken care of first. I haven't gone all the way through it or compiled it to see where the disjunctions are yet. For the time being this will only be for one leg with 2 servos, it will gradually expand from there. I will edit this post as I work on it and ask for advice accordingly - unless anyone is able to see any obvious, needed restructuring.
/*
'zoomkat 11-22-12 simple delimited ',' string parse' is combined with 'PROGMEM string demo'
inside of a while loop that gets started with a bumper sensor used as a manual switch.
Moves one leg prototyped for a hexapod ~to be further expanded...
********************************
*Testing this gave me alphabet characters in the serial monitor instead
of the indexed numbers I defined (degree values for servos), this tells me
I must be close. A simple change of datatypes only gives disassembler messages
I don't understand(strcpy_P). Some help with this would be great.*
********************************
zoomkat 11-22-12 simple delimited ',' string parse
from serial port input (via serial monitor)
and print result out serial port
multi servos added
PROGMEM string demo
-How to store a table of strings in program memory (flash), and retrieve them.
Information summarized from:
http://www.nongnu.org/avr-libc/user-manual/pgmspace.html
-Here is a good template to follow for setting up a
table (array) of strings in program memory.
-Setting up the strings is a two-step process. */
//define strings
#include <avr/pgmspace.h>
prog_char string_0[] PROGMEM = {'60a, 90b'}; //positions leg at ready(may have no use)
prog_char string_1[] PROGMEM = {'25a, 170b'}; //gait for one leg (2 motors(a,b))
prog_char string_2[] PROGMEM = {'160a'}; //
prog_char string_3[] PROGMEM = {'5b'}; //
prog_char string_4[] PROGMEM = {'25a'}; //
//prog_uint16_t string_5[] PROGMEM = ;
//prog_uint16_t string_6[] PROGMEM = ;
//prog_uint16_t string_7[] PROGMEM = ;
//prog_uint16_t string_8[] PROGMEM = ;
//prog_uint16_t string_9[] PROGMEM = ;
// Then set up a table to refer to strings.
PROGMEM const char *string_table[] = //change "string_table" name to suit
{
string_0,
string_1,
string_2,
string_3,
string_4,
// string_5
// string_6,
// string_7,
// string_8,
// string_9,
};
char buffer[32]; //needs to be large enough for the largest string it must hold
//may eventually hold up to 240 characters for all servos
String readString;
//int string[] = {1,2,3,4,5,6,7,8,9};
#include <Servo.h>
Servo myservoa, myservob; // create servo object to control a servo
#include <Wire.h>
const int bumperPinR = 2; // the number of the bumper pin
const int bumperPinL = 4; //
int bumperStateR = 0; // variable for reading the bumper status
int bumperStateL = 0; //
void setup()
{
Serial.begin(9600);
//wrong idea but want to save the values temporarily
//String('60a, 90b'); //positions leg at ready(may have no use)
//String('25a, 170b'); //gait for one leg (2 motors(a,b))
//String('160a'); //
//String('5b'); //
//String('25a'); //
//String('125a, 140b'); //smoothed motion not in use
//String('80a, 150b'); //
//String('45a, 160b'); //
//String('30a, 170b'); //
//myservoa.writeMicroseconds(1500); //set initial servo position if desired?
//myservob.writeMicroseconds(1500); //
myservob.attach(8); //the pin for the servob control
myservoa.attach(9); //
//Serial.println(""); //keep track of what is loaded
pinMode(bumperPinR, INPUT); //sets the bumper pins as an inputs
pinMode(bumperPinL, INPUT); //
Wire.begin(); //initializes/activates voltage sensing for bumperpins?
}
void loop()
{
bumperStateR = digitalRead(bumperPinR); //read state of bumper sensors
//bumperStateL = digitalRead(bumperPinL); //
while (bumperStateR == LOW) //begin a loop(one leg walking) with bumper sensor
{
for (unsigned int i = 1; i < 8; i++) //prints successive strings in table?
{ //'strcpy_P' copies strings from program memory to buffer,
strcpy_P(buffer, (PGM_P)pgm_read_word(&(string_table[i]))); //necessary casts and dereferencing
Serial.println(buffer); //required to gather data from table above.
delay(500); //~line:'strcpy_P' seems to be the problem...~
}
if (Serial.available())
{
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') //where/what is 'char c'?
{
if (readString.length() >1) //greater than 1 char?
{
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, my servos use 0-180 so 181 should work
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
}
readString=""; //clears variable for new input
}
}
else
{
readString += c; //makes the string readString
}
}
}
}