NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Sensors, Actuators, and Robotics » Programming in C to control a Submarine...
March 03, 2010 by Malicious |
I'm currently working on a personal project where I am creating an ROV of sorts, anywho, I currently have the NerdKit set-up to remotely control a small Wal-Mart R/C car from my keyboard. I fooled around with the PWM explained in the iPhone controlled R/C car, and got that working, then switched it over to a simple on/off for all directions. Now, the issue I am having is thus, the submarine I want to control is much higher quality than the car, and the remote uses two ... I'm not even sure what they're called ... devices that contain an axle which turns when a stick on the remote is moved, this allows for extremely accurate speed control with the sub. How can I program the MCU to replicate this behavior? Is this simple PWM, just done with an analog device? Any advice or help would be greatly appreciated. If you have any questions or want a picture of what I'm talking abut just ask. Thank you in advance |
---|---|
March 04, 2010 by BobaMosfet |
Can you do a couple of things, please?
Thank you BM |
March 04, 2010 by Malicious |
BobaMosfet, Sorry about the language, I'm teaching this all to myself an do not have a reference at to what to refer to items as. There is the 'axle' device described. I did some simple googleing and couldn't find an answer. |
March 04, 2010 by Phrank916 |
That axle part looks like a rotary potentiometer which makes perfect sense because pretty much all analog joysticks are made of one or two pots depending on how many axes you need. That joystick looks like its a single axis throttle type. And yes, you are correct, you would use this analog device connected to an ADC pin on the MCU, then take that reading and translate it with a simple equation to the correct range needed to control the top compare of the PWM timer and send the proper signal to control a servo. Take a look at my code for 2 pots(1 joystick) and 2 servos, it's basically just a combination of the servo squirter and the temp sensor code. This should be a good basis for what you ultimately want, it just doesn't incorporate any wireless functionality. Ted |
March 04, 2010 by Malicious |
Ted, Awesome reply, thank you. I may be confused here, but are you saying hook up the potentiometer to the MCU? I was trying to circumvent the pot, and use the MCU to duplicate its output; however this may be the totally wrong way to go about this. My goal was to hook up the transmitter to the MCU without the analog components and use keyboard input to control the sub. I would assume I would trash the potentiometer and use code to duplicate its possible readings. Am I totally off on this one? |
March 04, 2010 by Phrank916 |
Because the pot is an analog device I think its the easiest way for you to get that fine control you're looking for. The MCU as you've already experienced, can receive a digital signal from your computer over uart but the rate at which the changes are sent to the MCU are pretty much constant. I mean you can vary the resolution or amount that each keypress changes the PWM top compare by changing it in the software, but the analog pot gives you the ability to slowly change the number or with a quick movement sweep through the whole range at a very fast rate if necessary. The equivalent of "stepping on the gas", if you will. Making the MCU mimic this in a digital fashion seems like a lot of work to me, but I'm sure with some creative programming it can be done. I'm picturing one keypress that accelerates and then maybe a few diff keys to control the rate of acceleration like 1x, 2x, 5x, 10x, etc., then when you press one of those multiplier keys it makes your acceleration key have a different acceleration rate. I am just still concerned that you won't get that fine control you want with a keyboard. |
March 05, 2010 by Malicious |
Ted, That is exactly what i'm going for. This whole system will be implemented in a kiosk-style environment, where there are only high and low speeds for the rov, I don't actually need the fine control the potentiometer gives. I'll fool around with some PWM in C and see if I can get this to work. Thank you for your help Ted, and I'll let you know of my progress. |
March 08, 2010 by Malicious |
Ok, so I hooked up the NerdKit to the Transmitter and passed every PWM value through it - Nothing. I hijacked the code from the iPhone controlled R/C car and modified it for my purposes using the serial port. I hooked up the 2n7000 transistor to the ground of the transmitter and nothing; from -255 to 255 I get no action from the reciever, I, of course, and a noobie, but I have no idea what the issue is; any idea's? |
March 08, 2010 by Malicious |
After doing some research I concluded that I am not using the correct type of transistor, A few hobby forums were specifying the IRL 2203N or IRF 530 to be used (not for this purpose of course - it was for building speed controls - but it is sort of the same situation), could this be the problem? I plan on trying to track two of these down tomorrow. |
March 09, 2010 by BobaMosfet |
Malicious- Yes, he is correct, it is a trimpot (variable resistor). If you put an ohmmeter on it, you can tell what it's range is. I've done a lot of work with transistors, calculating gain (alpha & beta) as well as identifying the Q-point (for power characteristics), and such. In order to identify the proper transistor, you need to have some electrical information handy about what you want to drive it with, switch it with, and what the load is. Then we can calculate these things and determine the characteristics of the transistor you need- and then you can go find one that fits those characteristics. Let me reread what you've written here and see if that information is already posted. I'll get back to you here for what isn't (or an answer). BM |
March 09, 2010 by Malicious |
BM, Great, I'm not sure what exactly the problem is, and while I realize I may be in slightly over my head, I am determined to make this work and understand it. Just let me know what you need to know, and I'll get it up here. I will also post any images you think will help, wiring, receiver, transmitter, etc. Thank you in advance. Malicious |
March 09, 2010 by mrobbins (NerdKits Staff) |
Hi Malicious, Just to clarify: my understanding is that you want to enable PC or MCU control your submarine, but without replacing the electronics on the submarine itself (receiver-side), and therefore making use as much as possible of the original transmitter hardware, particularly the radio guts. Is that correct? If so, then the goal is to understand how the transmitter is making use of the potentiometer, and replace that in a microcontroller-friendly way. There's a chip inside the transmitter that's actually controlling what gets sent over the radio link, and we have to figure out how that's reading from the potentiometer, and emulate it. Potentiometers as used in joysticks are typically used in one of two different ways: The first way is as part of a voltage divider. This produces an analog voltage that's changes as the position changes. This can work with a three-terminal potentiometer, with perhaps one end connected to ground, one to some known positive supply, and the middle (wiper) would then be an output voltage that changes with rotation. This can also work with a two-terminal potentiometer (one of those terminals being the potentiometer's wiper), with a fixed/known resistance used as the other leg of the voltage divider. The second way is a little more roundabout, but it's certainly one of the ways that this is done, and it actually makes sense for the application. Let me take a step back and explain the setup: Ultimately in RC links, an analog value usually doesn't get transmitted as an analog value over the radio link. It instead gets transmitted as a variable length of time. This means that at each instant in time, the receiver only has to decide whether it's getting a signal or not. (This gets abstracted in some different ways like frequency shift keying (FSK), or others, but it's not too unusual in simple RC control to see simply on-off keying (OOK).) Imagine being in a crowded, noisy room and listening for someone's instructions, encoded as a whistle. If you had to do something proportional to the loudness of the whistle, that would be pretty unreliable as you moved around. But instead of you could just decide whether or not you heard a whistle, and started and stopped a stopwatch, your transmission scheme would be much more robust to interference. RC car transmissions (and lots of other digital transmissions) do basically the same thing, but with a particular radio frequency turning on and off instead of an audible whistle. So ultimately, that little controller chip inside the transmitter is concerned with time periods. Perhaps 1ms means "full reverse" and 2ms means "full forward", and the range between 1-2ms represents the possible intermediate power levels. Now, while we could use a potentiometer as a voltage divider as described above, and use a microcontroller to then determine the time periods (this is totally possibly how your device works -- I don't know!), the other way would be to use the changing resistance to change a time period directly! This is easier than you might think. Our Halloween Capacitive Proximity Sensor video talked about using RC time constants to measure a changing capacitance, but in this case, you can use a known capacitance and a changing resistance to change a time period. This may sound like a roundabout way of measuring the potentiometer position, but since the radio transmitter wants a time period anyway, they could just set up a circuit like the one in our Halloween video above and use that. And for the company making the transmitter, comparators are cheaper than ADCs (on custom silicon ICs -- we're now counting pennies), so it's yet another reason they might do it that way. Just in case you think this is too far-fetched to be the case, the original PC joystick interface worked exactly this way. Each axis was a potentiometer which had variable resistance, and this resistance changed an RC time constant, measured digitally. Anyhow, I'm not sure whether method #1 or method #2 is what's actually going on inside your transmitter device. If it's #1, then PWM and some analog filtering could probably produce an analog voltage that's good enough to feed to the transmitter chip. But if it's #2, then we'll have to think a little harder about how to control it. Can you please post a photo of some of the other guts of the transmitter? Particularly, where do those two potentiometer wires go? Can you use a multimeter to tell if either wire is connected directly to one of the power supply rails (possibly battery + or GND)? Can you measure the resistance across the potentiometer at both mechanical extremes? Do you happen to have access to an oscilloscope? Hope this helps get the discussion on the right track! Mike |
March 10, 2010 by BobaMosfet |
Questions:
If you are looking simply to replace the trimpot with a transistor (essentially), then all that really matters is that you match the output that the trimpot provides and let the rest of the transmitter work as before. BM |
March 10, 2010 by Malicious |
Ok guys, I think I have some good information for you. Firstly the Ohm range of the pot. Measuring on the 1k Ohm scale the min was 10 Ohm and the max was 40 Ohm, as far as the voltage goes, I used the ACV 10 setting, and got a steady of reading of 1/2 volt the whole time. That was also the input voltage. If there was any mistakes I made in getting those measurements let me know. I also have two good pictures here of the transmitter for you. I currently an just working on forward and reverse, so I only focused on that pot. This first image is of the whole transmitter, the pot I am focusing on is in the lower left, the ground attaches to the pin nearest it - if that matters. This is a close up of the pot and where its wires go, while you cannot see the backside of the board, the ground is connected to a general ground on the outside of the board, while the positive is connected to the blue item below it, then to resistor R2 (the bottom-est one) and on to the red item directly to the right of that. Thank you guys very much for all your help. Malicious |
March 10, 2010 by BobaMosfet |
Malicious- GREAT pictures, and thank you. FYI, you might remeasure the voltage through the trimpot-- it has to be VDC (Volts DC). Virtually all digital electronics (and most analog electronics) operate on DC. AC is used primarily for anything where you need a waveform or have to run off of an AC source. There are other cases of course, but I don't want to bury you with information. Thank you again for the great pictures. BM |
March 10, 2010 by BobaMosfet |
Malicious- What is the IC in the photo? PIC? What is the battery/power source for the Joystick... 9V? BM |
March 10, 2010 by Malicious |
BM- What exactly do you mean by an IC? The power source is 12V (8 AA Batteries). Also, I was unsure how to measure the voltage, and after just playing around with the trimpot, I cannot get the multimeter to give me a reading. Below I have an image of the trimpot, and I tried everything to get a reading. Testing lead to lead on the trimpot, and lead to ground, lead to power, etc. What am I doing wrong? |
March 10, 2010 by Malicious |
If IC simply stands for Integrated Circuit, then that is an image of the transmitter's circuitry. If you want a shot of the underside I could easily take one. I also want to say that I thank you guys greatly for the advice. I am trying to teach this all to myself with no real support, so you guys are definitely a great help. Just know it does not go unappreciated. |
March 10, 2010 by BobaMosfet |
Malicious- Yes, "IC" - the 14-pin chip that shows in one of your pictures. It has numbering on it, I'm curious what it is. I'm in the middle of burning a chip right now, so can't take long here. BM |
March 10, 2010 by Malicious |
BM - Ah! My bad, the numbering is actually scratch off, I noticed this earlier when I was trying to do research on the components of the board to figure out what to do. I'll include a pic here for ya, it looks as if it was scratched with sandpaper or something. I've included the only other text on the board along the "top" - right side - there, it says FM-06T-CPCB, but I suppose that is irrelevant, possibly used for identification where ever it was made. Sorry about the late reply, let me know what you think when you have time. Malicious |
March 11, 2010 by Rick_S |
That's unfortunately not too uncommon. Some manufacturers will purposely "sand" off the top of an IC to prevent people from reverse engineering. Most likely Bobamosfet is right and it is some sort of microcontroller. |
March 15, 2010 by Malicious |
So what is the best method I should use for trying to mimic the rotary potentiometer with the NerdKits MCU? I have been unable to get the voltage for the pot, I'm going to get a digital multimeter later this week, but I did get the resistance hi/low, 100 to 9000 ohms. I suppose I should just find the voltage hi/low and then I can use PWM to mimic the rotary pot? Sound correct, or am I only dreaming it will be this easy? |
March 16, 2010 by mrobbins (NerdKits Staff) |
Hi Malicious, The 100 to 9K ohm range is a good start. As I tried to explain in my earlier post a few days ago, PWM may or may not work depending on exactly how the potentiometer is connected and used in the circuit. Is it correct that one of the two potentiometer wires leads to the overall circuit ground (i.e. the lowest voltage point in the circuit, being the negative end of the AA battery pack)? If so, you might be able to use a 2N7000 MOSFET as a switch. Put the 2N7000 source terminal to ground, gate terminal to a microcontroller PWM output, and on the drain terminal, attach a 100 ohm resistor. The other end of the 100 ohm resistor would go to whereever the other potentiometer wire goes now. Then, adjust the duty cycle, and you could vary the "effective" resistance between 100 ohms (plus a few extra ohms for the 2N7000 itself) and infinity. However, please note that I am not very confident about this working, because I think there's a good chance that there's some time-dependent behavior in your transmitter that the PWM won't properly emulate. Crank the PWM frequency as high as you can! Also, if we knew just a little bit more about the circuit it's connected to, I could suggest a digital potentiometer IC, such as the MCP4251, which for about a dollar contains two digitally-controllable potentiometers. However, there's a big limitation, which is that the voltage at the potentiometer terminals must stay within the 0-5V range. Without knowing more about the circuit you're connecting to, it's hard for me to know that that will indeed be true. I'd recommend that you give the PWM method I describe above a quick try. However, I'm not confident about it working, so I'd suggest that you go further and try to investigate whether the digital potentiometer IC solution might work -- i.e. how high does the potentiometer voltage get during operation? BTW, great project! Mike |
March 16, 2010 by Malicious |
Mike, Before I blew the fuse on my analog multimeter I got the resistance and was fooling around trying to acquire the voltage. My readings were consistently inconsistent. :p The overall voltage seemed to be 5v (when my multimeter was set to the DCV 250 range), but when switched to the DCV 10 range, the reading was 1/4v (the needle moved the same amount). I was testing some different situations to see what I did not understand about the multimeter when I blew it up. So, in short, I'll have the voltages as soon as I get time to run to the store. As far as the potentiometers wires, yes, the ground does run to the overall ground, while the other runs from a resistor to the pot. This did not make sense to me, because when creating my proof-of-concept model for this project (I used a $10 Wal-Mart R/C car), the ground wire carried the 'information' from my MCU to the transmitter. Such as in the iPhone R/C car you guys did, you ignored the + wire on the transmitter. This seems that the + wire is the one 'carrying the information' if you will. (Hope my analogies are not confusing) I will set-up the PWM scenario you described tonight and fool around with it for a bit. I ill be sure to let you know of my results. If I were to to use the other method you described, what would you need to know about the overall transmitter circuit, besides the voltage I have yet to measure? |
March 18, 2010 by mrobbins (NerdKits Staff) |
Hi Malicious, How did the PWM test go? The voltage at each end of the potentiometer (well, we know one is tied to ground) is the most important thing if you want to go via the digital potentiometer route. However, based on my first post on this thread -- plus just looking at all of the capacitors nearby the potentiometer connection -- I think it's likely that there's some time dependent behavior going on here. What's really important to know is the maximum voltage at the other side of the potentiometer at any point in time. Mike |
March 23, 2010 by Malicious |
Mike First of; sorry, my friend, for the radio silence; Its finals week and a few projects needed to get done. Anyway, I found few hours today to sit down and continue working on this. I hooked up the 100 Ohm resistor and Viola! it worked!! Amazing. I wrote a program that allowed me to choose any integer from -255 to 255 and use that as the PWM; anything above 0 was forward, below 0 was reverse and 0 was off. I will have to do some more experimenting (I'm writing this after only a few minuets of toying around, I just had to let you know it worked) but at first glance it seems the propellers are 'stuttering', if you will. At +/- 255, which I assume would be full-throttle on the joystick they ran, what seemed to be, fine, but at any other integer they did not run smooth; I just do not want to ruin the propeller motors seeing as how I cannot replace them. I'll do some more experimenting and let you know. Is this a valid concern? and what timing issues are you referring to specifically? If I understand correctly the only place timing is involved is with actually sending the signal from the transmitter to the receiver, a process I am not trying to fool with...just yet. |
March 23, 2010 by Malicious |
So, here is a question for everyone: I wired up everything to test the system out. I wires the left/right control the same as the forward/reverse that worked in my test. I have a LED hooked up to PC4 on the NerdKit MCU as an on/off light, and the controls are hooked up to PD5 for L/R and PD6 for F/R. Now here is the issue. I am powering the entire operation, transmitter and nerdkit from the 8AAs in the transmitter. I spliced the power and ground wires from the batteries and ran them to the nerdkit, running the power through the voltage regulator that came with the nerdkit so I don't destroy anything. Here is the issue. Code compiled, installed on MCU, everything is off; I plug in the power wire the nerdkit, the green light comes on, the Putty serial application registers the unit turned on, all good so far, I turn on the transmitter, the Putty application displays a strange symbol (nothing i've every seen before, ascii or otherwise) and the nerdkit's Led turns off. As soon as I turn the transmitter off again, the nerdkit light returns to on, and the putty application displays another strange character. Any idea as to what is going on here? I will gladly post images if requested. I figured there was not enough voltage to go around and since I don't have any more AAs I wired a 9v battery between the positive on the transmitter and the nerdkit - thinking the nerdkit would use the extra voltage, but I still have the same problem. They do not both stay on... All help is appreciated, thank you in advance. Aaron |
March 24, 2010 by BobaMosfet |
I guess, a question I have is-- how much current does the transmitter draw, and the nerdkit? The 7805 can only deliver so much. And are you powering the motors off of the 8AAs as well? BobaMosfet |
March 24, 2010 by Malicious |
The transmitter is drawing 100mA at 250m setting on the multimeter, so I would assume this is 25000mA or 25 Amps. The nerdkit is drawing 90mA at 250m so I would say 22.5 Amps. I figured out the on/off issue, I had the ground wire from the 2n7000 mosfet unplugged, causing some type malfunction, once plugged into the ground rail both boards ran simultaneously. My dumb mistake ;p I hooked up everything correctly and wrote some simple code to allow me to vary the PWM for both controls (F/R & L/R) from -255 to -200 and 200 to 255. No here I ran in to some issues, nothing happened. Period. Once or twice the propellers spun erratically but i assume it was some type of interference seeing as how I could not replicate it. So, I rewired the L/R control back to the trimpot thus isolating the F/R control. Ran the test again and I could make the propellers spin erratically on will, a value of 235 caused this. Now, I could not get them to spin 'in-phase' if you will, so my question is, would this have anything to do with the phase-correction programmed in C? I have been using the code from this site used in the R/C car demo: // Timer0: phase-correct PWM TCCR0A = (1<<COM0A1) | (1<<COM0B1) | (1<<WGM00); to initialize this. Would someone explain what exactly this is doing? Well, you don't have to explain it, I assume it is involved, at least supply a source where I could read more. If this is not the problem, and it is something to do with lack of voltage or current, are their ways I could determine what is causing this erratic motion? |
March 24, 2010 by BobaMosfet |
Can we see a picture of your meter? '250m' doesn't make sense, unless it's in 'amps'. Is there some fine print on it that says 'Measurements in Amps'? Your NerdKit is going to draw about 90mA (the 22.5 Amps is erroneous... please answer the above :P ). Also, do the LED fix I mentioned to you elsewhere, it will help. Since you don't have an oscilloscope, I'd use another LED (red or something and also with it's own resistor) and connect it in parallel to your PWM circuit. That might atleast let you see what the PWM circuit is doing by dimming or lighting the LED differently depending on control position (giving you feedback). Your problem is lack of feedback to help you logic out the problem. TCCR0A - See section 14.9.1 in the ATMEGA168 Datasheet. Hope that helps BM |
May 03, 2010 by Malicious |
Hello everyone, sorry for the disappearance, my first submarine took a dive - excuse the pun - and I had to make another one from scratch. Now that that is completed, its back to the nerdkit. So here is my current issue. If you bothered to read all the posts above or were helping me before, you know I have a receiver submarine with two rotors, a transmitter controller with a Forward/Reverse and Left/Right control stick. I attempting to use the nerdkit to mimic the potentiometers the transmitter employs. Currently I have the Forward/Reverse Control isolated on the transmitter, and am using part of the Pulse Wave Modulation code from the iPhone R/C car project to send command via the serial port. The issue I am having is that the rotors rotation is not smooth, it is very choppy. I almost feel like the PWM is not mimicking the trim pot the way I want. Am I going about this the correct way? Should I be using PWM? Do I have to modify the PWM code for my purposes? If so, could someone please explain the PWM code to me? Thank you for you help. Malicious |
May 03, 2010 by hevans (NerdKits Staff) |
Hi Malicious, How exactly are you sending the PWM signal, and what PWM signal are you sending? Have you tried writing some code to increase the PWM duty cycle one tick at a time and see how your rotors react? Humberto |
May 03, 2010 by Malicious |
Hello Humberto, This is my code, I copied some of it from you guys in your iPhone R/C Car Source Code, and do not understand how to modify the duty cycle.
That is the code simplified, I have functions for the actual actions, and I can choose any PWM value from -255 to 255. I would assume the Timer0 and Timer2 lines would need modified but I am not sure exactly what that code means. If there any type of api for this? Thank you for your help. |
August 07, 2010 by BobaMosfet |
Here's the other thread on this project. http://www.nerdkits.com/forum/thread/746/ BM |
Please log in to post a reply.
Did you know that interrupts can be used to trigger pieces of code when events happen? Learn more...
|