Skip to content

Commit 636bc62

Browse files
author
Dilawar Singh
committed
reset is back. Ctrl+C will restart arduino is TRIAL0 mode. Fixes #40. Caused by #39.
1 parent 49d6488 commit 636bc62

4 files changed

Lines changed: 63 additions & 30 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ add_custom_target(reset_boards
133133
VERBATIM
134134
)
135135

136-
# Pass animal options to run.sh which will will passed to python script.
136+
# pass these arguments to camera server.
137137
SET(RUN_ARGS --port ${PORT} --datadir ${DATADIR})
138-
139138
add_custom_target(run
140139
DEPENDS cam_server upload
141140
COMMAND ${CMAKE_BINARY_DIR}/cam_server ${RUN_ARGS}

PointGreyCamera/src/main.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,11 @@ void ReadLine(string& res)
205205
// If the line starts with '<' or '>' characters; then dump them to console.
206206
if(res[0] == '<' || res[1] == '>')
207207
{
208-
cout << res << endl;
208+
cout << "[ARDUINO] " << res << endl;
209209
res = "";
210210
return;
211211
}
212212

213-
214213
// Append timestamp to the line.
215214
res = get_timestamp() + ',' + res;
216215
}
@@ -254,6 +253,11 @@ void ArduinoClient()
254253
arduinoQ_.push(line);
255254
arduinoQ_.pop();
256255
}
256+
257+
// Send 'r' to arduino to reboot it.
258+
cout << "[INFO] All done or Ctrl+C. Writing r to arduino. Reboot it." << endl;
259+
serial_ << 'r' << endl;
260+
return;
257261
}
258262

259263
// This function returns the camera to its default state by re-enabling automatic
@@ -354,12 +358,14 @@ int ProcessFrame(void* data, size_t width, size_t height)
354358
if( k < ' ')
355359
return 0;
356360

357-
cout << "[INFO] Key pressed " << k << endl;
361+
cout << "[INFO] Key pressed " << k;
358362
if(validCommands_.find(k) != std::string::npos)
359363
{
360-
cout << "[INFO] Got valid command. Writing to serial." << k << endl;
361364
serial_ << k << endl;
365+
cout << ". Valid command. Sending to Arduino: " << endl;
362366
}
367+
else
368+
cout << ". Invalid. Ignoring ... " << endl;
363369
return 0;
364370
}
365371

@@ -745,8 +751,8 @@ int main(int argc, char** argv)
745751

746752
// Release system_
747753
system_->ReleaseInstance();
748-
749754
destroyAllWindows();
755+
750756
if(t.joinable())
751757
t.join();
752758

src/main.ino

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* See the GIT changelog for more details.
1414
*/
1515

16+
#include <avr/wdt.h>
17+
1618
// Pin configurations and other global variables.
1719
#include "config.h"
1820

@@ -37,10 +39,25 @@ char trial_state_[5] = "PRE_";
3739
int incoming_byte_ = 0;
3840
bool reboot_ = false;
3941

42+
ISR(WDT_vect)
43+
{
44+
// Handle interuppts here.
45+
// Nothing to handle.
46+
}
47+
48+
/* --------------------------------------------------------------------------*/
49+
/**
50+
* @Synopsis When ctrl+c is pressed, reboot_ is set true and we restart the
51+
* arduino else ctrl+c will not stop arduino from giving stimulus.
52+
*/
53+
/* ----------------------------------------------------------------------------*/
4054
void reset_watchdog( )
4155
{
56+
if( not reboot_ )
57+
wdt_reset( );
4258
}
4359

60+
4461
/**
4562
* @brief Read a command from command line. Consume when character is matched.
4663
*
@@ -65,14 +82,25 @@ bool is_command_read( char command, bool consume = true )
6582
return false;
6683
}
6784

85+
void check_for_reset( void )
86+
{
87+
if( is_command_read('r', true ) )
88+
{
89+
Serial.println( ">>> Received r. Reboot in 2 seconds" );
90+
reboot_ = true;
91+
}
92+
}
93+
94+
6895
/**
6996
* @brief Write data line to Serial port in csv format.
7097
* @param data
7198
* @param timestamp
7299
*/
73100
void write_data_line( )
74101
{
75-
reset_watchdog( );
102+
check_for_reset();
103+
reset_watchdog();
76104

77105
// Just read the registers where pin data is saved.
78106
int puff = digitalRead( PUFF_PIN );
@@ -100,15 +128,6 @@ void write_data_line( )
100128
delay( 3 );
101129
}
102130

