Skip to content

Commit

Permalink
#4 Uart initialization
Browse files Browse the repository at this point in the history
Uart baud rate should be 115200 on initialization
  • Loading branch information
minayaserrano authored Sep 27, 2023
2 parents a3cc1a1 + 84bb575 commit e36c2fa
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-arduino-nano-33-ble.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI-Arduino-Nano-33-BLE

on: [push, pull_request]
on: [push]

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-native.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI-native

on: [push, pull_request]
on: [push]

jobs:
test:
Expand Down
7 changes: 5 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
default_install_hook_types: [pre-push]
default_stages: [pre-push]
repos:
- repo: local
hooks:
- id: pio-test
name: pio-test
entry: pio test -e native
entry: pio test -e native -e nano33ble-renode --without-uploading
pass_filenames: false
language: system
stages: [push]
stages: [pre-push]
12 changes: 12 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]
pre-commit = "*"

[requires]
python_version = "3.8"
140 changes: 140 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
- ~~RoboCaddie sends a STOP message to the motor when it is stopped~~
- ~~RoboCaddie transmits a message to the motor every 30ms~~
- ~~Arduino Nano 33 BLE support~~
- Go to metal: Integration tests
- Go to metal:
- Main loop
- Arduino UART implementation
- ~~UART baud rate initialization~~
- Arduino TimeService implementation
- Consecutive messages should increase CI (Continuity counter) and CS (CheckSum) values
- RoboCaddie goes forward when it receives a forward command
- RoboCaddie goes backward when it receives a backward command
- RoboCaddie goes left when it receives a left command
Expand Down
2 changes: 2 additions & 0 deletions lib/RoboCaddie/src/RoboCaddie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ RoboCaddie::~RoboCaddie() {}

int RoboCaddie::getStatus() { return STOP; }

void RoboCaddie::init() { uart.init(); }

