Sanity check please regarding serial/USB output

Topics and questions about AMC, h-bridges, JRK's etc.

Sanity check please regarding serial/USB output

Postby KMud » Mon 31. Dec 2012, 12:38

I am making an instrument cluster, as many have done before me. It's a learning exercise, so I haven't taken the easiest route...and although I've got the later stuff worked out, I'm stuck at a fairly fundamental bit :lol:

I am struggling with the X-Sim USO serial/USB output - I have it talking to an Arduino using the COM port so things should be simple. As far as I know there is nothing wrong with my code; using HTerm or the Arduino COM port listener I can send data and receive sane data back telling me I've parsed the 2-byte value correctly. For debugging I have the LED on the Arduino flashing once for every 1k RPM, and this behaves with data injected manually. When I send the data from X-Sim (Math Setup just 1:1) I get 36 blinks of the LED, so the supposed RPM value is something between 36,000 and 37,000 (should be ~2,200). Can anyone spot what I've done wrong? :oops:



Code: Select all
const int LED = 13;

void setup() {
  Serial.begin(115200);
  pinMode(LED,OUTPUT);
}

void loop() {
char dataID;

while (Serial.available() > 0)  {
      dataID = Serial.read();
      if (dataID == 'R') f_RPM();
  }
}


void f_RPM() {
  delay(1);
  unsigned char d1 = Serial.read();  // high byte
  delay(1);
  unsigned char d2 = Serial.read();  // low byte
  unsigned int rpm = word(d1,d2);
 
  Serial.print(rpm);
  Serial.write(rpm);
 
  for (int i = 0; i < (rpm/1000); i++) {
    digitalWrite(LED, HIGH);
    delay(300);
    digitalWrite(LED, LOW);
    delay(300);
  }
}


Thanks in advance, and to those whose code I've pored over.
KMud
 
Posts: 5
Joined: Wed 26. Dec 2012, 22:45
Has thanked: 0 time
Been thanked: 0 time

Re: Sanity check please regarding serial/USB output

Postby KMud » Mon 31. Dec 2012, 15:28

My LCD shield has just arrived which makes things easier. The outputted value when the RPM should be 2268 is 36483. Bringing the revs up to 10699 outputs 50297, so I'm not even linear! 0 rpm is 32767...that number looks familiar, 2^15 -1 :|

Here's my current code:

Code: Select all
const int LED = 13;

#include <LiquidCrystal.h>
/* DIO pin definitions */
#define LCD_DATA4 4         /* LCD data DIO pin 4 */
#define LCD_DATA5 5         /* LCD data DIO pin 5 */
#define LCD_DATA6 6         /* LCD data DIO pin 6 */
#define LCD_DATA7 7         /* LCD data DIO pin 7 */
#define LCD_RESET 8         /* LCD Reset DIO pin */
#define LCD_ENABLE 9        /* LCD Enable DIO pin */
#define LCD_BACKLIGHT 10    /* LCD backlight DIO pin */
/* Initialise the LiquidCrystal library with the correct DIO pins */
LiquidCrystal lcd(LCD_RESET, LCD_ENABLE, LCD_DATA4, LCD_DATA5, LCD_DATA6, LCD_DATA7);
void setup()
{
  /* Set the correct display size (16 character, 2 line display) */
  lcd.begin(16, 2);
 
  Serial.begin(115200);
 
  pinMode(LED,OUTPUT);
}
/* Main program loop */
void loop() {
  char dataID;

  while (Serial.available() > 0)  {    //if 6 something is in the Serial buffer...
    dataID = Serial.read();
 
    if (dataID == 'R') f_RPM();
  }
}


void f_RPM() {
  delay(1);
  unsigned char d1 = Serial.read();  // high byte
  delay(1);
  unsigned char d2 = Serial.read();  // low byte
  unsigned int rpm = word(d1,d2);
 
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(rpm);
}
KMud
 
Posts: 5
Joined: Wed 26. Dec 2012, 22:45
Has thanked: 0 time
Been thanked: 0 time

Re: Sanity check please regarding serial/USB output

Postby sirnoname » Mon 31. Dec 2012, 16:01

You do outputs all 30 seconds?
If a answer is correct or did help you for a solution, please use the solve button.
User avatar
sirnoname
Site Admin
 
Posts: 1829
Images: 45
Joined: Thu 1. Sep 2011, 22:02
Location: Munich, Germany
Has thanked: 35 times
Been thanked: 128 times

Re: Sanity check please regarding serial/USB output

Postby KMud » Mon 31. Dec 2012, 16:12

That was so I could count the blinking LED to estimate the interpreted value of RPM. Since I now have a LCD shield things have gotten easier and the refresh rate is currently at 30 ms ;)

edit to add: USO outputs unsigned integer values in 8 bit, 16 bit, or 32 bit resolution, correct? It certainly looks that way from the comments on the radio buttons. What I don't understand is why things are correct when I manually inject binary, but why the USO output gives crazy numbers. Wrong endian? Really signed?

edit2: OK, so fiddling with the range on the Math Setup changes my value of RPM sent to the LCD. However, from the code in this thread (which I've obviously borrowed from) it would seem that the true (and not mapped) values are expected to be sent. I'll keep playing - sorry, struggling with the English in the X-Sim GUIs somewhat!
KMud
 
Posts: 5
Joined: Wed 26. Dec 2012, 22:45
Has thanked: 0 time
Been thanked: 0 time

Re: Sanity check please regarding serial/USB output

Postby KMud » Mon 31. Dec 2012, 17:16

OK, got it working. I think. 8-)
KMud
 
Posts: 5
Joined: Wed 26. Dec 2012, 22:45
Has thanked: 0 time
Been thanked: 0 time

Re: Sanity check please regarding serial/USB output

Postby Cabeza » Thu 27. Jun 2013, 20:04

Hi Kmud, i have the same trouble of you, if you have the solution i would be grateful...thank you so much and sorry for my bad english...
Cabeza
 
Posts: 2
Joined: Fri 8. Feb 2013, 06:13
Has thanked: 0 time
Been thanked: 0 time

Re: Sanity check please regarding serial/USB output

Postby KMud » Thu 27. Jun 2013, 21:12

Cabeza wrote:Hi Kmud, i have the same trouble of you, if you have the solution i would be grateful...thank you so much and sorry for my bad english...


It was a while back (been too busy to play) - but opening x-sim and looking in Math Setup, for RPM and speed I have set #1 as Output 1:1, and #2 as Calibration offset -2147483648. Gear I have set #1 as Output 1:1, and #2 as Calibration offset 0. IIRC the software is not very intuitive for gauges, it makes more sense when creating a range of motion for a force feedback device.
KMud
 
Posts: 5
Joined: Wed 26. Dec 2012, 22:45
Has thanked: 0 time
Been thanked: 0 time


Return to Controllers and Drivers

Who is online

Users browsing this forum: No registered users and 2 guests