Pages: [1]   Go Down
Author Topic: Problem with reading sensor  (Read 1756 times)
0 Members and 1 Guest are viewing this topic.
Poland/Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
Finally I've made my OBD2 logger. It's recording data, but I don't know why only first value is acctual.
Code:
PID_RPM: 1479;PID_SPEED: 0;PID_THROTTLE: 18;PID_ENGINE_LOAD: 41;PID_ABS_ENGINE_LOAD: 0;PID_FUEL_LEVEL: 0;PID_DISTANCE: 0
PID_RPM: 1476;PID_SPEED: 0;PID_THROTTLE: 18;PID_ENGINE_LOAD: 41;PID_ABS_ENGINE_LOAD: 0;PID_FUEL_LEVEL: 0;PID_DISTANCE: 0

Rest of pid's value seems to be frozen at moment when I start logging.

Can you help me ivestigate this, why it doesn't record all pid's?
Here is my code:
Code:
#include <SdFat.h>
#include <arduino.h>
#include <OBD.h>

const uint8_t chipSelect = SS;
unsigned long lastTick = millis();

SdFat sd;
COBD obd;

  int rpm;
  int speed_;
  int throttle;
  int eng_load;
  int abs_eng_load;
  int fuel;
  int dist;

void setup() {
  Serial.begin(OBD_SERIAL_BAUDRATE);
  sd.begin(chipSelect, SPI_FULL_SPEED);
  obd.Init();
}
//------------------------------------------------------------------------------
void loop() {
 
  unsigned long curTick = millis();
  if (curTick - lastTick > 500)
        {
    ofstream sdlog("LOGFILE.TXT", ios::out | ios::app);
          obd.ReadSensor(PID_RPM, rpm);
          obd.ReadSensor(PID_SPEED, predk);
          obd.ReadSensor(PID_THROTTLE, przepustnica);
          obd.ReadSensor(PID_ENGINE_LOAD, eng_load);
          obd.ReadSensor(PID_ABS_ENGINE_LOAD, abs_eng_load);
          obd.ReadSensor(PID_FUEL_LEVEL, fuel);
          obd.ReadSensor(PID_DISTANCE, dist);
         
          sdlog << "PID_RPM: " << rpm << ";PID_SPEED: " << predk << ";PID_THROTTLE: " << przepustnica << ";PID_ENGINE_LOAD: " << eng_load << ";PID_ABS_ENGINE_LOAD: " << abs_eng_load << ";PID_FUEL_LEVEL: " << fuel << ";PID_DISTANCE: " << dist << endl;
         // if (!sdlog) sd.errorHalt("append failed");
  lastTick = curTick;
}
}
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14051
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I would first try to read the data and display them on an LCD or so. Much easier to debug.

DO you have a link to the ODB library?

does this code actually run? as predk and przepustnica are not defined?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Poland/Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I've got OBD2 library from www.arduinodev.com/hardware/obd-kit/
It's display data correctly, I've try it earlier, and it works out of the box. Yes I know that this two variables are not defined, but it doesn't change anything.
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14051
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found a diff with the rpm_led demo - it's probably the initialization

Code:
const uint8_t chipSelect = SS;
unsigned long lastTick = millis();

SdFat sd;
COBD obd;

  int rpm;
  int speed_;
  int throttle;
  int eng_load;
  int abs_eng_load;
  int fuel;
  int dist;

void setup() {
  Serial.begin(OBD_SERIAL_BAUDRATE);
  sd.begin(chipSelect, SPI_FULL_SPEED);
  while (!obd.Init());   // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< change this line this is from the example program
}
//------------------------------------------------------------------------------
void loop() {
  
  unsigned long curTick = millis();
  if (curTick - lastTick > 500)
        {
    ofstream sdlog("LOGFILE.TXT", ios::out | ios::app);
          obd.ReadSensor(PID_RPM, rpm);
          obd.ReadSensor(PID_SPEED, predk);
          obd.ReadSensor(PID_THROTTLE, przepustnica);
          obd.ReadSensor(PID_ENGINE_LOAD, eng_load);
          obd.ReadSensor(PID_ABS_ENGINE_LOAD, abs_eng_load);
          obd.ReadSensor(PID_FUEL_LEVEL, fuel);
          obd.ReadSensor(PID_DISTANCE, dist);
        
          sdlog << "PID_RPM: " << rpm << ";PID_SPEED: " << predk << ";PID_THROTTLE: " << przepustnica << ";PID_ENGINE_LOAD: " << eng_load << ";PID_ABS_ENGINE_LOAD: " << abs_eng_load << ";PID_FUEL_LEVEL: " << fuel << ";PID_DISTANCE: " << dist << endl;
         // if (!sdlog) sd.errorHalt("append failed");
 lastTick = curTick;
}
}
« Last Edit: December 22, 2012, 03:00:37 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Poland/Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Truthly  I doubt that this change is necessary. As I understand this line it's try initiate obd until it finish success, and this is executed only once on start. So when I received this one value, this means obd was initialized successfully.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes I know that this two variables are not defined, but it doesn't change anything.

