vjaunet / QUADCOPTER_V2

A quadcopter project based on Raspberry Pi and Arduino
MIT License
114 stars 40 forks source link

SPI byte sending error. #3

Closed erocircuits closed 8 years ago

erocircuits commented 9 years ago

Hi i am trying to get this code running.

issue 1: Unable to send single byte say byteSent giving me error can't sent spi byte.

But if i populate the tx[] with hello world string then its getting passed to arduino. if the number of bytes less then 8 in tx[] then it gives error can't send spi byte.

Amy help pls.

vjaunet commented 9 years ago

Hi,

can you give me some more details on where/when does this error happen ?

SPI is a protocole that exchange data between slave and master at the same time (3 wires interface), if the tx is empty, I assume that nothing can be sent and thus nothing retrieved. If you want to send less than 8 bit, fill up the useless ones with 0.

Regards, Vincent

erocircuits commented 9 years ago

hi, thanks for the reply and sorry for the delay.

Here is the quad_pilot.log output.

rm: cannot remove `quadpilot.log': No such file or directory

QuadCopter Pilot v2.0

Initializing I2C devices... Testing device connections... MPU6050 connection successful Initializing DMP... Success! DMP code written and verified. Success! DMP configuration written and verified. Current FIFO count=0 Waiting for FIFO count > 2... Current FIFO count=42Enabling DMP... DMP ready! Initializing IMU... IMU init done; offset values are : yaw = -29.814916, pitch = -25.174358, roll = -3.866261, n= 956

SPI initialization finished Setting PID constants Rate PID : Kp = 1.400000, Ki = 0.010000, Kd = 0.020000 Rate PID : Kp = 2.500000, Ki = 0.010000, Kd = 0.500000 Yaw Rate PID : Kp = 5.000000 SPI writeByte : can't send spi message SPI readByte : can't get spi message SPI readByte : can't get spi message SPI writeByte : can't send spi message SPI readByte : can't get spi message

and the SPI read/write error goes on...

erocircuits commented 9 years ago

In the SPIClass.cpp

If i fill it up the tx[] with (Hello World string)

uint8_t tx[] = { 0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x20, 0x57, 0x4F, 0x52, 0x4C, 0x44, 0x0A };

and call ArduSPI.wirteByte once after disableing Timer.start();

i am able to get the string Hello World in the SPI port (Arduinio with SPI to Serial print sketch)

if i fill the tx[] with anything less then 8 bytes will always gives error "SPI readByte : can't get spi message"

Note : used this url for testing : http://mitchtech.net/raspberry-pi-arduino-spi/

int SPI::writeByte(uint8_t byteSent) { int ret;

//uint8_t tx[1] = {byteSent};

uint8_t tx[] = { 0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x20, 0x57, 0x4F, 0x52, 0x4C, 0x44, 0x0A };

uint8_t rx[ARRAY_SIZE(tx)] = {0,};

struct spi_ioc_transfer tr; tr.tx_buf = (unsigned long)tx; tr.rx_buf = (unsigned long)rx; tr.len = ARRAY_SIZE(tx); tr.delay_usecs = _delay; tr.speed_hz = _speed; tr.bits_per_word = _bits;

ret = ioctl(_fd, SPI_IOC_MESSAGE(1), &tr); if (ret < 1) { printf("SPI writeByte : can't send spi message\n"); }

return ret;

}

vjaunet commented 9 years ago

This reminds me something : do you launch the quad_pilot program with sudo ? You need to be root to r/w not the /dev/spi* hardware file. I'll check tonight if the repo is up to date with my own quad. It has been a little while since I worked on it. Cheers, Vincent

erocircuits commented 9 years ago

Yes. I am launching with Sudo.

I am able to send the "Hello World" string as i mentioned in the previous message. This confirms that i have no permission issue.

erocircuits commented 9 years ago

hi vincent,

any help?...

i am still struck at spi routine...

if possible can you pls share some pics or videos of your quad.

tks.

vjaunet commented 9 years ago

Hi, Sorry I have been very busy these days. I am still confused. Are you trying to send a packet with less than 8 bits via SPI ? Or any other 8 bits array else than "Hello World" bugs ? If you want to send less than 8 bits, you need to fill the remaining one by zero, so that you send 8 bits.

What code did you put on the arduino side ? the timer.cpp first write a specific order to the arduino so that the arduino fills-up its SPDR register. Then it read/write simultaneously the rest of the data to be exchanged. My code is sending/reading uint_8 variables for efficiency considerations, in a very dedicated manner. You should modify mitchtech arduino's code to receive the ISR in the quad's arduino code. Indeed, the code from mitchtech expects characters and displays something when the character "\n" has been received, which I never send, but you do when you fill-up tx[] with "H,e,l,l,o, ,W,o,r,l,d,(0x0A)"

Hope this can help. Cheers, Vincent

erocircuits commented 9 years ago

Tks for the reply and really sorry to take your time.

Sorry for the below lengthy post. Might be useful for other beginners like me.

At first i compiled and started the quad_pilot through launch_pilot.sh

Added LED and pushbutton as per the sh script.

i have integrated 6channel receiver with arduino pro mini and tested with debugging. then i uploaded the final sketch and connected to Rpi.

LED and pushbutton works fine. Checked i2c ok for mpu6050 initialization completed successfully spi initialization successful....

Then i am getting error spi can't send byte or spi can't read byte.

i thought Rpi not able to send bytes through spi ports to arduino. So i tryed a simple program hellow world to test the Rpi to arduino without changing and hardware connections. And it worked. (Arduino with spi to serial sketch loaded)

This confirms the all hardware connection ok. But i only struck up with spi::writeByte function.

So i try to debug the spi::writeBtye function with the tx[] loaded with hellow world string and called arduSPI.writeByte multiple times and it is working. (After disabling timer function in main.cpp)

Now i am confused why normal sequence of bytes not working in spi port for rc channel info and esc data out Towards arduino.

erocircuits commented 9 years ago

I mean why i am getting the error : SPI writeByte : can't send spi message.

vjaunet commented 9 years ago

I don't think the issue is in the SPI class. I feel like it is more related to what happens in the arduino. Don't you have too many Interrupts going on ? Could be that the SPI transfer is interrupted by your PWM measurements.

Please, post a version of your code in your github so that we can have a look. It si too difficult to debug without any piece of your code.

Regards, Vicnent

erocircuits commented 9 years ago

Dear vincent

Dont worry about arduino sketch. As byte is sent from rpi spi port we will get successful otherwise it will through the error.

So far what i have found is

 ArduSPI.writeByte/ .readByte /rwByte are working fine within main() function.

If ArduSPI.writeByte is called anywhere other than main() it shows the error byte not sent.

Is it due to the ArduSPI.initialize in main function.

Will you available for live chat sometime?

I am really breaking my head for last 1 week.

Sent from Yahoo Mail on Android

From:"vjaunet" notifications@github.com Date:Wed, Sep 2, 2015 at 5:47 PM Subject:Re: [QUADCOPTER_V2] SPI byte sending error. (#3)

I don't think the issue is in the SPI class. I feel like it is more related to what happens in the arduino. Don't you have too many Interrupts going on ? Could be that the SPI transfer is interrupted by your PWM measurements.

Please, post a version of your code in your github so that we can have a look. It si too difficult to debug without any piece of your code.

Regards, Vicnent

— Reply to this email directly or view it on GitHub.

erocircuits commented 9 years ago

Hi vincent, yes. you are right t there is no issue with SPIClass as it works when sending debug message.

The Arduino sketch and RPI programs are same in the repo. (I have just changed arduino sketch to SPI to serial for debuging. But when running the I use the same Arduino sketch as yours). So I can confirm that I have the exact clone of your repo.

to summarize ...... when launching ---sudo ,/launch_pilot.sh


pi@pi ~/git/test/QUADCOPTER_V2/RPI/PILOT $ sudo ./launch_pilot.sh

Waiting for button ... Button pressed : stay pressed for shutdown Restarting Pilot

Waiting for button ...

Quad_pilot.log output


rm: cannot remove `quadpilot.log': No such file or directory