void RoboCaddie::transmission() {
std::vector<uint8_t> stopMsg = {0x04, 0x01, 0x0A, 0x57, 0x0E, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90};
Expand Down
1 change: 1 addition & 0 deletions lib/RoboCaddie/src/RoboCaddie.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class RoboCaddie {
RoboCaddie(RoboCaddieUART::UART &, TimeService &);
~RoboCaddie();
int getStatus();
void init();
void transmission();
void run();
};
Expand Down
5 changes: 5 additions & 0 deletions lib/RoboCaddie/src/UART.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class UART {
public:
UART() {}
~UART() {}
virtual void init(void) = 0;
virtual int transmit(const uint8_t *message, int length) = 0;
};

Expand All @@ -19,6 +20,7 @@ class SpyUART : public RoboCaddieUART::UART {
private:
std::vector<uint8_t> lastSentMessage;
uint64_t numberOfExecutions = 0;
uint64_t baudRate = 0;
const int MAX_UART_MESSAGE_LENGTH = 258;

public:
Expand All @@ -27,6 +29,8 @@ class SpyUART : public RoboCaddieUART::UART {
lastSentMessage = {};
}

void init() { baudRate = 115200; }

int transmit(const uint8_t *message, int length) {
lastSentMessage.assign(message, message + length);
numberOfExecutions = numberOfExecutions + 1;
Expand All @@ -35,6 +39,7 @@ class SpyUART : public RoboCaddieUART::UART {

std::vector<uint8_t> getLastSentMessage() { return lastSentMessage; }
uint8_t getNumbersOfExecutions() { return numberOfExecutions; }
uint64_t getBaudRate() { return baudRate; }
};

#endif
9 changes: 3 additions & 6 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

[env:native]
platform = native
test_ignore = test_runner_embedded

[env:nano33ble]
platform = [email protected]
Expand All @@ -18,15 +19,11 @@ framework = arduino
; Use arduino framework Unity version instead of Platformio default version
; https://docs.platformio.org/en/stable/advanced/unit-testing/frameworks/custom/examples/custom_unity_library.html
test_framework = custom
test_ignore = test_runner_native

[env:nano33ble-renode]
platform = [email protected]
board = nano33ble
framework = arduino
extends = env:nano33ble
build_flags = -DRENODE_ENVIRONMENT
; Use arduino framework Unity version instead of Platformio default version
; https://docs.platformio.org/en/stable/advanced/unit-testing/frameworks/custom/examples/custom_unity_library.html
test_framework = custom
platform_packages =
platformio/tool-renode
test_testing_command =
Expand Down
66 changes: 23 additions & 43 deletions test/Test_RoboCaddie.cpp → test/RoboCaddie/RoboCaddieTests.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#ifdef UNIT_TEST

#ifdef ARDUINO_ARCH_MBED
#include <Arduino.h>
#endif
#include <RoboCaddie.h>
#include <TimeService.h>
#include <unity.h>

// Fix arduino/ArduinoCore-mbed issue:
// https://github.com/platformio/platformio-core/issues/3980#issuecomment-1500895461
#ifdef ARDUINO_ARCH_MBED
#ifndef RENODE_ENVIRONMENT
REDIRECT_STDOUT_TO(Serial);
#endif
#endif
void test_robocaddie_is_stopped_on_startup();
void test_a_STOP_message_is_sent_to_the_motor_when_robocaddie_status_is_STOP();
void test_robocaddie_sends_a_transmission_every_30_ms();
void test_uart_baud_rate_should_be_115200();

int run_tests() {
UNITY_BEGIN();

RUN_TEST(test_robocaddie_is_stopped_on_startup);
RUN_TEST(
test_a_STOP_message_is_sent_to_the_motor_when_robocaddie_status_is_STOP);
RUN_TEST(test_robocaddie_sends_a_transmission_every_30_ms);
RUN_TEST(test_uart_baud_rate_should_be_115200);

return UNITY_END();
}

void test_robocaddie_is_stopped_on_startup() {
SpyUART uart;
Expand Down Expand Up @@ -101,38 +103,16 @@ void test_robocaddie_sends_a_transmission_every_30_ms() {
TEST_ASSERT_EQUAL_UINT64(2, uart.getNumbersOfExecutions());
}

#ifndef ARDUINO_ARCH_MBED

int main(int argc, char **argv) {
UNITY_BEGIN();

RUN_TEST(test_robocaddie_is_stopped_on_startup);
RUN_TEST(
test_a_STOP_message_is_sent_to_the_motor_when_robocaddie_status_is_STOP);
RUN_TEST(test_robocaddie_sends_a_transmission_every_30_ms);

UNITY_END();
}

#endif

#ifdef ARDUINO_ARCH_MBED

void setup() {
delay(2000);
void test_uart_baud_rate_should_be_115200() {
SpyUART uart;
FakeTimeService time;
RoboCaddie robocaddie(uart, time);

UNITY_BEGIN();
TEST_ASSERT_EQUAL_UINT64(0, uart.getBaudRate());

RUN_TEST(test_robocaddie_is_stopped_on_startup);
RUN_TEST(
test_a_STOP_message_is_sent_to_the_motor_when_robocaddie_status_is_STOP);
RUN_TEST(test_robocaddie_sends_a_transmission_every_30_ms);
robocaddie.init();

UNITY_END();
TEST_ASSERT_EQUAL_UINT64(115200, uart.getBaudRate());
}

void loop() { delay(1000); }

#endif

#endif
23 changes: 23 additions & 0 deletions test/test_runner_embedded/Test_Embedded.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifdef UNIT_TEST

#include <Arduino.h>
#include <RoboCaddie.h>
#include <unity.h>

#include "RoboCaddie/RoboCaddieTests.h"

// Fix arduino/ArduinoCore-mbed issue:
// https://github.com/platformio/platformio-core/issues/3980#issuecomment-1500895461
#ifndef RENODE_ENVIRONMENT
REDIRECT_STDOUT_TO(Serial);
#endif

void setup() {
delay(2000);

run_tests();
}

void loop() { delay(1000); }

#endif
File renamed without changes.
10 changes: 10 additions & 0 deletions test/test_runner_native/Test_Native.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifdef UNIT_TEST

#include <RoboCaddie.h>
#include <unity.h>

#include "RoboCaddie/RoboCaddieTests.h"

int main() { return run_tests(); }

#endif

0 comments on commit e36c2fa

Please sign in to comment.