Community
cancel
Showing results for 
Search instead for 
Did you mean: 
TKirv1
New Contributor II
1,305 Views

Error handling for MRAA::I2C

Jump to solution

I am currently using a few i2c devices on a quadrotor which is being developed, so quite often an i2c line comes unplugged during a rough flight. Currently when this happens the program quits with the error

terminate called after throwing an instance of 'std::runtime_error'

what(): writeWord: mraa_i2c_write_word_data() failed

Aborted

which leads to the quadrotor's props spinning uncontrolled. I need the program to recognize/handle this error and continue running, so that it may safely turn off the motors. I have tried the following if statement

if( flow->readBytesReg(0x00,frame,22)==-1 || flow->readBytesReg(0x16,int_frame,25)==-1) {

kill(pwm);

exit(1);

}

But it seems the program is aborted before the condition is even evaluated so this does not work.

Is there a simple relatively simple way to do this?

Thanks,

-Thomas

1 Solution
idata
Community Manager
68 Views

Hello Thomas,

Thanks for reaching out!

I believe the answer to this question is simpler than one would think. I believe what you should try is to have two different scripts, the one you already have and one that starts to work whenever you have the issue described above. This second script would simply tell you quadrotor to stop (or to do some specific commands for this specific situation). The question is now, how do I start a second script when my original script crashes? Searching on the web I came across a great shell script (in http://stackoverflow.com/questions/7708715/check-if-program-is-running-with-bash-shell-script http://stackoverflow.com/questions/7708715/check-if-program-is-running-with-bash-shell-script in case you are interested) which I slightly modified. The script checks on a process (in my case called blink) and when it stops (by whatever reason), it starts a different one (in my case blink1). This is the script:

# !/bin/bash

check_process() {

 

echo "$ts: checking $1"

 

[ "$1" = "" ] && return 0

 

[ `pgrep -n $1` ] && return 1 || return 0

 

}

while [ 1 ]; do

 

# timestamp

 

ts=`date +%T`

echo "$ts: begin checking..."

 

check_process "blink"

 

[ $? -eq 0 ] && echo "$ts: not running, starting alternate condition..." && `./blink1` && break

 

sleep 5

 

done

***You'll need to install procps, procps-dbg, procps-dev and procps-docs in order to use pgrep, this packages can be downloaded from the official repo.

I hope this helps you. I tested it and works fantastic, let me know if you have any questions about the code and I'll gladly explain it to you.

 

-Peter.

View solution in original post

9 Replies
idata
Community Manager
69 Views

Hello Thomas,

Thanks for reaching out!

I believe the answer to this question is simpler than one would think. I believe what you should try is to have two different scripts, the one you already have and one that starts to work whenever you have the issue described above. This second script would simply tell you quadrotor to stop (or to do some specific commands for this specific situation). The question is now, how do I start a second script when my original script crashes? Searching on the web I came across a great shell script (in http://stackoverflow.com/questions/7708715/check-if-program-is-running-with-bash-shell-script http://stackoverflow.com/questions/7708715/check-if-program-is-running-with-bash-shell-script in case you are interested) which I slightly modified. The script checks on a process (in my case called blink) and when it stops (by whatever reason), it starts a different one (in my case blink1). This is the script:

# !/bin/bash

check_process() {

 

echo "$ts: checking $1"

 

[ "$1" = "" ] && return 0

 

[ `pgrep -n $1` ] && return 1 || return 0

 

}

while [ 1 ]; do

 

# timestamp

 

ts=`date +%T`

echo "$ts: begin checking..."

 

check_process "blink"

 

[ $? -eq 0 ] && echo "$ts: not running, starting alternate condition..." && `./blink1` && break

 

sleep 5

 

done

***You'll need to install procps, procps-dbg, procps-dev and procps-docs in order to use pgrep, this packages can be downloaded from the official repo.

I hope this helps you. I tested it and works fantastic, let me know if you have any questions about the code and I'll gladly explain it to you.

 

-Peter.

View solution in original post

TKirv1
New Contributor II
68 Views

Thank you Peter!

This looks like it should do the trick. I will test this as soon as possible and let you know how it goes.

-Thomas

idata
Community Manager
68 Views

I'm here to help!

 

 

Let me know how it goes.

 

-Peter.
TKirv1
New Contributor II
68 Views

I put this to the test and it works great! Only thing left to test is to see what happens if the SCL or SDA lines controlling pwm driver itself come unplugged. I would hope the driver would stop outputting a pwm signal, though.

Thanks for your help peter,

-Thomas

idata
Community Manager
68 Views

I'm glad to hear that!

 

 

Please continue your tests and keep us updated. If you still have any issues of doubts, please don't hesitate to come back to the community, we will try to help you in any way we can.

 

 

-Peter.
TKirv1
New Contributor II
68 Views

I actually have one more question: Is there a way to automatically run the script which checks on a process when that process starts?

Thanks,

Thomas

idata
Community Manager
68 Views

Yes, and the way to do it is easier that you would image. You can simply use a system call in you code, which I believe is written in C++, please correct me if that is wrong. The beginning of your script can look like the following:

# include

 

# include

using namespace std;

int main ()

 

{

 

cout << "Starting system check... \n";<p>  system("./system_check.sh &");

 

}

That should start your shell script alongside the C++ script.

Let me know if this helps you.

 

-Peter.
TKirv1
New Contributor II
68 Views

That works great. Thanks again Peter for being very helpful and teaching me something new!

Cheers,

Thomas

idata
Community Manager
68 Views

I'm glad to help! Keep us updated on your projects.

 

 

-Peter.
Reply