Hi,
I'm using (Py)CmdMessenger on OS X with Python 3 in conjunction with a Teensy 3.6 to control the intensity of a few LEDs. I do this by sending a series of 2x 7 integers as binaries to the Teensy who then turns on the output to the pins accordingly using analogWrite() with 12-bit precision.
This works fine except under two specific circumstances, namely when the at least 6 of the 7 binaries are 44 or 59 (out of 4095). I know this might sound bizarre but when six of the numbers are either 44 or 59, the command somehow gets lost. It is 100% repeatable.
When I hard code the values (in my Teensy sketch) to be 44 or 59, I can turn on the LEDs just fine, so I suspect it has to do with the serial communication.
Any suggestions at all?
Teensy sketch:
Python code:
I'm using (Py)CmdMessenger on OS X with Python 3 in conjunction with a Teensy 3.6 to control the intensity of a few LEDs. I do this by sending a series of 2x 7 integers as binaries to the Teensy who then turns on the output to the pins accordingly using analogWrite() with 12-bit precision.
This works fine except under two specific circumstances, namely when the at least 6 of the 7 binaries are 44 or 59 (out of 4095). I know this might sound bizarre but when six of the numbers are either 44 or 59, the command somehow gets lost. It is 100% repeatable.
When I hard code the values (in my Teensy sketch) to be 44 or 59, I can turn on the LEDs just fine, so I suspect it has to do with the serial communication.
Any suggestions at all?
Teensy sketch:
Code:
#include "CmdMessenger.h"
/* Define available CmdMessenger commands */
enum {
get_valsS,
get_valsC,
};
// Here, we'll different pins for the 2x 7 LEDs
// Define the first set
int C01 = 36;
int C02 = 37;
int C03 = 35;
int C04 = 10;
int C05 = 9;
int C06 = 6;
int C07 = 17;
// Define the second set
int S01 = 2;
int S02 = 7;
int S03 = 8;
int S04 = 14;
int S05 = 20;
int S06 = 21;
int S07 = 22;
// Status LED
const int X00 = 13;
/* Initialize CmdMessenger -- this should match PyCmdMessenger instance */
const int BAUD_RATE = 115200;
CmdMessenger cmdMessenger = CmdMessenger(Serial,',',';','/');
void setup() {
// put your setup code here, to run once:
analogWriteResolution(12);// analogWrite value 0 to 4095 for high
// Set all pins to output
pinMode(C01, OUTPUT);
pinMode(C02, OUTPUT);
pinMode(C03, OUTPUT);
pinMode(C04, OUTPUT);
pinMode(C05, OUTPUT);
pinMode(C06, OUTPUT);
pinMode(C07, OUTPUT);
pinMode(S01, OUTPUT);
pinMode(S02, OUTPUT);
pinMode(S03, OUTPUT);
pinMode(S04, OUTPUT);
pinMode(S05, OUTPUT);
pinMode(S06, OUTPUT);
pinMode(S07, OUTPUT);
pinMode(X00, OUTPUT);
int intensity0 = 4095;
for (int i=0; i <= 38; i++){
analogWrite(i, intensity0);
}
Serial.begin(BAUD_RATE);
attach_callbacks();
}
/* callback */
void on_get_valsC(void){
/* Grab seven integers
int valueC01 = cmdMessenger.readBinArg<int>();
int valueC02 = cmdMessenger.readBinArg<int>();
int valueC03 = cmdMessenger.readBinArg<int>();
int valueC04 = cmdMessenger.readBinArg<int>();
int valueC05 = cmdMessenger.readBinArg<int>();
int valueC06 = cmdMessenger.readBinArg<int>();
int valueC07 = cmdMessenger.readBinArg<int>();
analogWrite(C01, invv(valueC01));
analogWrite(C02, invv(valueC02));
analogWrite(C03, invv(valueC03));
analogWrite(C04, invv(valueC04));
analogWrite(C05, invv(valueC05));
analogWrite(C06, invv(valueC06));
analogWrite(C07, invv(valueC07));
}
/* callback */
void on_get_valsS(void){
/* Grab seven integers
int valueS01 = cmdMessenger.readBinArg<int>();
int valueS02 = cmdMessenger.readBinArg<int>();
int valueS03 = cmdMessenger.readBinArg<int>();
int valueS04 = cmdMessenger.readBinArg<int>();
int valueS05 = cmdMessenger.readBinArg<int>();
int valueS06 = cmdMessenger.readBinArg<int>();
int valueS07 = cmdMessenger.readBinArg<int>();
analogWrite(S01, invv(valueS01));
analogWrite(S02, invv(valueS02));
analogWrite(S03, invv(valueS03));
analogWrite(S04, invv(valueS04));
analogWrite(S05, invv(valueS05));
analogWrite(S06, invv(valueS06));
analogWrite(S07, invv(valueS07));
}
void loop() {
cmdMessenger.feedinSerialData();
}
/* Attach callbacks for CmdMessenger commands */
void attach_callbacks(void) {
cmdMessenger.attach(get_valsC, on_get_valsC);
cmdMessenger.attach(get_valsS, on_get_valsS);
}
int invv(int input) {
return 4095-input;
}
Python code:
Code:
# ------------------------------------------------------------------------------
# Python program using the library to interface with the arduino sketch above.
# ------------------------------------------------------------------------------
import PyCmdMessenger
import time
import csv
import sys
import random
# Initialize an ArduinoBoard instance. This is where you specify baud rate and
# serial timeout. If you are using a non ATmega328 board, you might also need
# to set the data sizes (bytes for integers, longs, floats, and doubles).
arduino = PyCmdMessenger.ArduinoBoard("/dev/cu.usbmodem3071981",baud_rate=115200,int_bytes=8, long_bytes=8, float_bytes=8, double_bytes=8)
# List of commands and their associated argument formats. These must be in the
# same order as in the sketch.
commands = [["get_valsS","iiiiiii"],["get_valsC","iiiiiii"]]
# Initialize the messenger
c = PyCmdMessenger.CmdMessenger(arduino,commands)
c.send("get_valsS", 44, 44, 44, 44, 44, 44, 1)
c.send("get_valsC", 100, 100, 100, 100, 100, 100, 100)