I can't imagine what would be different.
Post the debug output showing the exact characters you get from python, and what strtok_r does with them.
Raspberry Pi, I2c, Python and Adafruit_I2c library with Ardu
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
- impartit
- Posts: 61
- Joined: Tue Feb 04, 2014 12:50 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
As you have already stated "You use C functions to manipulate C strings, such as strcpy, strlen, strtok, etc. Strtok won't work on a String object."
Therefore the difference must be that that what is sent from Arduino to Arduino in your tests is a C String and what is being sent from the Raspberry Pi is a String Object.
If, in debugging the Arduino sketch, I populate an array with contents of I2CinBuffer I can "Serial.print" the two values received but strok does nothing with them.
Therefore the difference must be that that what is sent from Arduino to Arduino in your tests is a C String and what is being sent from the Raspberry Pi is a String Object.
If, in debugging the Arduino sketch, I populate an array with contents of I2CinBuffer I can "Serial.print" the two values received but strok does nothing with them.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
It can't be a String object. python will only send the data in the string. The type of the received data is determined by the I2C driver on the arduino, and that cannot create a String object. It can only create a character array.
The Arduino code appends a null at the end of the array, thus making a C string out of the received character array.
Look at I2CComplete - I2CinBuffer is a character array. I2C stuffs n characters into it, and then appends a 0. That's a C string.
The Arduino code appends a null at the end of the array, thus making a C string out of the received character array.
Look at I2CComplete - I2CinBuffer is a character array. I2C stuffs n characters into it, and then appends a 0. That's a C string.
Code: Select all
void receiveEvent(int howMany) {
int readCount = Wire.readBytes(I2CinBuffer, howMany);
//Serial.println(readCount);
//delay(1500);
I2CinBuffer[readCount] = 0;
I2CComplete = true;
}
- impartit
- Posts: 61
- Joined: Tue Feb 04, 2014 12:50 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
This is the Python code (with author comments) that creates the (character array] and prints it correctly
# controlCommand is a comma separated string from the Webserver that consists of a single character Command, plus a
# Parameter.
# Move forward 200mm would be represented as "f,200\n\r"
print controlCommand
#send command to arduino
motion.write_command(controlCommand)
The motion.write_command is:
def write_command(self, command):
err = self.move.writeList(self.MOTION_DATA, map(ord,command))
return err
and writeList (from the Adafruit_I2C.py library is:
def writeList(self, reg, list):
"Writes an array of bytes using I2C format"
try:
if self.debug:
print "I2C: Writing list to register 0x%02X:" % reg
print list
self.bus.write_i2c_block_data(self.address, reg, list)
except IOError, err:
return self.errMsg()
This results in an array containing the 2 values required to be tokenized.
I remain mistified.
# controlCommand is a comma separated string from the Webserver that consists of a single character Command, plus a
# Parameter.
# Move forward 200mm would be represented as "f,200\n\r"
print controlCommand
#send command to arduino
motion.write_command(controlCommand)
The motion.write_command is:
def write_command(self, command):
err = self.move.writeList(self.MOTION_DATA, map(ord,command))
return err
and writeList (from the Adafruit_I2C.py library is:
def writeList(self, reg, list):
"Writes an array of bytes using I2C format"
try:
if self.debug:
print "I2C: Writing list to register 0x%02X:" % reg
print list
self.bus.write_i2c_block_data(self.address, reg, list)
except IOError, err:
return self.errMsg()
This results in an array containing the 2 values required to be tokenized.
I remain mistified.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
I'd still like to see the full debug output from the Arduino, showing the actual string that is received and whatever substrings strtok_r comes up with.
- impartit
- Posts: 61
- Joined: Tue Feb 04, 2014 12:50 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
The Arduino code does NOT enter the while loop in the ParseI2CData() function:
while ((str = strtok_r(p, ",", &p)) != NULL) // seperate at each "," delimiter
{
inParse[count] = str; // Add chunk to array
count++;
}
so I cannot send you any debug output from the strok function, but outputting the elements of the I2CinBuffer array clearly shows that it contains two values for command and parameter e.g. 6, 100. These were as printed in the python code sent to you yesterday.
while ((str = strtok_r(p, ",", &p)) != NULL) // seperate at each "," delimiter
{
inParse[count] = str; // Add chunk to array
count++;
}
so I cannot send you any debug output from the strok function, but outputting the elements of the I2CinBuffer array clearly shows that it contains two values for command and parameter e.g. 6, 100. These were as printed in the python code sent to you yesterday.
- impartit
- Posts: 61
- Joined: Tue Feb 04, 2014 12:50 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
The Arduino code does NOT enter the while loop in the ParseI2CData() function:
while ((str = strtok_r(p, ",", &p)) != NULL) // seperate at each "," delimiter
{
inParse[count] = str; // Add chunk to array
count++;
}
so I cannot send you any debug output from the strok function, but outputting the elements of the I2CinBuffer array clearly shows that it contains two values for command and parameter e.g. 6, 100. These were as printed in the python code sent to you yesterday.
while ((str = strtok_r(p, ",", &p)) != NULL) // seperate at each "," delimiter
{
inParse[count] = str; // Add chunk to array
count++;
}
so I cannot send you any debug output from the strok function, but outputting the elements of the I2CinBuffer array clearly shows that it contains two values for command and parameter e.g. 6, 100. These were as printed in the python code sent to you yesterday.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
What? receiveEvent sets I2CComplete to true no matter what happens, and ParseI2C data is called whenever I2CComplete is true. So receiveEvent must not be running. Is that the problem? You have no I2C communications going on?impartit wrote:The Arduino code does NOT enter the while loop in the ParseI2CData() function:
Code: Select all
if (I2CComplete) // if I2C commands are available, read it:
{
ParseI2CData(); // Parse the recieved data
inString = ""; // Reset inString to empty
I2CComplete = false; // Reset the system for further input of data
}
- impartit
- Posts: 61
- Joined: Tue Feb 04, 2014 12:50 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
This is the output from the attached Arduino Sketch:
01. 140328
I2C Initiated
########## LET US BEGIN ##########
I2C Complete Function
6
(one space)
,
(one space)
5
3
5
(one space)
(one space)
(one space)
(one space)
(one space)
(one space)
(one space)
0
Command from I2C is ....
Parameter from I2C is
CMD,,,
01. 140328
I2C Initiated
########## LET US BEGIN ##########
I2C Complete Function
6
(one space)
,
(one space)
5
3
5
(one space)
(one space)
(one space)
(one space)
(one space)
(one space)
(one space)
0
Command from I2C is ....
Parameter from I2C is
CMD,,,
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
This is bizarre. The Arduino code definitely works, so what is python sending it that screws it up?
Can you run this and post the output - I changed it to print out the hex values of the received characters:
Can you run this and post the output - I changed it to print out the hex values of the received characters:
Code: Select all
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <Wire.h>
Adafruit_7segment matrix = Adafruit_7segment();
// For the Arduino slave, you need to set up a few defines:
#define SLAVE_ADDRESS 0x33 // I2C slave address
#define REG_MAP_SIZE 32 // Max I2C Buffer size
#define MAX_SENT_BYTES 7 // Command, parameter, heading, batt
#define IDENTIFICATION 0x0D // An identifier for this slave
// Then we include the Wire library.
#include <Wire.h> // Include the Wire library so we can use I2C.
// Set up some variables for communications:
byte lcount;
byte rcount;
byte motion;
byte spd;
String Command = ""; // The parsed Command from the RPi
String Parameter = ""; // The parsed Parameter from the RPi
char inData[REG_MAP_SIZE]; // Raw Buffer for the incoming serial data
char *inParse[REG_MAP_SIZE]; // Buffer for the parsed data chunks
char I2CinBuffer[REG_MAP_SIZE]; // Raw Buffer for the incoming I2C data
byte I2CoutBuffer[REG_MAP_SIZE]; // Buffer for the outgoing I2C data
String inString = ""; // Storage for data as string
int index = 0;
boolean stringComplete = false; // Tells the main loop that it has received
// a command via the Serial port
boolean I2CComplete = false; // Tells the main loop that it has received
// a command via I2C
// Propellor Setup
// PWM is connected to pin 3.
const int pinPwm = 3;
// DIR is connected to pin 2.
const int pinDir = 2;
// Speed of the motor.
static int iSpeed = 0;
// Acceleration of the motor.
static int iAcc = 1;
// In setup() we initialize Serial and Wire:
void setup()
{
matrix.begin(0x73);
// Initialize the PWM and DIR pins as digital outputs.
pinMode(pinPwm, OUTPUT);
pinMode(pinDir, OUTPUT);
Serial.begin(115200);
Serial.println("RST, Running Motors_I2C_01. 140328");
Wire.begin(SLAVE_ADDRESS);
Serial.println("I2C Initiated.");
Wire.onRequest(requestEvent); // Set up Request Interrupt Service Routine
Wire.onReceive(receiveEvent); // Set up Receive Interrupt Service Routine
I2CoutBuffer[0x08] = IDENTIFICATION; // ID register
Serial.println("########## LET US BEGIN #############");
}
// In loop() you need to watch for Serial or I2C commands available:
void loop()
{
SerialEvent(); // Grab characters from Serial/
if (stringComplete) // if there's any serial available, read it:
{
ParseSerialData(); // Parse the recieved data
inString = ""; // Reset inString to empty
stringComplete = false; // Reset the system for further input of data
}
if (I2CComplete) // if I2C commands are available, read it:
{
Serial.println("I2C Complete function");
matrix.print(1010); // print integer
matrix.writeDisplay(); // push data to display
ParseI2CData(); // Parse the recieved data
inString = ""; // Reset inString to empty
I2CComplete = false; // Reset the system for further input of data
}
}
// Then there are Serial Receive and I2C Request and Receive handlers:
void SerialEvent()
{
while (Serial.available() && stringComplete == false) // Read while we have data
{
char inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it in char array
index++; // Increment where to write next
inString += inChar; // Also add it to string storage
if (inChar == '\n' || inChar == '\r') // Check for termination character
{
index = 0;
stringComplete = true;
}
}
//Serial.print(inString);
}
void receiveEvent(int howMany) {
int readCount = Wire.readBytes(I2CinBuffer, howMany);
//Serial.print("Read Count = ");
//Serial.println(readCount);
//delay(1500);
I2CinBuffer[readCount] = 0;
I2CComplete = true;
}
void requestEvent()
{
Wire.write(I2CoutBuffer, 8);
////Serial.print('line 99');
// reset left and right tick counters
//lcount = 0;
//rcount = 0;
}
// storeData is used to copy volatile variables into a register for I2C send
void storeData()
{
// Notice Highbyte/Lowbyte order MSB,LSB
cli();
I2CoutBuffer[0x00] = highByte(motion);
I2CoutBuffer[0x01] = lowByte(motion);
I2CoutBuffer[0x02] = highByte(lcount);
I2CoutBuffer[0x03] = lowByte(lcount);
I2CoutBuffer[0x04] = highByte(rcount);
I2CoutBuffer[0x05] = lowByte(rcount);
I2CoutBuffer[0x06] = highByte(spd);
I2CoutBuffer[0x07] = lowByte(spd);
I2CoutBuffer[0x08] = IDENTIFICATION;
sei();
}
//Once the Command and Parameters are received they have to be parsed into functions to control the Autonomous Rover:
// Take the Comma separated Serial string and format it to Command and Parameter
void ParseSerialData()
{
matrix.clear();
matrix.writeDisplay();
for (int wait = 0; wait < 3; wait++) { // repeat multiple times
for (int i = 1; i < 64; i *= 2) {
matrix.writeDigitRaw(0,i);
matrix.writeDisplay(); // push data to display
delay(100);
}
}
char *p = inData; // The data to be parsed
char *str; // Temp store for each data chunk
int count = 0; // Id ref for each chunk
while ((str = strtok_r(p, ",", &p)) != NULL) // seperate at each "," delimiter
{
inParse[count] = str; // Add chunk to array
count++;
Serial.print("Parsing Serial data Now.........");
}
if(count == 2) // If the data has two values then..
{
Command = inParse[0]; // Define value 1 as a Command identifier
Serial.print("Command from Serial in is.... ");
Serial.println(Command);
Parameter = inParse[1]; // Define value 2 as a Parameter value
Serial.print("Parameter from Serial in is.... ");
Serial.println(Parameter);
processCommand();
}
}
// Take the Comma separated I2C string and format it to Command and Parameter
void ParseI2CData()
{
matrix.clear();
matrix.writeDisplay();
for (int wait = 0; wait < 3; wait++) { // repeat multiple times
for (int i = 1; i < 64; i *= 2) {
matrix.writeDigitRaw(0,i);
matrix.writeDisplay(); // push data to display
delay(100);
}
}
//I2CinBuffer = {"f,175"};
char *p = I2CinBuffer;
//char *p = I2CinBuffer; // The data to be parsed
char *str; // Temp store for each data chunk
int count = 0; // Id ref for each chunk
int i;
for (i = 0; i < 16; i = i + 1) {
Serial.println(I2CinBuffer[i], HEX);
}
while ((str = strtok_r(p,",", &p))!= NULL)
{
//count = sscanf(I2CinBuffer," %c, %d", &Command, &Parameter);
inParse[count] = str; // Add chunk to array
count++;
Serial.print("Parsing I2C data Now.........");
}
Serial.print(I2CinBuffer); Serial.print(" ");Serial.println(count);
if(count = 2) // If the data has two values then..
{
Command = inParse[0]; // Define value 1 as a Command identifier
Serial.print("Command from I2C is.... "); Serial.println(Command);
Parameter = inParse[1]; // Define value 2 as a Parameter value
Serial.print("Parameter from I2C is.... "); Serial.println(Parameter);
processCommand();
}
}
// Determine actions from Command / Parameter -- This can be called from either
// Serial or I2C parser
void processCommand()
{
char buf[REG_MAP_SIZE]; // make this at least big enough for the whole string
Parameter.toCharArray(buf, sizeof(buf)); // Convert String to Character array
Serial.print("CMD,"); Serial.print(Command); Serial.print(",");
Serial.print(Parameter);Serial.print(","); //Serial.println(now);
// remove brakes
//digitalWrite(rmbrkpin,LOW);
//digitalWrite(lmbrkpin,LOW);
// Call the relevant identified Command
if(Command[1]) Command[0]=Command[1];
switch(Command[0])
{
// Move Forward "Parameter" ticks
case 'f':
//matrix.print(0,121);
//matrix.writeDisplay();
matrix.writeDigitRaw(0,128); //print decimal point
matrix.writeDisplay();
//matrix.print(Parameter); // print Parmeter
//matrix.writeDisplay(); // push data to display
analogWrite(pinPwm, 100);
digitalWrite(pinDir, LOW);
break;
case 'b':
matrix.print(4321); //print integer
matrix.writeDisplay(); // push data to display
analogWrite(pinPwm, 100);
digitalWrite(pinDir, HIGH);
break;
case 'r':
//lspeed = spd;
//rspeed = spd;
//ldir = forward;
//rdir = backward;
//motionStop = atoi(buf);
//motion = TURN_RIGHT;
//motion = IN_//motion;
//turning = 1;
break;
case 'l':
//lspeed = spd;
//rspeed = spd;
//ldir = backward;
//rdir = forward;
//motionStop = atoi(buf);
//motion = TURN_LEFT;
//motion = IN_//motion;
//turning = 1;
break;
case 's': // Set Desired Speed
//spd = atoi(buf);
break;
case 'x': // STOP!!!
//lspeed = 0;
//rspeed = 0;
//motionStop = 0;
//motion = STOP;
// Apply brakes
//digitalWrite(rmbrkpin,HIGH);
//digitalWrite(lmbrkpin,HIGH);
// reset left and right tick counters
//lcount = 0;
//rcount = 0;
break;
}
// release brakes
//digitalWrite(rmbrkpin,LOW);
//digitalWrite(lmbrkpin,LOW);
}
- impartit
- Posts: 61
- Joined: Tue Feb 04, 2014 12:50 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
With 6 and 100 inputted
Output is:
36
2C
20
31
30
30
A
D
0
0
0
I really, really hope this helps you.
Output is:
36
2C
20
31
30
30
A
D
0
0
0
I really, really hope this helps you.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
Well, it helps show that there's nothing wrong with the Arduino code. Here is the output with the exact same input via I2C:
As you can see, it's the identical array of data in I2CinBuffer. This data has been sent over I2C from another Arduino (not a Pi, but that's irrelevant since the received data is the same).
If the data is coming from the Pi too rapidly, that might cause this problem, since I2CinBuffer will be overwritten. Run this version and post the *entire* output for several iterations:
Code: Select all
,I2C Complete function
36
2C
20
31
30
30
A
D
0
0
0
0
0
0
0
0
Parsing I2C data Now.........Parsing I2C data Now.........6 2
Command from I2C is.... 6
Parameter from I2C is.... 100
CMD,6, 100
If the data is coming from the Pi too rapidly, that might cause this problem, since I2CinBuffer will be overwritten. Run this version and post the *entire* output for several iterations:
Code: Select all
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <Wire.h>
Adafruit_7segment matrix = Adafruit_7segment();
// For the Arduino slave, you need to set up a few defines:
#define SLAVE_ADDRESS 0x33 // I2C slave address
#define REG_MAP_SIZE 32 // Max I2C Buffer size
#define MAX_SENT_BYTES 7 // Command, parameter, heading, batt
#define IDENTIFICATION 0x0D // An identifier for this slave
// Then we include the Wire library.
#include <Wire.h> // Include the Wire library so we can use I2C.
// Set up some variables for communications:
byte lcount;
byte rcount;
byte motion;
byte spd;
String Command = ""; // The parsed Command from the RPi
String Parameter = ""; // The parsed Parameter from the RPi
char inData[REG_MAP_SIZE]; // Raw Buffer for the incoming serial data
char *inParse[REG_MAP_SIZE]; // Buffer for the parsed data chunks
char I2CinBuffer[REG_MAP_SIZE]; // Raw Buffer for the incoming I2C data
byte I2CoutBuffer[REG_MAP_SIZE]; // Buffer for the outgoing I2C data
String inString = ""; // Storage for data as string
int index = 0;
boolean stringComplete = false; // Tells the main loop that it has received
// a command via the Serial port
boolean I2CComplete = false; // Tells the main loop that it has received
// a command via I2C
// Propellor Setup
// PWM is connected to pin 3.
const int pinPwm = 3;
// DIR is connected to pin 2.
const int pinDir = 2;
// Speed of the motor.
static int iSpeed = 0;
// Acceleration of the motor.
static int iAcc = 1;
// In setup() we initialize Serial and Wire:
void setup()
{
matrix.begin(0x73);
// Initialize the PWM and DIR pins as digital outputs.
pinMode(pinPwm, OUTPUT);
pinMode(pinDir, OUTPUT);
Serial.begin(115200);
Serial.println("RST, Running Motors_I2C_01. 140328");
Wire.begin(SLAVE_ADDRESS);
Serial.println("I2C Initiated.");
Wire.onRequest(requestEvent); // Set up Request Interrupt Service Routine
Wire.onReceive(receiveEvent); // Set up Receive Interrupt Service Routine
I2CoutBuffer[0x08] = IDENTIFICATION; // ID register
Serial.println("########## LET US BEGIN #############");
}
// In loop() you need to watch for Serial or I2C commands available:
void loop()
{
SerialEvent(); // Grab characters from Serial/
if (stringComplete) // if there's any serial available, read it:
{
ParseSerialData(); // Parse the recieved data
inString = ""; // Reset inString to empty
stringComplete = false; // Reset the system for further input of data
}
if (I2CComplete) // if I2C commands are available, read it:
{
Serial.println("I2C Complete function");
matrix.print(1010); // print integer
matrix.writeDisplay(); // push data to display
ParseI2CData(); // Parse the recieved data
inString = ""; // Reset inString to empty
I2CComplete = false; // Reset the system for further input of data
}
}
// Then there are Serial Receive and I2C Request and Receive handlers:
void SerialEvent()
{
while (Serial.available() && stringComplete == false) // Read while we have data
{
char inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it in char array
index++; // Increment where to write next
inString += inChar; // Also add it to string storage
if (inChar == '\n' || inChar == '\r') // Check for termination character
{
index = 0;
stringComplete = true;
}
}
//Serial.print(inString);
}
void receiveEvent(int howMany) {
int readCount = Wire.readBytes(I2CinBuffer, howMany);
Serial.print("Read Count = ");
Serial.println(readCount);
//delay(1500);
I2CinBuffer[readCount] = 0;
I2CComplete = true;
}
void requestEvent()
{
Wire.write(I2CoutBuffer, 8);
////Serial.print('line 99');
// reset left and right tick counters
//lcount = 0;
//rcount = 0;
}
// storeData is used to copy volatile variables into a register for I2C send
void storeData()
{
// Notice Highbyte/Lowbyte order MSB,LSB
cli();
I2CoutBuffer[0x00] = highByte(motion);
I2CoutBuffer[0x01] = lowByte(motion);
I2CoutBuffer[0x02] = highByte(lcount);
I2CoutBuffer[0x03] = lowByte(lcount);
I2CoutBuffer[0x04] = highByte(rcount);
I2CoutBuffer[0x05] = lowByte(rcount);
I2CoutBuffer[0x06] = highByte(spd);
I2CoutBuffer[0x07] = lowByte(spd);
I2CoutBuffer[0x08] = IDENTIFICATION;
sei();
}
//Once the Command and Parameters are received they have to be parsed into functions to control the Autonomous Rover:
// Take the Comma separated Serial string and format it to Command and Parameter
void ParseSerialData()
{
matrix.clear();
matrix.writeDisplay();
for (int wait = 0; wait < 3; wait++) { // repeat multiple times
for (int i = 1; i < 64; i *= 2) {
matrix.writeDigitRaw(0,i);
matrix.writeDisplay(); // push data to display
delay(100);
}
}
char *p = inData; // The data to be parsed
char *str; // Temp store for each data chunk
int count = 0; // Id ref for each chunk
while ((str = strtok_r(p, ",", &p)) != NULL) // seperate at each "," delimiter
{
inParse[count] = str; // Add chunk to array
count++;
Serial.print("Parsing Serial data Now.........");
}
if(count == 2) // If the data has two values then..
{
Command = inParse[0]; // Define value 1 as a Command identifier
Serial.print("Command from Serial in is.... ");
Serial.println(Command);
Parameter = inParse[1]; // Define value 2 as a Parameter value
Serial.print("Parameter from Serial in is.... ");
Serial.println(Parameter);
processCommand();
}
}
// Take the Comma separated I2C string and format it to Command and Parameter
void ParseI2CData()
{
matrix.clear();
matrix.writeDisplay();
for (int wait = 0; wait < 3; wait++) { // repeat multiple times
for (int i = 1; i < 64; i *= 2) {
matrix.writeDigitRaw(0,i);
matrix.writeDisplay(); // push data to display
delay(100);
}
}
//I2CinBuffer = {"f,175"};
char *p = I2CinBuffer;
//char *p = I2CinBuffer; // The data to be parsed
char *str; // Temp store for each data chunk
int count = 0; // Id ref for each chunk
int i;
for (i = 0; i < 16; i = i + 1) {
Serial.println(I2CinBuffer[i], HEX);
}
while ((str = strtok_r(p,",", &p))!= NULL)
{
//count = sscanf(I2CinBuffer," %c, %d", &Command, &Parameter);
inParse[count] = str; // Add chunk to array
count++;
Serial.print("Parsing I2C data Now.........");
}
Serial.print(I2CinBuffer); Serial.print(" ");Serial.println(count);
if(count = 2) // If the data has two values then..
{
Command = inParse[0]; // Define value 1 as a Command identifier
Serial.print("Command from I2C is.... "); Serial.println(Command);
Parameter = inParse[1]; // Define value 2 as a Parameter value
Serial.print("Parameter from I2C is.... "); Serial.println(Parameter);
processCommand();
}
}
// Determine actions from Command / Parameter -- This can be called from either
// Serial or I2C parser
void processCommand()
{
char buf[REG_MAP_SIZE]; // make this at least big enough for the whole string
Parameter.toCharArray(buf, sizeof(buf)); // Convert String to Character array
Serial.print("CMD,"); Serial.print(Command); Serial.print(",");
Serial.print(Parameter);Serial.print(","); //Serial.println(now);
// remove brakes
//digitalWrite(rmbrkpin,LOW);
//digitalWrite(lmbrkpin,LOW);
// Call the relevant identified Command
if(Command[1]) Command[0]=Command[1];
switch(Command[0])
{
// Move Forward "Parameter" ticks
case 'f':
//matrix.print(0,121);
//matrix.writeDisplay();
matrix.writeDigitRaw(0,128); //print decimal point
matrix.writeDisplay();
//matrix.print(Parameter); // print Parmeter
//matrix.writeDisplay(); // push data to display
analogWrite(pinPwm, 100);
digitalWrite(pinDir, LOW);
break;
case 'b':
matrix.print(4321); //print integer
matrix.writeDisplay(); // push data to display
analogWrite(pinPwm, 100);
digitalWrite(pinDir, HIGH);
break;
case 'r':
//lspeed = spd;
//rspeed = spd;
//ldir = forward;
//rdir = backward;
//motionStop = atoi(buf);
//motion = TURN_RIGHT;
//motion = IN_//motion;
//turning = 1;
break;
case 'l':
//lspeed = spd;
//rspeed = spd;
//ldir = backward;
//rdir = forward;
//motionStop = atoi(buf);
//motion = TURN_LEFT;
//motion = IN_//motion;
//turning = 1;
break;
case 's': // Set Desired Speed
//spd = atoi(buf);
break;
case 'x': // STOP!!!
//lspeed = 0;
//rspeed = 0;
//motionStop = 0;
//motion = STOP;
// Apply brakes
//digitalWrite(rmbrkpin,HIGH);
//digitalWrite(lmbrkpin,HIGH);
// reset left and right tick counters
//lcount = 0;
//rcount = 0;
break;
}
// release brakes
//digitalWrite(rmbrkpin,LOW);
//digitalWrite(lmbrkpin,LOW);
}
- impartit
- Posts: 61
- Joined: Tue Feb 04, 2014 12:50 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
Sorry, been away for a couple of days. No change with the code you provided I am afraid. In looking at the script I can detect no real difference additionaly the Arduino receives the data from the I2c bus and populates an array correctly so why it won't tokenize remains a seemingly unsolveable issue. I have checked the I2C communication with a very simple python code and corresponding Arduino sketch where the python send sequential numbers for 1 - 255 to the Uno and the Arduino sends back an acknowledgement. Works perfectly at all baud rates.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
The code was just a test - it didn't try to change anything except the debug output. Can you run it and post the *entire* serial output for several iterations? I have an idea of what might be going on...
- impartit
- Posts: 61
- Joined: Tue Feb 04, 2014 12:50 am
Re: Raspberry Pi, I2c, Python and Adafruit_I2c library with
OK so here is the output from the Arduino Serial Monitor after a few iterations from the sketch that you provided. I wait with baited breath for your response.
Please be positive and constructive with your questions and comments.