Created Tue, 05 May 2015 13:47:00 +0000 by screamingtiger
Tue, 05 May 2015 13:47:00 +0000
Hello,
I am attempting to use the ChipKit Pi (CKP). I have a sketch written for arduino that works perfect on the Arduino. It uses the servo library as well as I2C. However when using the CKP it doesn't work as expected
I have tried uploading the "sweep" sketch that comes with the MPIDE and it does not work. None of my servo code works either on the CKP. The servo is being powered externally etc and like I said it works fine on an arduino. The same code does not make the servo move.
Also, attaching a servo to Pin 5 (digital pine 5 on the CKP) in the CKP code causes a hang. I mean calling Servo.attach(5) causes a hang, I don't have a servo physically connected yet.
Also for I2c, when I do a Wire.begin(9) I also get other items on the bus such as IDs 2,3,4 and I did not put them there. Its like there are phantom devices on the bus?
When I send data across, it does not come through correctly either. The CKP is a slave being send data by the Pi. The arduino gets the data perfectly, the CKP gets different data.
My understanding is that the 3.3V to 5V is not an issue when using I2C when CKP is the slave.
Please help me out, I am using the latest version of MPIDE 2014-8-21
What is going on I though the ChipKits where 100% arduino compatible or did I misunderstand?
Tue, 05 May 2015 14:54:09 +0000
What is going on I though the ChipKits where 100% arduino compatible or did I misunderstand?
You misunderstood. The only thing that can possibly be 100% arduino compatible is an Arduino. Everything else tries to be as compatible as possible, but due to hardware differences it's never possible to be 100% compatible.
The PIC32 chip on the ckPI is considerably more complex than the lowly AVR chip on the Arduino. It's like the difference between a Ford Fiesta and a Boeing 747. Yes, they both have wheels. Yes, they both have a steering wheel. Yes, they both have seats. But you can't keep a Ford Fiesta in the air for very long, and it's very very very hard to drive a Jumbo Jet down a country lane.
Ok, maybe that's a bit of an extreme analogy, but you get the idea - the two are very different devices, and we do our best to make the transition from driving to flying as straight forward as possible. But the onus is on you to realise that you're learning to fly, not just continuing to drive, albeit in a car with these strange things sticking out the sides and 300 people in the boot.
So although much of it looks and feels the same as the Arduino there are some differences. Also the Arduino platform isn't good for teaching you the right way of doing things, and other platforms with better chips have to impose the proper way of doing things because that's what the chips require. One example is I2C.
Arduino lets you use any of the 127 I2C addresses in any way you like. However, the official I2C protocol specifications don't let you do that. Arduino breaks the specifications "just because they can" (and they don't know better). In the PIC32 I2C manual you can find this table (it's also in most other chip manufacturer's I2C manuals):
[attachment=0]i2cres.png[/attachment]
So addresses 7 and below, and addresses 120 and above are reserved. So that only leaves addresses 9 to 119 (or 0x09 to 0x77). The "phantom" addresses you're seeing on the bus are from these reserved addresses.
Another thing Arduino does wrong is teach you that you can use the internal pullup resistors in the IO pins as the I2C bus pullup resistors. Not only that, but it turns them on by default.
This is very very very wrong. Internal pullup resistors are normally in the order of 10kΩ to 50KΩ. This is around 10x bigger than the correct resistors to use on an I2C bus, which should be around 2.2KΩ to 3.3KΩ. Too high a resistance slows down rising edges of the waveforms and can corrupt the data, especially if you have a large number of devices on the bus causing increased capacitance. The worst thing, though, is that many people end up not even knowing that I2C requires pullup resistors, and when they transition to a different platform they wonder why I2C doesn't work, and it's normally because they haven't added the pullup resistors on the I2C bus, or don't know what value they should be using and use a completely inappropriate value, like 100KΩ, because that's what they've read is a good value for pullup resistors in general.
So check the pullup resistors you're using (if any) on the I2C bus.
As for the servo problem - I have seen issues in the past using I2C and Servo together on the chipKIT platform. It's something I've never got to the bottom of, but I haven't looked too deep, since there is an alternative that I always recommend. That is the "SoftPWMServo" library bundled with MPIDE. It uses the task scheduling system of the core timer (a concept foreign to Arduino users) to allow you to use PWM on any pin you like (and as many pins as you like) purely in software, so you're not restricted to just using the PWM enabled pins. It works like a charm.
Oh, and I don't think pin 5 on the ckPI is a PWM pin. It's a little confusing because the chip has "PPS" (Peripheral Pin Select) which allows you to change which pin does what functionality (another Jumbo Jet function missing from the Ford Fiesta), but by default the PWM pins should be set to 0, 3, 8, 9 and 10.
Tue, 05 May 2015 15:56:58 +0000
Ok so my expectations are a little high for compatibility, that's fine.
I'll give that software servo library a try, I see it is in the MPIDE.
Thanks for clarifying I2c. I am not using any external pullups. The raspberry pi does have pullups and they are in fact on always pulling the SD and SCL high. I'll look into that.
The question I need to figure out is if the SDA and SCL on the CKP are 5V or 3.3V. I assume if it trying emulate arduino that it would be 5V but at this point I am not sure. The RPI can only use 3.3V in so I am wondering if that is an issue?
I'm shocked this is being marketed to the arduino community.
Tue, 05 May 2015 18:38:49 +0000
Lets get down to brass tacks on what I want to do:
I want to control servos I want to read PWM pulses I want to communicate via I2C.
This is for a radio control project I am doing. So I need to be able to set interrupts for the reads, and I want the Servos to user interrupts as well so nothing it blocking.
The arduino I have works good, but it has a 4-6 us lag due to interrupt overhead, and I think the chipkit pie being 6-7 times faster will get a much better resolution.
Any ideas on how I can do the above without having to learn a whole new microprocessor? I use C/C++ and not going to do ASM or learn memories addresses etc.. too much work for what I have going on.
If I can get the chipkit to work with SOME investment, I will do it. But if it is going to be a huge undertaking I don't care to try it at this time.
I also do not see the SoftwarePWM library you speek of. EDIT: FOUND IT in the PIC32 directory. Got to figure out how to use it now!
I also just learned that not all the digial pins that are available on the UNO are available on the CKP. D5 being one of them. Might be why putting a servo on that pin doesn't work.
BTW, in terms of a servo, I am just trying to use the "Sweep" sketch, no I2c involved. It does nothing. In another forum someone gave me some jumper setting to check out.
Thanks for your help, I really want to use this and will put in some effort with help. I'll admit I am skewed by the adruino knowledge but I want to learn things the right way and get into real pic programming. I just started with Arduino because they are so easy to get going.
Wed, 06 May 2015 12:30:21 +0000
Quick update, I got the servos working and all my code working EXCEPT I2C. Still working out why the data doesn't come through correctly from the raspberry pi.
I am working on that now and I really like this kit. I do understand what it is about now and how Aruino has skewed me a bit. I've been wanting to get into pic programming for over 10 years, and 10 years ago I had the book "Pic'N Easy" but never got into it. So I started with arduinos and now am ready to take the plunge!