103-
void check_for_reset( void )
104-
{
105-
if( is_command_read('r', true ) )
106-
{
107-
Serial.println( ">>>Received r. Reboot in 2 seconds" );
108-
reboot_ = true;
109-
}
110-
}
111-
112131
/**
113132
* @brief Play tone for given period and duty cycle.
114133
*
@@ -120,7 +139,7 @@ void check_for_reset( void )
120139
*/
121140
void play_tone( unsigned long period, double duty_cycle = 0.5 )
122141
{
123-
// reset_watchdog( );
142+
reset_watchdog( );
124143
check_for_reset( );
125144
unsigned long toneStart = millis();
126145
while( (millis() - toneStart) <= period )
@@ -228,6 +247,11 @@ void wait_for_start( )
228247
Serial.println( ">>>Received 1. Start capturing frame" );
229248
digitalWrite( CAMERA_TTL_PIN, HIGH );
230249
}
250+
else if( is_command_read( 'r', true ) )
251+
{
252+
Serial.println( ">>>Received r. Rebooting " );
253+
reboot_ = true;
254+
}
231255
else if( is_command_read( '2', true ) )
232256
{
233257
Serial.println( ">>>Received 2. Stop capturing frames" );
@@ -263,6 +287,9 @@ void setup()
263287
// chars per ms i.e. baud rate should be higher than 100,000.
264288
Serial.begin( 115200 );
265289

290+
//esetup watchdog. If not reset in 2 seconds, it reboots the system.
291+
wdt_enable( WDTO_2S );
292+
wdt_reset();
266293
// Random seed.
267294
randomSeed( analogRead(A5) );
268295

@@ -363,7 +390,7 @@ void monitor_for_shock(void)
363390
*/
364391
void do_trial( size_t trial_num, bool isprobe = false )
365392
{
366-
// reset_watchdog( );
393+
reset_watchdog( );
367394
check_for_reset( );
368395

369396
print_trial_info( );
@@ -526,7 +553,7 @@ void do_trial( size_t trial_num, bool isprobe = false )
526553
sprintf(trial_state_, "ITI_");
527554
while((millis() - stamp_) <= rduration)
528555
{
529-
reset_watchdog( );
556+
reset_watchdog();
530557
write_data_line();
531558
delay(200);
532559
}
@@ -542,9 +569,10 @@ void loop()
542569

543570
for (size_t i = 1; i <= PROTO_NumTrialsInABlock; i++)
544571
{
545-
546-
reset_watchdog( );
572+
check_for_reset();
573+
reset_watchdog();
547574
trial_count_ = i;
575+
548576
if(String("TONE/LIGHT") == String(PROTO_CSValue))
549577
{
550578
// FOR Shomu. Mixed trials.

src/protocol.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// These variables are generated by cmake + /home/dilawars/Work/BhallaLab/MouseBehaviour/Protocols/protocol_to_config.py
22
#define PROTO_NumTrialsInABlock 60
3-
#define PROTO_USValue "PUFF"
4-
#define PROTO_PreStimDuration_HIGH 8000
53
#define PROTO_PreStimDuration_LOW 6000
6-
#define PROTO_SessionType 10
7-
#define PROTO_CSValue "LIGHT"
4+
#define PROTO_USValue "PUFF"
85
#define PROTO_CSDuration 50
96
#define PROTO_NextProbeTrial_HIGH 10
10-
#define PROTO_NextProbeTrial_LOW 6
11-
#define PROTO_PostStimDuration 8000
12-
#define PROTO_CODE "An2"
7+
#define PROTO_TraceDuration 450
138
#define PROTO_Comments "Ananth"
9+
#define PROTO_PostStimDuration 8000
10+
#define PROTO_PreStimDuration_HIGH 8000
11+
#define PROTO_CSValue "LIGHT"
1412
#define PROTO_Description "light + 450 trace + puff1"
15-
#define PROTO_TraceDuration 450
13+
#define PROTO_CODE "An2"
14+
#define PROTO_SessionType 10
15+
#define PROTO_NextProbeTrial_LOW 6
1616
#define PROTO_USDuration 50

0 commit comments

Comments
 (0)