QuadCopter Pilot v2.0

Initializing I2C devices... Testing device connections... MPU6050 connection successful Initializing DMP... Success! DMP code written and verified. Success! DMP configuration written and verified. Current FIFO count=0 Waiting for FIFO count > 2... Current FIFO count=42Enabling DMP... DMP ready! Initializing IMU... IMU init done; offset values are : yaw = -167.500650, pitch = 51.526670, roll = 14.009199, n= 1981

SPI initialization finished Setting PID constants Rate PID : Kp = 1.400000, Ki = 0.010000, Kd = 0.020000 Rate PID : Kp = 2.500000, Ki = 0.010000, Kd = 0.500000 Yaw Rate PID : Kp = 5.000000 SPI writeByte : can't send spi message SPI readByte : can't get spi message SPI readByte : can't get spi message SPI writeByte : can't send spi message SPI readByte : can't get spi message SPI readByte : can't get spi message SPI writeByte : can't send spi message


When I try to debug

sending the below commands within main() after ArduSPI.initialize ArduSPI.writeByte('S'); ArduSPI.writeByte(0x0A);

No errors

sending the below commands other than main()

example: void Blink_led(){ // use gpio to blink an led on pin 0 for(int i=0;i<30;i++){ system("/usr/local/bin/gpio write 0 0"); usleep(50000); system("/usr/local/bin/gpio write 0 1"); usleep(50000); } ArduSPI.writeByte('S'); ArduSPI.writeByte(0x0A); }

I am getting SPI writeByte : can't send spi message

So I assume SPIClass is working fine but not able to send the ArduSPI.writeByte other than main() That's why periotic timer function running in timer.cpp with ArduSPI.writeByte throwing SPI writeByte : can't send spi message continuously (infact this error is for ArduSPI.writeByte, ArduSPI.readByte and ArduSPI.rwByte)

Can you please help in confirming the repo is upto date and it is working for you. I am really in urgency to complete this project. Kindly help.

If Possible can you please upload it with your setup and confirm.


I am Running RPI B with Noobs with RASPBIAN
Linux pi 3.18.11+ #781 PREEMPT Tue Apr 21 18:02:18 BST 2015 armv6l GNU/Linux

and GCC version gcc (Debian 4.6.3-14+rpi1) 4.6.3

If possible can we have any online chat at your convenient time.

vjaunet commented 9 years ago

I made un update of the repo. But the changes seems cosmetic. Not sure this can help. Really sorry but I definitely have no time for a chat those days.

Regards, Vincent

erocircuits commented 9 years ago

Dear vincent

Can you pls confirm the code is running in your rpi without any issues?

If possible pls send me some pics or videos of your quad. Just to gave feeling and how you stacked the rpi and arduino stuff.

Whats you quad config? BL, ESC and battery.

Sent from Yahoo Mail on Android

From:"vjaunet" notifications@github.com Date:Thu, Sep 3, 2015 at 12:27 AM Subject:Re: [QUADCOPTER_V2] SPI byte sending error. (#3)

I made un update of the repo. But the changes seems cosmetic. Not sure this can help. Really sorry but I definitely have no time for a chat those days.

Regards, Vincent

— Reply to this email directly or view it on GitHub.

erocircuits commented 9 years ago

Got the hw config. You already mentioned it in readme file.

erocircuits commented 9 years ago

Did you go through my ealier post... the complete summary Of the satus.

Any clue why ArduSPI.writeByte only works in main() and not in the timer.cpp or any where else?

This is the only issue left now to get my quad to test.

erocircuits commented 9 years ago

Hi vincent I have verified that SPI::readByte() function is not used anywhere so removed the SPI::readByte() both in SPIClass.cpp and SPIClass.h....... Now SPI error message has completely stoped...:-)

Now I am trying to test the complete hardware setup with ESC and BL. Will update.

Mean while can you please send some Pics or videos... Is your quad is stable with this setup and flying fine?

vjaunet commented 9 years ago

Glad you found a solution, but it is really strange. For sake of curiosity, can you put back the readByte function and recompile? Could be some compilation related stuff. Are you using the makefile ?

The quad is stable, but you will have to finetune the PID constants to fit your set-up... This is harsh and must be done with care, preferably on a stand with the quad solidly attached.

I'll see what I can do for the pics. Cheers Good luck ! Vincent

vjaunet commented 9 years ago

I updated the repo to the exact version that is in the quad rigth now. The one that has flown ;-) Cheers Vincent

