Skip to content

Commit

Permalink
brand aren't anymore hardcoded, other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aster94 committed Apr 6, 2020
1 parent 4d5c9e4 commit 243d79a
Show file tree
Hide file tree
Showing 8 changed files with 626 additions and 548 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lib/**
.pio/**
/test/**
**/test/**
include/**
.vscode/**
doxygen/**
Expand Down
42 changes: 15 additions & 27 deletions examples/basic_working/basic_working.ino
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
#include <Arduino.h>
#include "KWP2000.h"

// Different core have different way to use a Serial port
#if defined(ARDUINO_ARCH_ESP32)
HardwareSerial bike(2); // for the ESP32 core
HardwareSerial bike(2);
#define TX_PIN 17
#elif defined(ARDUINO_ARCH_STM32)
HardwareSerial bike(PA3, PA2); // for the stm32duino core
HardwareSerial bike(PA3, PA2); // RX and TX
#define TX_PIN PA2
#else
#define bike Serial3 // for the Arduino avr core
#define bike Serial3
#define TX_PIN 14
#endif

#define debug Serial
#define TX_PIN 14

KWP2000 ECU(&bike, TX_PIN);

uint8_t dealer_status = false;
char in;
uint8_t fake_request[] = {1, 2};
// Replace YOUR_MOTORBIKE with SUZUKI, KAWASAKI, YAMAHA or HONDA
KWP2000 ECU(&bike, TX_PIN, YOUR_MOTORBIKE);

void setup()
{
ECU.enableDebug(&debug, DEBUG_LEVEL_VERBOSE, 115200);
//Serial.begin(); this is not needed because we use then same serial as the debug
//ECU.enableDealerMode(8);

while (!debug)
{
// wait for connection with the serial
}
while (debug.available() > 0)
{
debug.read(); // empty serial buffer
}
//ECU.enableDealerMode(pin); // This is available only on Suzuki bikes
}

void loop()
{
//ECU.printStatus();
if (debug.available() > 0)
{
in = debug.read();
char in = debug.read();
debug.print("User Input: ");
debug.println(in);

Expand All @@ -53,11 +43,10 @@ void loop()
break;

case 'd':
dealer_status = !dealer_status;
ECU.dealerMode(dealer_status);
ECU.setDealerMode(!ECU.getDealerMode());
break;

case 'D':
case 't':
ECU.readTroubleCodes();
break;

Expand All @@ -66,9 +55,8 @@ void loop()
ECU.printSensorsData();
break;

case 'h':
ECU.handleRequest(fake_request, sizeof(fake_request));
ECU.printLastResponse();
case 'p':
ECU.printStatus();
break;

case 'c':
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=KWP2000
version=1.2.1
version=1.3.0
author=aster94
maintainer=aster94
sentence=A library that makes interfacing with motorbikes a breeze.
paragraph=Connect with the ECU of Suzuki, Honda, Kawasaki, Yamaha etc.
paragraph=Connect with the ECU of Suzuki, Kawasaki, Yamaha, Honda.
category=Device Control
url=https://github.com/aster94/Keyword-Protocol-2000
architectures=*
32 changes: 14 additions & 18 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

[platformio]
src_dir = examples/basic_working
;src_dir = examples/test
lib_dir = .
;default_envs = mega
default_envs = mega

; Commmon
[env]
framework = arduino
platform = atmelavr
board = megaatmega2560
monitor_speed = 115200
check_tool = cppcheck
build_flags = -DKAWASAKI
build_flags =-D YOUR_MOTORBIKE=KAWASAKI

; Boards
; Test the library on different platforms
[env:mega]
platform = atmelavr
board = megaatmega2560
Expand All @@ -24,23 +28,15 @@ board = lolin32
platform = ststm32
board = maple_mini_b20

; Motorbikes
; Test the library on each brand
[env:suzuki]
platform = atmelavr
board = megaatmega2560
build_flags = -DSUZUKI
build_flags =-D YOUR_MOTORBIKE=SUZUKI

[env:kawasaki]
platform = atmelavr
board = megaatmega2560
build_flags = -DKAWASAKI

[env:honda]
platform = atmelavr
board = megaatmega2560
build_flags = -DHONDA
build_flags =-D YOUR_MOTORBIKE=KAWASAKI

[env:yamaha]
platform = atmelavr
board = megaatmega2560
build_flags = -DYAMAHA
build_flags =-D YOUR_MOTORBIKE=YAMAHA

[env:honda]
build_flags =-D YOUR_MOTORBIKE=HONDA
157 changes: 157 additions & 0 deletions src/ISO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
ISO.h
Parameter IDs
Copyright (c) Aster94
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef ISO_h
#define ISO_h

#include "KWP2000.h"

// These values are defined by the ISO protocol
#define ISO_BAUDRATE 10400
#define ISO_MAX_DATA 260 ///< maximum length of a response from the ecu: 255 data + 4 header + 1 checksum
#define ISO_T_P1 10 ///< inter byte time for ECU response - min: 0 max: 20
#define ISO_T_P2_MIN_LIMIT 50
#define ISO_T_P2_MAX_LIMIT 89600 ///< P2 time between tester request and ECU response or two ECU responses
#define ISO_T_P3_MAX_LIMIT 89600 ///< P3 time between end of ECU responses and start of new tester request
#define ISO_T_P4_MAX_LIMIT 20 ///< inter byte time for tester request
// P2 (min & max), P3 (min & max) and P4 (min) are defined by the ECU with accessTimingParameter()
#define ISO_T_IDLE 1000 ///< min 300, max undefined
#define ISO_T_INIL (unsigned int)25 ///< Initialization low time
#define ISO_T_WUP (unsigned int)50 ///< Wake up Pattern

// Common values between all brands/bikes

const uint8_t format_physical = 0x80;
const uint8_t format_functional = 0xC0; // not supported
const uint8_t format_CARB = 0x40; // not supported

const uint8_t OUR_addr = 0xF1; // between hex F0 and FD

const uint8_t start_com[] = {0x81};
const uint8_t stop_com[] = {0x82};
uint8_t start_diagnostic[2] = {0x10};
const uint8_t read_data_local_identifier = 0x21;

// access timing parameter and timing paramenter indicator
const uint8_t atp_read_limits[] = {0x83, 0x00};
const uint8_t atp_set_default[] = {0x83, 0x01};
const uint8_t atp_read_current[] = {0x83, 0x02};
const uint8_t atp_set_given[] = {0x83, 0x03};

const uint8_t tester_present_with_answer[] = {0x3E, 0x01}; // the ECU will answer
const uint8_t tester_present_without_answer[] = {0x3E, 0x02}; // the ECU won't answer

const uint8_t trouble_codes_all[] = {0x13};
const uint8_t trouble_codes_only_active[] = {0x17};
const uint8_t trouble_codes_with_status[] = {0x18};
const uint8_t clear_trouble_codes[] = {0x14};

#define request_ok(x) x | 0x40
const uint8_t request_rejected = 0x7F;

// Specific brands/bikes values here are only declared and will be assigned inside the function
// this is very likely to change, do not refert too much to the code below

uint8_t ECU_addr;

uint8_t suzuki_request_sens[] = {0x21, 0x08};
uint8_t kawasaki_request_gps[2] = {0x21, 0x0B};
uint8_t kawasaki_request_rpm[2] = {0x21, 0x09};
uint8_t kawasaki_request_speed[2] = {0x21, 0x0c};
uint8_t kawasaki_request_tps[2] = {0x21, 0x04};
uint8_t kawasaki_request_iap[2] = {0x21, 0x07};
uint8_t kawasaki_request_iat[2] = {0x21, 0x05};
uint8_t kawasaki_request_ect[2] = {0x21, 0x06};
uint8_t yamaha_request_sens[2] = {'?'};
uint8_t honda_request_sens[2] = {'?'};

uint8_t IDX_GPS;
uint8_t IDX_CLUTCH;
uint8_t IDX_RPM_H;
uint8_t IDX_RPM_L;
uint8_t IDX_SPEED;
uint8_t IDX_TPS;
uint8_t IDX_STPS;
uint8_t IDX_IAP;
uint8_t IDX_IAT;
uint8_t IDX_ECT;
uint8_t IDX_VOLT;

void KWP2000::set_bike_specific_values(const brand brand, const model model)
{
if (brand == SUZUKI)
{
ECU_addr = 0x12;

IDX_GPS = 26;
IDX_CLUTCH = 52;
IDX_RPM_H = 17;
IDX_RPM_L = 18;
IDX_SPEED = 16;
IDX_TPS = 19;
IDX_STPS = 46;
IDX_IAP = 20;
IDX_IAT = 22;
IDX_ECT = 21;
IDX_VOLT = 24;
/*
CLT?? 20
HO2?? 25
IAP2?? 27
idle speed?? 28
ISC valve controls the volume of air that bypasses the throttle valve -29
Fuel 31-38
mode (map)?? 50
pair 51
*/
}
else if (brand == KAWASAKI)
{
ECU_addr = 0x11;
start_diagnostic[1] = {0x80};
IDX_GPS = 6;
IDX_RPM_H = 6;
IDX_RPM_L = 7;
IDX_SPEED = 6;
IDX_TPS = 6;
IDX_IAP = 6;
IDX_IAT = 6;
IDX_ECT = 6;
}
else if (brand == YAMAHA)
{
/*
As far as I could understand the YDS (Yamaha Diagnostic Protocol) protocol, it is close to the Honda protocol.
There are no sender-/receiver-addresses or header information. Just the checksum at the end.
You initialize the diagnostic mode by sending 0x80 and from then on you can just submit 0x02 and it gives you:
Rpm, Speed, Error, Gear & Checksum.
*/
ECU_addr = '?';
}
else if (brand == HONDA)
{
// https://gonzos.net/projects/ctx-obd/
ECU_addr = '?';
}
}

#endif // ISO_h
Loading

0 comments on commit 243d79a

Please sign in to comment.