Skip to content

Commit 243d79a

Browse files
committed
brand aren't anymore hardcoded, other improvements
1 parent 4d5c9e4 commit 243d79a

File tree

8 files changed

+626
-548
lines changed

8 files changed

+626
-548
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
lib/**
22
.pio/**
3-
/test/**
3+
**/test/**
44
include/**
55
.vscode/**
66
doxygen/**

examples/basic_working/basic_working.ino

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
11
#include <Arduino.h>
22
#include "KWP2000.h"
33

4+
// Different core have different way to use a Serial port
45
#if defined(ARDUINO_ARCH_ESP32)
5-
HardwareSerial bike(2); // for the ESP32 core
6+
HardwareSerial bike(2);
7+
#define TX_PIN 17
68
#elif defined(ARDUINO_ARCH_STM32)
7-
HardwareSerial bike(PA3, PA2); // for the stm32duino core
9+
HardwareSerial bike(PA3, PA2); // RX and TX
10+
#define TX_PIN PA2
811
#else
9-
#define bike Serial3 // for the Arduino avr core
12+
#define bike Serial3
13+
#define TX_PIN 14
1014
#endif
1115

1216
#define debug Serial
13-
#define TX_PIN 14
1417

15-
KWP2000 ECU(&bike, TX_PIN);
16-
17-
uint8_t dealer_status = false;
18-
char in;
19-
uint8_t fake_request[] = {1, 2};
18+
// Replace YOUR_MOTORBIKE with SUZUKI, KAWASAKI, YAMAHA or HONDA
19+
KWP2000 ECU(&bike, TX_PIN, YOUR_MOTORBIKE);
2020

2121
void setup()
2222
{
2323
ECU.enableDebug(&debug, DEBUG_LEVEL_VERBOSE, 115200);
2424
//Serial.begin(); this is not needed because we use then same serial as the debug
25-
//ECU.enableDealerMode(8);
26-
27-
while (!debug)
28-
{
29-
// wait for connection with the serial
30-
}
31-
while (debug.available() > 0)
32-
{
33-
debug.read(); // empty serial buffer
34-
}
25+
//ECU.enableDealerMode(pin); // This is available only on Suzuki bikes
3526
}
3627

3728
void loop()
3829
{
39-
//ECU.printStatus();
4030
if (debug.available() > 0)
4131
{
42-
in = debug.read();
32+
char in = debug.read();
4333
debug.print("User Input: ");
4434
debug.println(in);
4535

@@ -53,11 +43,10 @@ void loop()
5343
break;
5444

5545
case 'd':
56-
dealer_status = !dealer_status;
57-
ECU.dealerMode(dealer_status);
46+
ECU.setDealerMode(!ECU.getDealerMode());
5847
break;
5948

60-
case 'D':
49+
case 't':
6150
ECU.readTroubleCodes();
6251
break;
6352

@@ -66,9 +55,8 @@ void loop()
6655
ECU.printSensorsData();
6756
break;
6857

69-
case 'h':
70-
ECU.handleRequest(fake_request, sizeof(fake_request));
71-
ECU.printLastResponse();
58+
case 'p':
59+
ECU.printStatus();
7260
break;
7361

7462
case 'c':

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=KWP2000
2-
version=1.2.1
2+
version=1.3.0
33
author=aster94
44
maintainer=aster94
55
sentence=A library that makes interfacing with motorbikes a breeze.
6-
paragraph=Connect with the ECU of Suzuki, Honda, Kawasaki, Yamaha etc.
6+
paragraph=Connect with the ECU of Suzuki, Kawasaki, Yamaha, Honda.
77
category=Device Control
88
url=https://github.com/aster94/Keyword-Protocol-2000
99
architectures=*

platformio.ini

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
[platformio]
44
src_dir = examples/basic_working
5+
;src_dir = examples/test
56
lib_dir = .
6-
;default_envs = mega
7+
default_envs = mega
78

9+
; Commmon
810
[env]
911
framework = arduino
12+
platform = atmelavr
13+
board = megaatmega2560
1014
monitor_speed = 115200
1115
check_tool = cppcheck
12-
build_flags = -DKAWASAKI
16+
build_flags =-D YOUR_MOTORBIKE=KAWASAKI
1317

14-
; Boards
18+
; Test the library on different platforms
1519
[env:mega]
1620
platform = atmelavr
1721
board = megaatmega2560
@@ -24,23 +28,15 @@ board = lolin32
2428
platform = ststm32
2529
board = maple_mini_b20
2630

27-
; Motorbikes
31+
; Test the library on each brand
2832
[env:suzuki]
29-
platform = atmelavr
30-
board = megaatmega2560
31-
build_flags = -DSUZUKI
33+
build_flags =-D YOUR_MOTORBIKE=SUZUKI
3234

3335
[env:kawasaki]
34-
platform = atmelavr
35-
board = megaatmega2560
36-
build_flags = -DKAWASAKI
37-
38-
[env:honda]
39-
platform = atmelavr
40-
board = megaatmega2560
41-
build_flags = -DHONDA
36+
build_flags =-D YOUR_MOTORBIKE=KAWASAKI
4237

4338
[env:yamaha]
44-
platform = atmelavr
45-
board = megaatmega2560
46-
build_flags = -DYAMAHA
39+
build_flags =-D YOUR_MOTORBIKE=YAMAHA
40+
41+
[env:honda]
42+
build_flags =-D YOUR_MOTORBIKE=HONDA

src/ISO.h

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
ISO.h
3+
Parameter IDs
4+
5+
Copyright (c) Aster94
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
8+
and associated documentation files (the "Software"), to deal in the Software without restriction,
9+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
10+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all copies or
14+
substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
17+
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#ifndef ISO_h
24+
#define ISO_h
25+
26+
#include "KWP2000.h"
27+
28+
// These values are defined by the ISO protocol
29+
#define ISO_BAUDRATE 10400
30+
#define ISO_MAX_DATA 260 ///< maximum length of a response from the ecu: 255 data + 4 header + 1 checksum
31+
#define ISO_T_P1 10 ///< inter byte time for ECU response - min: 0 max: 20
32+
#define ISO_T_P2_MIN_LIMIT 50
33+
#define ISO_T_P2_MAX_LIMIT 89600 ///< P2 time between tester request and ECU response or two ECU responses
34+
#define ISO_T_P3_MAX_LIMIT 89600 ///< P3 time between end of ECU responses and start of new tester request
35+
#define ISO_T_P4_MAX_LIMIT 20 ///< inter byte time for tester request
36+
// P2 (min & max), P3 (min & max) and P4 (min) are defined by the ECU with accessTimingParameter()
37+
#define ISO_T_IDLE 1000 ///< min 300, max undefined
38+
#define ISO_T_INIL (unsigned int)25 ///< Initialization low time
39+
#define ISO_T_WUP (unsigned int)50 ///< Wake up Pattern
40+
41+
// Common values between all brands/bikes
42+
43+
const uint8_t format_physical = 0x80;
44+
const uint8_t format_functional = 0xC0; // not supported
45+
const uint8_t format_CARB = 0x40; // not supported
46+
47+
const uint8_t OUR_addr = 0xF1; // between hex F0 and FD
48+
49+
const uint8_t start_com[] = {0x81};
50+
const uint8_t stop_com[] = {0x82};
51+
uint8_t start_diagnostic[2] = {0x10};
52+
const uint8_t read_data_local_identifier = 0x21;
53+
54+
// access timing parameter and timing paramenter indicator
55+
const uint8_t atp_read_limits[] = {0x83, 0x00};
56+
const uint8_t atp_set_default[] = {0x83, 0x01};
57+
const uint8_t atp_read_current[] = {0x83, 0x02};
58+
const uint8_t atp_set_given[] = {0x83, 0x03};
59+
60+
const uint8_t tester_present_with_answer[] = {0x3E, 0x01}; // the ECU will answer
61+
const uint8_t tester_present_without_answer[] = {0x3E, 0x02}; // the ECU won't answer
62+
63+
const uint8_t trouble_codes_all[] = {0x13};
64+
const uint8_t trouble_codes_only_active[] = {0x17};
65+
const uint8_t trouble_codes_with_status[] = {0x18};
66+
const uint8_t clear_trouble_codes[] = {0x14};
67+
68+
#define request_ok(x) x | 0x40
69+
const uint8_t request_rejected = 0x7F;
70+
71+
// Specific brands/bikes values here are only declared and will be assigned inside the function
72+
// this is very likely to change, do not refert too much to the code below
73+
74+
uint8_t ECU_addr;
75+
76+
uint8_t suzuki_request_sens[] = {0x21, 0x08};
77+
uint8_t kawasaki_request_gps[2] = {0x21, 0x0B};
78+
uint8_t kawasaki_request_rpm[2] = {0x21, 0x09};
79+
uint8_t kawasaki_request_speed[2] = {0x21, 0x0c};
80+
uint8_t kawasaki_request_tps[2] = {0x21, 0x04};
81+
uint8_t kawasaki_request_iap[2] = {0x21, 0x07};
82+
uint8_t kawasaki_request_iat[2] = {0x21, 0x05};
83+
uint8_t kawasaki_request_ect[2] = {0x21, 0x06};
84+
uint8_t yamaha_request_sens[2] = {'?'};
85+
uint8_t honda_request_sens[2] = {'?'};
86+
87+
uint8_t IDX_GPS;
88+
uint8_t IDX_CLUTCH;
89+
uint8_t IDX_RPM_H;
90+
uint8_t IDX_RPM_L;
91+
uint8_t IDX_SPEED;
92+
uint8_t IDX_TPS;
93+
uint8_t IDX_STPS;
94+
uint8_t IDX_IAP;
95+
uint8_t IDX_IAT;
96+
uint8_t IDX_ECT;
97+
uint8_t IDX_VOLT;
98+
99+
void KWP2000::set_bike_specific_values(const brand brand, const model model)
100+
{
101+
if (brand == SUZUKI)
102+
{
103+
ECU_addr = 0x12;
104+
105+
IDX_GPS = 26;
106+
IDX_CLUTCH = 52;
107+
IDX_RPM_H = 17;
108+
IDX_RPM_L = 18;
109+
IDX_SPEED = 16;
110+
IDX_TPS = 19;
111+
IDX_STPS = 46;
112+
IDX_IAP = 20;
113+
IDX_IAT = 22;
114+
IDX_ECT = 21;
115+
IDX_VOLT = 24;
116+
/*
117+
CLT?? 20
118+
HO2?? 25
119+
IAP2?? 27
120+
idle speed?? 28
121+
ISC valve controls the volume of air that bypasses the throttle valve -29
122+
Fuel 31-38
123+
mode (map)?? 50
124+
pair 51
125+
*/
126+
}
127+
else if (brand == KAWASAKI)
128+
{
129+
ECU_addr = 0x11;
130+
start_diagnostic[1] = {0x80};
131+
IDX_GPS = 6;
132+
IDX_RPM_H = 6;
133+
IDX_RPM_L = 7;
134+
IDX_SPEED = 6;
135+
IDX_TPS = 6;
136+
IDX_IAP = 6;
137+
IDX_IAT = 6;
138+
IDX_ECT = 6;
139+
}
140+
else if (brand == YAMAHA)
141+
{
142+
/*
143+
As far as I could understand the YDS (Yamaha Diagnostic Protocol) protocol, it is close to the Honda protocol.
144+
There are no sender-/receiver-addresses or header information. Just the checksum at the end.
145+
You initialize the diagnostic mode by sending 0x80 and from then on you can just submit 0x02 and it gives you:
146+
Rpm, Speed, Error, Gear & Checksum.
147+
*/
148+
ECU_addr = '?';
149+
}
150+
else if (brand == HONDA)
151+
{
152+
// https://gonzos.net/projects/ctx-obd/
153+
ECU_addr = '?';
154+
}
155+
}
156+
157+
#endif // ISO_h

0 commit comments

Comments
 (0)