Skip to content

Commit

Permalink
fix bugs; change behaviour auf user led
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-h committed Mar 5, 2019
1 parent f18b5df commit 472d0e1
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 65 deletions.
12 changes: 8 additions & 4 deletions ECU/application/ECU.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "ECU.h"

ECU::ECU(Serial* uart, double t_period_s, double timeout_s) : uart(uart), t_period_s(t_period_s), timeout_s(timeout_s) {
ECU::ECU(Serial* uart, DigitalOut* status, double t_period_s, double timeout_s) : uart(uart), status(status), t_period_s(t_period_s), timeout_s(timeout_s) {
ini_ok = 0;
timer = new RtosTimer(callback(this, &ECU::statemachine), osTimerPeriodic);
}
Expand Down Expand Up @@ -50,7 +50,7 @@ void ECU::statemachine()
else if(state == WAIT_FOR_SILENT_BUS && t > 0 && packet.validate_control_packet(THS_ID, ECU_ID, REQUEST, rx_data)) {
state = BUS_BUSY;
}
else if(state == WAIT_FOR_SILENT_BUS && t > timeout_s) {
else if(state == WAIT_FOR_SILENT_BUS && t > 10 * timeout_s) {
state = START;
}
else if(state == BUS_BUSY && t > 0) {
Expand Down Expand Up @@ -103,8 +103,12 @@ void ECU::statemachine()

/*** output process image ***/
if(!ini_ok);
else if(state == REQUEST_TH_POS) { uart->putc(tx_data); }
else if(state == SEND_MOTOR_PAR) { uart->putc(tx_data); }
else if(state == WAIT_FOR_SILENT_BUS) { *status = 0; }
else if(state == BUS_BUSY) { *status = 1; }
else if(state == REQUEST_TH_POS) { uart->putc(tx_data); }
else if(state == RECEIVE_TH_POS) { *status = 1; }
else if(state == SEND_MOTOR_PAR) { uart->putc(tx_data); }
else if(state == WAIT_FOR_MCU_ACK) { *status = 0; }
// else;

ini_ok = 1;
Expand Down
3 changes: 2 additions & 1 deletion ECU/application/ECU.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef enum state_e

class ECU {
public:
ECU(Serial* uart, double t_period_s, double timeout_s);
ECU(Serial* uart, DigitalOut* status, double t_period_s, double timeout_s);
~ECU();
void start();
void stop();
Expand All @@ -29,6 +29,7 @@ class ECU {
Packet packet;
RtosTimer* timer;
Serial* uart;
DigitalOut* status;

uint8_t ini_ok;
state_t state;
Expand Down
5 changes: 2 additions & 3 deletions ECU/application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@

/*******************************************************************/
Serial uart(UART_TX, UART_RX, 9600);
ECU ecu(&uart, 2e-3, 0.2);
DigitalOut led(USER_LED);
ECU ecu(&uart, &led, 1e-3, 0.05);

int main (void)
{
ecu.start();

while(1)
{
led = !led;
wait(0.25);

}
}

29 changes: 25 additions & 4 deletions Engine/application/Engine.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Engine.h"

Engine::Engine(Serial* uart, DigitalOut* engine, double t_period_s) : uart(uart), engine(engine), t_period_s(t_period_s) {
Engine::Engine(Serial* uart, DigitalOut* engine, DigitalOut* status, double t_period_s) : uart(uart), engine(engine), status(status), t_period_s(t_period_s) {
ini_ok = 0;
timer = new RtosTimer(callback(this, &Engine::statemachine), osTimerPeriodic);
}
Expand Down Expand Up @@ -42,15 +42,26 @@ void Engine::statemachine()
if(!ini_ok) {
state = IDLE;
m_state = state;
attempts = 0;
}
else if(state != IDLE && t > 0 && error != 0) {
tx_data = packet.build_control_packet(ECU_ID, MCU_ID, ERROR_2);
state = SEND_ERROR;
}
else if(state == IDLE && t > 0 && packet.validate_data_packet(MCU_ID, rx_data)) {
state = WAIT_FOR_ECU_DATA;
else if(state == IDLE && t > 0 && attempts < 2 && packet.validate_data_packet(MCU_ID, rx_data)) {
attempts++;
}
else if(state == IDLE && t > 0 && packet.validate_data_packet(MCU_ID, rx_data)) {
attempts = 0;
tx_data = packet.build_control_packet(ECU_ID, MCU_ID, ACKNOWLEDGE);
state = SEND_ACK;
}
else if(state == WAIT_FOR_ECU_DATA && t > 0 && packet.validate_control_packet(ECU_ID, MCU_ID, ACKNOWLEDGE, rx_data)) {
else if(state == IDLE && t > 0 && packet.validate_control_packet(ECU_ID, MCU_ID, ACKNOWLEDGE, rx_data)) {
state = BUS_BUSY;
}
else if(state == BUS_BUSY && t > 0)
{
attempts = 0;
state = IDLE;
}
else if(state == WAIT_FOR_ECU_DATA && t > 0 && packet.validate_data_packet(MCU_ID, rx_data)) {
Expand All @@ -68,7 +79,17 @@ void Engine::statemachine()

/*** output process image ***/
if(!ini_ok);
else if(state == IDLE) {
*status = 0;
}
else if(state == BUS_BUSY) {
*status = 1;
}
else if(state == WAIT_FOR_ECU_DATA) {
*status = 1;
}
else if(state == SEND_ACK) {
*status = 0;
*engine = rx_data & DATA_MASK;
uart->putc(tx_data);
}
Expand Down
5 changes: 4 additions & 1 deletion Engine/application/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
typedef enum state_e
{
IDLE = 1,
BUS_BUSY,
WAIT_FOR_ECU_DATA,
SEND_ACK,
SEND_ERROR,
} state_t;

class Engine {
public:
Engine(Serial* uart, DigitalOut* engine, double t_period_s);
Engine(Serial* uart, DigitalOut* engine, DigitalOut* status, double t_period_s);
~Engine();
void start();
void stop();
Expand All @@ -26,7 +27,9 @@ class Engine {
RtosTimer* timer;
Serial* uart;
DigitalOut* engine;
DigitalOut *status;

uint8_t attempts;
uint8_t ini_ok;
state_t state;
state_t m_state;
Expand Down
5 changes: 2 additions & 3 deletions Engine/application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@
/*******************************************************************/
Serial uart(UART_TX, UART_RX, 9600);
DigitalOut engine_pp(ENGINE_PP, 1);
Engine engine(&uart, &engine_pp, 2e-3);
DigitalOut led(USER_LED);
Engine engine(&uart, &engine_pp, &led, 1e-3);

int main (void)
{
engine.start();

while(1)
{
led = !led;
wait(1.0);

}
}

16 changes: 0 additions & 16 deletions Engine/keil_xilinx_cm1/engine_xilinx_cm1.uvoptx
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,6 @@
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
<Bp>
<Number>1</Number>
<Type>0</Type>
<LineNumber>28</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>D:\Projects\SoC_Lab\Firmware\Engine\Engine.cpp</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
</Breakpoint>
<Tracepoint>
<THDelay>0</THDelay>
Expand Down
30 changes: 22 additions & 8 deletions Throttle/application/Throttle.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Throttle.h"

Throttle::Throttle(Serial* uart, DigitalIn* i_throttle, double t_period_s) : uart(uart), i_throttle(i_throttle), t_period_s(t_period_s) {
Throttle::Throttle(Serial* uart, DigitalIn* i_throttle, DigitalOut* status, double t_period_s) : uart(uart), i_throttle(i_throttle), status(status), t_period_s(t_period_s) {
ini_ok = 0;
timer = new RtosTimer(callback(this, &Throttle::statemachine), osTimerPeriodic);
}
Expand Down Expand Up @@ -42,16 +42,27 @@ void Throttle::statemachine()

if(!ini_ok) {
state = IDLE;
m_state = state;
}
m_state = state;
attempts = 0;
}
else if(state != IDLE && t > 0 && error != 0) {
tx_data = packet.build_control_packet(ECU_ID, THS_ID, ERROR_1);
state = SEND_ERROR;
}
else if(state == IDLE && t > 0 && packet.validate_control_packet(THS_ID, ECU_ID, REQUEST, rx_data)) {
state = WAIT_FOR_ECU_REQ;
else if(state == IDLE && t > 0 && attempts < 2 && packet.validate_control_packet(THS_ID, ECU_ID, REQUEST, rx_data)) {
attempts++;
}
else if(state == IDLE && t > 0 && packet.validate_control_packet(THS_ID, ECU_ID, REQUEST, rx_data)) {
attempts = 0;
tx_data = packet.build_data_packet(ECU_ID, throttle_pos);
state = SEND_TH_POS;
}
else if(state == IDLE && t > 0 && packet.validate_data_packet(ECU_ID, rx_data)) {
state = BUS_BUSY;
}
else if(state == WAIT_FOR_ECU_REQ && t > 0 && packet.validate_data_packet(THS_ID, rx_data)) {
else if(state == BUS_BUSY && t > 0)
{
attempts = 0;
state = IDLE;
}
else if(state == WAIT_FOR_ECU_REQ && t > 0 && packet.validate_control_packet(THS_ID, ECU_ID, REQUEST, rx_data)) {
Expand All @@ -69,8 +80,11 @@ void Throttle::statemachine()

/*** output process image ***/
if(!ini_ok);
else if(state == SEND_TH_POS) { uart->putc(tx_data); }
else if(state == SEND_ERROR) { uart->putc(tx_data); }
else if(state == IDLE) { *status = 0; }
else if(state == BUS_BUSY) { *status = 1; }
else if(state == WAIT_FOR_ECU_REQ) { *status = 1; }
else if(state == SEND_TH_POS) { *status = 0; uart->putc(tx_data); }
else if(state == SEND_ERROR) { uart->putc(tx_data); }
// else;

ini_ok = 1;
Expand Down
5 changes: 4 additions & 1 deletion Throttle/application/Throttle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
typedef enum state_e
{
IDLE = 1,
BUS_BUSY,
WAIT_FOR_ECU_REQ,
SEND_TH_POS,
SEND_ERROR,
} state_t;

class Throttle {
public:
Throttle(Serial* uart, DigitalIn* i_throttle, double t_period_s);
Throttle(Serial* uart, DigitalIn* i_throttle, DigitalOut* status, double t_period_s);
~Throttle();
void start();
void stop();
Expand All @@ -26,7 +27,9 @@ class Throttle {
RtosTimer* timer;
Serial* uart;
DigitalIn* i_throttle;
DigitalOut* status;

uint8_t attempts;
uint8_t ini_ok;
state_t state;
state_t m_state;
Expand Down
5 changes: 2 additions & 3 deletions Throttle/application/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@
/*******************************************************************/
Serial uart(UART_TX, UART_RX, 9600);
DigitalIn i_throttle(THROTTLE_PP);
Throttle throttle(&uart, &i_throttle, 2e-3);
DigitalOut led(USER_LED);
Throttle throttle(&uart, &i_throttle, &led, 1e-3);

int main (void)
{
throttle.start();

while(1)
{
led = !led;
wait(0.5);

}
}

2 changes: 1 addition & 1 deletion Throttle/keil_stm32f072/throttle_stm32f072.uvoptx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
<GroupNumber>1</GroupNumber>
<FileNumber>3</FileNumber>
<FileType>8</FileType>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\application\Throttle.cpp</PathWithFileName>
Expand Down
12 changes: 6 additions & 6 deletions Throttle/keil_xilinx_cm1/throttle_xilinx_cm1.uvoptx
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\main.cpp</PathWithFileName>
<FilenameWithoutPath>main.cpp</FilenameWithoutPath>
<PathWithFileName>..\..\Library\Packet.cpp</PathWithFileName>
<FilenameWithoutPath>Packet.cpp</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
Expand All @@ -399,8 +399,8 @@
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\..\Library\Packet.cpp</PathWithFileName>
<FilenameWithoutPath>Packet.cpp</FilenameWithoutPath>
<PathWithFileName>..\application\Throttle.cpp</PathWithFileName>
<FilenameWithoutPath>Throttle.cpp</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
Expand All @@ -411,8 +411,8 @@
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Throttle.cpp</PathWithFileName>
<FilenameWithoutPath>Throttle.cpp</FilenameWithoutPath>
<PathWithFileName>..\application\main.cpp</PathWithFileName>
<FilenameWithoutPath>main.cpp</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
Expand Down
26 changes: 13 additions & 13 deletions Throttle/keil_xilinx_cm1/throttle_xilinx_cm1.uvprojx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<CreateLib>0</CreateLib>
<CreateHexFile>0</CreateHexFile>
<DebugInformation>1</DebugInformation>
<BrowseInformation>0</BrowseInformation>
<BrowseInformation>1</BrowseInformation>
<ListingPath>.\objects\</ListingPath>
<HexFormatSelection>1</HexFormatSelection>
<Merge32K>0</Merge32K>
Expand Down Expand Up @@ -382,11 +382,6 @@
<Group>
<GroupName>application</GroupName>
<Files>
<File>
<FileName>main.cpp</FileName>
<FileType>8</FileType>
<FilePath>..\main.cpp</FilePath>
</File>
<File>
<FileName>Packet.cpp</FileName>
<FileType>8</FileType>
Expand All @@ -395,7 +390,12 @@
<File>
<FileName>Throttle.cpp</FileName>
<FileType>8</FileType>
<FilePath>..\Throttle.cpp</FilePath>
<FilePath>..\application\Throttle.cpp</FilePath>
</File>
<File>
<FileName>main.cpp</FileName>
<FileType>8</FileType>
<FilePath>..\application\main.cpp</FilePath>
</File>
</Files>
</Group>
Expand Down Expand Up @@ -1352,11 +1352,6 @@
<Group>
<GroupName>application</GroupName>
<Files>
<File>
<FileName>main.cpp</FileName>
<FileType>8</FileType>
<FilePath>..\main.cpp</FilePath>
</File>
<File>
<FileName>Packet.cpp</FileName>
<FileType>8</FileType>
Expand All @@ -1365,7 +1360,12 @@
<File>
<FileName>Throttle.cpp</FileName>
<FileType>8</FileType>
<FilePath>..\Throttle.cpp</FilePath>
<FilePath>..\application\Throttle.cpp</FilePath>
</File>
<File>
<FileName>main.cpp</FileName>
<FileType>8</FileType>
<FilePath>..\application\main.cpp</FilePath>
</File>
</Files>
</Group>
Expand Down
Loading

0 comments on commit 472d0e1

Please sign in to comment.