Well, it does change something because the fact that the sketch doesn't compile means it can't possibly demonstrate the problem. You need to post your actual sketch.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Poland/Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <SdFat.h>
#include <arduino.h>
#include <OBD.h>

const uint8_t chipSelect = SS;
unsigned long lastTick = millis();

SdFat sd;
COBD obd;

  int rpm;
  int speed_;
  int throttle;
  int fuel;
  int dist;
 
void setup() {
  Serial.begin(OBD_SERIAL_BAUDRATE);
  sd.begin(chipSelect, SPI_FULL_SPEED);
  obd.Init();

}
//------------------------------------------------------------------------------
void loop() {
 
  unsigned long curTick = millis();
  if (curTick - lastTick > 500)
        {
    ofstream sdlog("LOGFILE.TXT", ios::out | ios::app);

          obd.ReadSensor(PID_RPM, rpm);
          obd.ReadSensor(PID_SPEED, speed_);
          obd.ReadSensor(PID_THROTTLE, throttle);
          obd.ReadSensor(PID_FUEL_LEVEL, fuel);
          obd.ReadSensor(PID_DISTANCE, dist);
         
          sdlog << "PID_RPM: " << rpm << ";PID_SPEED: " << speed_ << ";PID_THROTTLE: " << throttle << ";PID_FUEL_LEVEL: " << fuel << ";PID_DISTANCE: " << dist << endl;
         // if (!sdlog) sd.errorHalt("append failed");
  lastTick = curTick;
}

}

This my actual sketch and compilation ends without any errors. The difference comparing to previous version is that I removed two pids.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which Arduino are you running this code on? For a 328-based Arduino, the SD library takes up more than 1/4 of the memory, because it actually writes to the card in 512 byte chunks.

The streaming library also use a fair amount of memory holding the data to be streamed.

My suspicion is that you are running out of memory.
Logged

Poland/Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it's arduino uno r3. How can I investigate do I suffer for lack of memory?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Google the site for FreeMemory(). Start getting rid of libraries you don't really need. Start with the Streaming one. The SD card doesn't care whether the data gets there because of one function call or 12 function calls.
Logged

Poland/Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok I found some function which counts how many free memory I have. And the result was 638 bytes of free memory. I'm still a beginner in arduino and it hard to say for me is it big or small.

Which library is those streaming?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Which library is those streaming?
This is supported because of the Streaming library. You do not explicitly include the Streaming library, so either SdFat or OBD is, although that should not be possible.

Code:
          sdlog << "PID_RPM: " << rpm << ";PID_SPEED: " << speed_ << ";PID_THROTTLE: " << throttle << ";PID_FUEL_LEVEL: " << fuel << ";PID_DISTANCE: " << dist << endl;

638 bytes should be plenty, but, of course, that depends on where you measured that value.

I'd try Serial.print()ing the values, to make sure that they are indeed correct. No sense chasing a bug in the wrong place.
Logged

Poland/Wroclaw
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I think I found root of this problem. OBD2 library which I use somehow doesn't allow me to read more than one PID. When I want to read two or more it reads only the first one. Now the tricky part. Does anyone of you have any idea how to solve this issue.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The library intro at link you provided implies that the library can read multiple sensors, although their example there  doesn't show it. Look at the dashboard_1602 example though and you see that multiple PIDs are being read, corresponding to which mode the device is in.

I suggest you try writing a minimalist program that reads two pids and displays the result, on the serial port for preference, and build up from there.

edit: grammar
« Last Edit: December 26, 2012, 07:29:02 am by wildbill » Logged

Pages: [1]   Go Up
Jump to: