Skip to content

Commit 742ba92

Browse files
samuelxu91biocubed
authored andcommitted
changed the arduino code to support cleaner current transitions
Made the catheter gui a ros-package to leverage rosdocs and roslint
1 parent 6fe14e7 commit 742ba92

32 files changed

+279
-190
lines changed

src/catheter_arduino_ard_ide/arduino_hardware.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,29 @@ void toggle_enable(int channel, int en) {
4040
*/
4141
void set_direction(int channel, int direction)
4242
{
43+
toggle_enable(channel, 0);
4344
if (direction == 0)
4445
{
45-
digitalWrite(H_Neg_pins[channel], DIR_ON);
4646
digitalWrite(H_Pos_pins[channel], !DIR_ON);
47+
digitalWrite(H_Neg_pins[channel], DIR_ON);
4748
}
4849
else
4950
{
50-
digitalWrite(H_Pos_pins[channel], DIR_ON);
5151
digitalWrite(H_Neg_pins[channel], !DIR_ON);
52+
digitalWrite(H_Pos_pins[channel], DIR_ON);
5253
}
54+
toggle_enable(channel, 1);
5355
}
5456

57+
// zeros out the channel completely
58+
void zero(int channel)
59+
{
60+
toggle_enable(channel, 0);
61+
62+
digitalWrite(H_Neg_pins[channel], DIR_ON);
63+
digitalWrite(H_Pos_pins[channel], DIR_ON);
5564

65+
}
5666
/**
5767
* @brief translate the 16 bit SPI data received from the MCP3201 ADC to the 12 bits sent to the PC
5868
*

src/catheter_arduino_ard_ide/catheter_arduino_ard_ide.ino

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,27 @@ unsigned int camera_counter;
5555
bool mriStatOld;
5656

5757
/**
58-
* @brief The Arduino time at which the MRI begins scanning.
58+
* @brief The MR scanning duration in which the currents are turned off.
59+
*
60+
* The duration is defined in scan lines.
5961
*/
60-
unsigned long scanStartTime = 0;
62+
double scanLines = 4;
6163

62-
/**
63-
* @brief The MR scanning duration in ms in which the currents are turned off.
64-
*/
65-
unsigned long scanDuration = 40;
6664

65+
volatile unsigned long scanStartTime;
66+
unsigned long scanDuration;
6767

68+
/**
69+
* @brief The interrupt handler is meant to always ensure that the current is disabled.
70+
*/
71+
void disableChannels()
72+
{
73+
scanStartTime = millis();
74+
for (int i = 0; i < NCHANNELS; i++)
75+
{
76+
zero(i);
77+
}
78+
}
6879

6980
/**
7081
* @brief The Arduino executes this function once when power on.
@@ -77,12 +88,19 @@ void setup()
7788
camera_counter = 0;
7889
mriStatOld = false;
7990

91+
// Here, the scanLines is converted to ms.
92+
// Each line takes 2.88 ms.
93+
// The arduino turn off delay of 0.5 ms is also added.
94+
scanDuration = ceil(scanLines*2.88+0.5);
95+
8096
for ( int i = 0; i < 512; i++)
8197
{
8298
inputBytes[i] = 0;
8399
outputBytes[i] = 0;
84100
}
85101

102+
attachInterrupt(mriPin, disableChannels, RISING);
103+
86104
delay(START_DELAY);
87105
}
88106

@@ -121,25 +139,17 @@ void loop()
121139
int mriStat(camera_write(camera_counter));
122140

123141
// If the MR scanner is pinging.
124-
if (mriStat && !mriStatOld)
125-
{
126-
mriStatOld = true;
127-
scanStartTime = millis();
128-
129-
for (int i = 0; i < NCHANNELS; i++)
130-
{
131-
DAC_write(i, 0);
132-
}
133-
}
134142

135143
// If the MR scanner is neither pinging or scanning.
136-
if (!mriStat && mriStatOld && (scanStartTime + scanDuration) < millis())
144+
if (scanStartTime > 0 && (scanStartTime + scanDuration) < millis())
137145
{
138-
mriStatOld = false;
139-
146+
scanStartTime = 0;
140147
for (int i = 0; i < NCHANNELS; i++)
141148
{
142-
DAC_write(i, channelList[i].DAC_val);
149+
set_direction(i,channelList[i].dir);
150+
toggle_enable(i, 1);
151+
143152
}
144153
}
145154
}
155+

src/catheter_arduino_ard_ide/cmd_support.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ uint8_t processSingleChannel(int i, channelStatus &local_channel, uint8_t cmdVal
216216
else
217217
{
218218
// enable or disable channel
219-
toggle_enable(i, en);
219+
// toggle_enable(i, en);
220220
// saved but useless.
221221
local_channel.enable = en;
222222
// set DAC value if necessary
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
build/
1+
build/
2+
doc/

src/catheter_arduino_gui/CMakeLists.txt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
cmake_minimum_required(VERSION 2.8)
22

3-
43
project(catheter_arduino_gui)
54

6-
75
find_package(catkin)
86

97
find_package(Boost COMPONENTS system thread REQUIRED)
@@ -12,22 +10,19 @@ find_package(GTest REQUIRED)
1210

1311
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O0" )
1412

15-
16-
1713
IF(catkin_FOUND)
1814

1915
MESSAGE(Found Catkin:)
2016

21-
find_package(catkin REQUIRED COMPONENTS roslint roscpp)
17+
find_package(catkin REQUIRED COMPONENTS roslint)
2218

2319
catkin_package(
24-
INCLUDE_DIRS inc
20+
INCLUDE_DIRS include
2521
LIBRARIES pc_utils_lib
2622
DEPENDS
2723
GTest
2824
Boost COMPONENTS system thread
2925
wxWidgets COMPONENTS core base adv propgrid
30-
CATKIN_DEPENDS roscpp
3126
)
3227

3328
ENDIF()
@@ -37,7 +32,7 @@ include_directories(
3732
${catkin_INCLUDE_DIRS}
3833
${BOOST_INCLUDE_DIRS}
3934
${GTEST_INCLUDE_DIRS}
40-
inc
35+
include
4136
)
4237

4338

src/catheter_arduino_gui/inc/gui/status_text.h

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/catheter_arduino_gui/inc/hardware/digital_analog_conversions.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/catheter_arduino_gui/inc/com/catheter_commands.h renamed to src/catheter_arduino_gui/include/catheter_arduino_gui/catheter_commands.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#pragma once
1818

19-
#ifndef CATHETER_ARDUINO_GUI_CATHETER_CHANNEL_COMMANDS_H
20-
#define CATHETER_ARDUINO_GUI_CATHETER_CHANNEL_COMMANDS_H
19+
#ifndef CATHETER_ARDUINO_GUI_CATHETER_COMMANDS_H
20+
#define CATHETER_ARDUINO_GUI_CATHETER_COMMANDS_H
2121

22-
#include "com/communication_definitions.h"
22+
#include "catheter_arduino_gui/communication_definitions.h"
2323

2424
#include <vector>
2525
#include <cstring>
@@ -182,4 +182,4 @@ int estResponseSize(const CatheterChannelCmdSet &);
182182

183183
void printData(const std::vector< uint8_t >& bytesRead);
184184

185-
#endif // CATHETER_ARDUINO_GUI_CATHETER_CHANNEL_COMMANDS_H
185+
#endif // CATHETER_ARDUINO_GUI_CATHETER_COMMANDS_H

src/catheter_arduino_gui/inc/gui/catheter_grid.h renamed to src/catheter_arduino_gui/include/catheter_arduino_gui/catheter_grid.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
#ifndef CATHETER_GUI_CATHETER_GRID_H
2-
#define CATHETER_GUI_CATHETER_GRID_H
1+
/*
2+
Copyright 2017 Russell Jackson
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#ifndef CATHETER_ARDUINO_GUI_CATHETER_GRID_H
18+
#define CATHETER_ARDUINO_GUI_CATHETER_GRID_H
319

420
#include "wx/wx.h"
521
#include "wx/panel.h"
@@ -9,8 +25,8 @@
925
#include "wx/generic/grideditors.h"
1026
#include <vector>
1127

12-
#include "com/communication_definitions.h"
13-
#include "com/catheter_commands.h"
28+
#include "catheter_arduino_gui/communication_definitions.h"
29+
#include "catheter_arduino_gui/catheter_commands.h"
1430

1531
// This file defines the grid in which commands are entered and run.
1632
// This object does not require any threaded communication as it is only
@@ -55,4 +71,4 @@ class CatheterGrid : public wxGrid
5571
unsigned int cmdCount;
5672
};
5773

58-
#endif // CATHETER_GUI_CATHETER_GRID_H
74+
#endif // CATHETER_ARDUINO_GUI_CATHETER_GRID_H

src/catheter_arduino_gui/inc/gui/catheter_gui.h renamed to src/catheter_arduino_gui/include/catheter_arduino_gui/catheter_gui.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1-
#ifndef CATHETER_GUI_CATHETER_GUI_H
2-
#define CATHETER_GUI_CATHETER_GUI_H
1+
/*
2+
Copyright 2017 Russell Jackson
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
18+
#ifndef CATHETER_ARDUINO_GUI_CATHETER_GUI_H
19+
#define CATHETER_ARDUINO_GUI_CATHETER_GUI_H
320

421

522
#include <wx/wx.h>
623
#include <boost/bind.hpp>
724
#include <boost/thread.hpp>
8-
#include "gui/catheter_grid.h"
9-
#include "gui/status_text.h"
10-
#include "gui/status_frame.h"
11-
#include "ser/serial_thread.h"
25+
#include "catheter_arduino_gui/catheter_grid.h"
26+
#include "catheter_arduino_gui/status_text.h"
27+
#include "catheter_arduino_gui/status_frame.h"
28+
#include "catheter_arduino_gui/serial_thread.h"
1229
#include <vector>
1330

1431
// This file defines the gui layout
@@ -318,4 +335,4 @@ class CatheterGuiApp : public wxApp
318335
};
319336

320337

321-
#endif // CATHETER_GUI_CATHETER_GUI_H
338+
#endif // CATHETER_ARDUINO_GUI_CATHETER_GUI_H

0 commit comments

Comments
 (0)