erocircuits commented 9 years ago

Hi vincent, I have replaced the SPIClass.cpp functions with out class. seems to working good in RPI side without any issue. I could See the RC details going smooth and sent back from RPI with PID correction.

Now in the arduino side once started after a couple of secs it is getted struck and the started LED is continuously blinking.

I try to do debug print. it shows the RC details sent and received correctly for few sec then started showing junk and getting reset.

Is it because Arduino not able to handle fast SPI com from RPI?

vjaunet commented 9 years ago

Hi, I don't think so, but you can try to increase the SPI Baudrate on the RPI side. Did you add any channels on the Arduino side ? I remember that I tried to add a 5th one and it messed up everything. Check that your pwm signal are in the range expected (800us-1800us), could be a division by 0 or an unexpected negative value hanging around. I made some test in the quad_pilot but they are not universal and may not be adapted to you RC receiver. Could also be your PID that winds-up, you can use gnuplot to plot the logfile values over time.

Cheers, Vincent

erocircuits commented 9 years ago

Hi vincent I am using arduino pro mini. As per connection Pitch output of receiver connected to Pin 10 of arduino which is slave select.

define PITCH_IN_PIN 10

In slave mode when the SS pin goes high SPI will shutdown and dropout all on going communications.

I have re-mapped the pins of motor out and receiver and every thing is normal and SPI is running with zero errors. i am getting the corrected pwm values nicely from RPI (i have made free all 4 SPI pins now to use only for SPI com)

Now i am getting the PWM in all 4 motor out pins verified with DSO. When connecting all motors one motor is working fine and all of a sudden one other ESC got burnt.

I am troubleshooting the motor and ESC parts with PWM tester....will update.

Mean while can you please confirm any other parameter to be checked like the range of calibrated IMU data and Attitude values, Whats PID value should i pass with the launch_poilt.sh to start with?

vjaunet commented 9 years ago

Sure, if you used the SPI pins for PWM that could not work ! :-) For PID tunning, have a look here: http://blog.oscarliang.net/quadcopter-pid-explained-tuning/. It is very well explained. Actual values depends on you own set-up, but start low. No need to the calibrate the IMU since we already use the DMP that is inside. Be simply aware that the Pilot code don't use quaternions and is therefore sensitive to the gimbal lock issue, when the angle is close to 180deg.

BTW, here is a video of a quad flying with the Pilot v1 code (almost the same as v2) : https://www.youtube.com/watch?v=cc01GgzKo0c

Cheers, Vincent