Skip to content

Commit

Permalink
Dimos CAN (#1084)
Browse files Browse the repository at this point in the history
### Summary
Dimos should now broadcast CAN properly.

### Changelist 
- Main Window CAN broadcasting in time
- Mutex locking for CAN tables

### Testing Done
- DevIO console logging

### Resolved Issues

### Checklist
*Please change `[ ]` to `[x]` when you are ready.*
- [x] I have read and followed the code conventions detailed in
[README.md](../README.md) (*This will save time for both you and the
reviewer!*).
- [x] If this pull request is longer then **500** lines, I have provided
*explicit* justification in the summary above explaining why I *cannot*
break this up into multiple pull requests (*Small PR's are faster and
less painful for everyone involved!*).

---------

Co-authored-by: peterjinweigu <[email protected]>
  • Loading branch information
Lucien950 and peterjinweigu authored Nov 16, 2023
1 parent 4188410 commit cb34abb
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 7 deletions.
16 changes: 15 additions & 1 deletion can_bus/json/dimos/dimos_tx.json
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
{}
{
"CommitInfo": {
"msg_id": 819,
"cycle_time": 100,
"description": "Dimos Current Running Commit Info",
"signals": {
"Hash": {
"bits": 32
},
"Clean": {
"bits": 1
}
}
}
}
2 changes: 1 addition & 1 deletion software/dimos/cmake/qt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function(ADD_QT_EXECUTABLE EXECUTABLE_NAME PROJECT_SOURCES LIBRARIES)
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
MACOSX_BUNDLE FALSE
WIN32_EXECUTABLE TRUE
)

Expand Down
12 changes: 10 additions & 2 deletions software/dimos/src/dev_io/can.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#include "can.h"
#include <iostream>
#include <thread>
#include <chrono>

using std::cout, std::endl;

Result<std::monostate, CanConnectionError> Can_Init() {
cout << "Can Initialized" << endl;
return std::monostate{};
}

Result<CanMsg, CanReadError> Can_Read() {
return CanMsg{};
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
cout << "Can Read Requested" << endl;
return CanMsg{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
}

Result<std::monostate, CanWriteError> Can_Write(const CanMsg *msg) {
cout << "Can with id " << msg->std_id << " Written" << endl;
return std::monostate{};
}
}
15 changes: 15 additions & 0 deletions software/dimos/src/ui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@ extern "C"
{
#include "App_CanTx.h"
#include "App_CanRx.h"
#include "Io_CanRx.h"
#include "Io_CanTx.h"
}

const bool GIT_COMMIT_HASH = true;
const bool GIT_COMMIT_CLEAN = false;

int main(int argc, char *argv[]) {
// IO init
Io_CanTx_Init(reinterpret_cast<void (*)(const CanMsg *)>(Can_Write));
Io_CanTx_EnableMode(CAN_MODE_DEFAULT, true);
// clear tables
App_CanTx_Init();
App_CanRx_Init();
// TODO commit hash
App_CanTx_dimos_Hash_Set(GIT_COMMIT_HASH);
App_CanTx_dimos_Clean_Set(GIT_COMMIT_CLEAN);

QApplication a(argc, argv);
MainWindow w;
w.show();
Expand Down
56 changes: 53 additions & 3 deletions software/dimos/src/ui/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,63 @@
#include <iostream>
#include <QThread>
#include <chrono>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "can.h"

using std::cout, std::endl;
extern "C" {
#include "Io_CanTx.h"
#include "Io_CanRx.h"
}

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
// CANTX TASK
tx100Hz.setInterval(10);
tx100Hz.setSingleShot(false);
QTimer::connect(&tx100Hz, &QTimer::timeout, Io_CanTx_Enqueue100HzMsgs);
tx100Hz.start();

tx1Hz.setInterval(1000);
tx1Hz.setSingleShot(false);
QTimer::connect(&tx1Hz, &QTimer::timeout, Io_CanTx_Enqueue1HzMsgs);
tx1Hz.start();

CanRxTaskThread = QThread::create(&MainWindow::CanRXTask);
CanTxPeriodicTaskThread = QThread::create(&MainWindow::CanPeriodicTXTask);
CanRxTaskThread->start();
CanTxPeriodicTaskThread->start();

ui->setupUi(this);
}

MainWindow::~MainWindow() = default;
[[noreturn]] void MainWindow::CanRXTask() {
while (true) {
Result<CanMsg, CanReadError> res = Can_Read();
if (res.index() == 1) {
switch (get<CanReadError>(res)) {
case ReadInterfaceNotCreated:
break;
case SocketReadError:
case IncompleteCanFrame:
continue;
}
continue;
}

// success
CanMsg message = get<CanMsg>(res);
// acquire lock
Io_CanRx_UpdateRxTableWithMessage(&message);
// release lock
}
}

[[noreturn]] void MainWindow::CanPeriodicTXTask() {
using namespace std::chrono;
while (true) {
auto ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
Io_CanTx_EnqueueOtherPeriodicMsgs(ms.count());
}
}

MainWindow::~MainWindow() = default;
9 changes: 9 additions & 0 deletions software/dimos/src/ui/mainwindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ Q_OBJECT

private:
std::unique_ptr<Ui::MainWindow> ui;

[[noreturn]] static void CanRXTask();

[[noreturn]] static void CanPeriodicTXTask();

QTimer tx100Hz{};
QTimer tx1Hz{};
QThread *CanRxTaskThread;
QThread *CanTxPeriodicTaskThread;
};

#endif // MAINWINDOW_H

0 comments on commit cb34abb

Please sign in to comment.