Hi Fallentine, I will try a couple of quick answers before I have to go do some other things.
All of the IO lines are TTL level pins except for the ones that go to the serial adapter which are refered to as S_IN and S_OUT. You can use the standard serin command on any of the IO pins P0-P19. You may need to experiment with n9600 and i9600 as sometimes different vendors have different ideas of what is inverted or not.
Pins 14 and 15 are special in that you can use the Hardware serial port that is built-in to the underlying microprocesor through the hserout and hserin commands.
I don’t think you need the One Wire interface. Unless of course the device you are trying to connect uses it.
I am not 100 percent sure I know exactly what you exactly want here. There are several possiblities. But I will write it to assume that you want the servo to start out at 0 and if one button is pressed you wish for the value to increase and if you press another button you wish for it to decrease and if you press a third button it will center it.
Now for some confusing parts. My assumption is that you don’t want to increase each time through the loop as will do this somewhere near 100 times per second, so I will assume that you only want to do that action once per press. Also for the fun of this I will change some of your code to use the three inputs from pins 12,13,14 as these coorespond to the buttons that are built into the BotBoard 2.
In fact I will extract the code from the center servo program that is part of one of the projects up on the web (Rover with Arm).
enablehservo
buttonA var bit
buttonB var bit
buttonC var bit
Changed var bit
var1 var sword
prevA var bit
prevB var bit
prevC var bit
input p12 ; Tell the system that these IO pins will be used for input
input p13
input p14
var1 = 0 ; initialize it to some value
Changed = 1 ; say that something changed
main:
prevA = buttonA ; Remember previous state of buttons
prevB = buttonB ; so we will only process the press once
prevC = buttonC
buttonA = in12 ; Get the current state of the three buttons.
buttonB = in13
buttonC = in14
if (buttonA = 0) AND (prevA = 1) then ; Button pressed now but not last time?
if var1 < 5000 then ; only increment if we now we are below maximum
var1 = var1 + 100
Changed = 1
endif
elseif (buttonB = 0) AND (prevB = 1)
var1 = var1 - 200 ; decrement by our decrement count
if var1 < -5000 then ; and then check to see if we went too low
var1 = -5000
endif
Changed = 1
elseif (buttonC = 0) AND (prevC = 1)
Changed = 1
var1 = 0
endif
; Only have to tell Hservo when something has changed.
if Changed = 1 then
HSERVO P4, var1, 255]
Changed = 0 ; Done processing the changed state
endif
goto main
I have not compiled this code and there could be some problems, but I hope I answered the right question. If instead you wanted the program to automatically increment/decrement the counter, but have the buttons jump it around you could do more or less the same, except add a line after main: line for a for loop. Something like:
for var1 = -5000 to 5000
You may want to put an increment on it, maybe 10?
for var1 = -5000 to 5000 step 10
Also you would add a line before the Goto Main, that was:
next
You could then get rid of the Changed variable processing as it would change everytime through the loop. Also you may wish to adjust the timings as you don’t want to endlessly output Hservo commands without giving them some time to be processed. you could add a pause statement within the for loop, or you could use the HSERVOWAIT command before the HSERVO command to wait until the previous command has completed. I hope some of this makes sense.
I think your example explains what i needed to know.
It’s a bit complicated, but that’s ok. I’m here to learn.
Basically what i want to do, is when i turn on my bot, the var will set to 0, i can do this by putting it before the main body with a nice little pause.
Then if i hold buttonA 1, for 1 second, it turn in the specified direction until i let go. And then it would hold there. IF i press buttonA again it’ll keep moving from where it left off.
And if i press buttonB it would go the opposite direction and when i let go it would stop and hold.
And if i press buttonC it would return to it’s default position. Which probably wont be zero, but i dont have it here yet so i dont know the exact position i need.
But i’m visualising the effect your code would have and i believe it’s correct. Thank you. And thank you for leaving notes to explain each step, otherwise i’d be lost.
I’m making a rover with an arm too, and i needed controls for the arm.
Gonna put the light weight, low light wireless camera right on the arm. Gonna use a 1.2ghz camera, not suppost to, but everything else in my house is 2.4ghz, and my router is so powerful i dont see any other choices.
Edit:
PS
We both had a problem, the manual said to use it like this
HSERVO P4, var1, 255
You used it like-
HSERVO P4, var1, 255]
But when compiling it, it gave me an error for a while, it’s really suppost to be. I’m guessing it’s just the different versions of the IDE.
HSERVO [P4\var1\255]
You are welcome, sorry for the mistyping of the hservo command. As I mentioned I did not compile it…
To control your robot from a PC using a serial commands there are lots of options. You can start off with simply using the serin command on S_IN and you can output stuff to the pc using serout on S_OUT. I do the serouts reasonably often when I am debugging my code. Serins from the port can work just fine, but there are complications. Sorry in advance of my complicated explanation, but: The serin and serout functions are quite often described as bit bang functions. That is they have no hardware support, they simply loop in their own code and generate the appropriate pulses on the serout, or look at the pulses at the appropriate time to see what is on the IO line in the serin command. This works fine, except: For serin to work properly, your program must be sitting in the serin command when the data is sent to it, otherwise part or all of it will be lost. The problem is that you don’t necessarily want your program always sitting in a serin command, but you would rather it had some time to do other things like to check buttons, or calculate where the arm should move to… So you need to have some protocol that allows the BAP and the PC to synchronize to send commands. There are several ways of doing this. The BAP can send a byte to the PC that says I am ready, and the PC has a fixed amount of time it can respond with a new command before the serin on the BAP times out. An example of this can be found in the code that the program POWERPOD generates to control a hex robot using serial input from the pc. Here is part of that code:
main
;SerialQuery
serout S_OUT,i9600,"Rd"] ; 'Ready' to recieve data now
serin S_OUT,i9600,100,NoData,[str DualShock(0)\7]
CheckSum = DualShock(1) ^ DualShock(2) ^ DualShock(3) ^ |
DualShock(4) ^ DualShock(5) ^ DualShock(6)
;serout S_OUT,i9600,[CheckSum]
if CheckSum <> DualShock(0) then
Sound 9,[50\2000]
NoData
DualShock(1) = $FF
DualShock(2) = $FF
for Index = 3 to 6
DualShock(Index) = $80
next
endif
The above code sends two bytes to the PC(RD) and then waits for a 10th of a second for data. If it gets data it generates a checksum from the data and compares it to the checksum that was sent to it to make sure the data was not corrupted. It also shows the proper format of S_IN and S_OUT usages.
An alternative to this protocol is to use an interrupt on the BAP (IRQ0), that is a little complicated to start off with. If you are interested you can read more about it in this thread:lynxmotion.net/viewtopic.php?t=3496
Alternatively you can use HSERIN and HSEROUT, which are on on IO lines P14 and P15 and have hardware support that take care of buffering the input. The problem is that P14 and P15 are TTL level signals and your pc wants RS232 level signals. Again couple of options here. You could use a converter circuit and plug. Sparkfun sells breakout boards and adapters that allow you to convert the TTL level signals to either RS232 or to USB.
Alternatively you could hook up a bluetooth interface between your PC and BAP. There are several threads on hooking up a bluetooth. Sparkfun sells several different versions.
I hope I indirectly answered most of the questions.
Thank you, appearently i can make an RS-232 to TTL converter (for commands from PC to the MCU) with 2 resisters and a transistor.
Which i’ll try that first, i have extra serial cables that i yanked off a Dex Drive. And i got some resisters from some old junk in my closet.
And my roomy will pick up the 2n2222 transistors on her way home today.
Then i’ll just use hserin.
Thanks again. And the hservo thing isn’t your fault, you copy/pasted mine i think, and i copied the layout from the atom pro manual. Which was outdated.
I’m sure i’ll have more questions, i’m kinda stuck at the point where i can’t program much more without my bot in hand.
Is this how to make a simple RS-232 to TTL Transfer cable?
I know it’ll only send data, and not recieve, but i only need to send commands for now, i wont have a normal converter for a little while.
And if robotshop.us is any good they’ll have sent my bot out last week and it should arrive today.
Edit: just checked and it says it’s still pending.
Hmm…kind of upset they hadn’t shipped it yet. But it gives me a chance to change my order since for a dollar more i can get the better pololu serial motor controller which allows for higher frequencies just incase. So no whining noise.
two small concerns with your circuit. first usually there is a small diode (like a 1n4148) in series with the 10K resistor to prevent driving the base significantly below the emitter. second the lack of a pull-up resistor between the collector and 5V may significantly limit your baudrate as you are relying on the internal pull-up of the port pin which is typically 100K or more.
feel free to edit the picture to show the changes as i’m not 100% sure what you mean.
I’m using the basic schimatic from another place.
It was intended to control a pololu micro serial motor controller, but i tried to modify it for the bot board.
I wan’t to use the hserial because the S_IN pin looks like it would be a pain in the butt.
Update: Robotshop.us did send it out already, sigh means i can’t change it to the slightly better controller. Controller cost 1$ more, but would have brought the shipping down 2$ oh well.
well she already has the 5V on the bot board connector so all the 7805, diodes, and cap stuff are not needed. the 10k resistor between the +5v and the collector of the npn transistor is the pull-up I suggested in part 2 of my reply. the series diode I mentioned in the first part is mostly for protecting the transistor and increasing the noise immunity of the input. it is not “necessary” but were I building the circuit I would put it in.
I posted my diagram for the rs232 portion. I don’t know what pin 14 is, so all that is probably needed for Fallentine’s diagram is a +5v power source that is connected to a ~10k resistor, the ~10k resistor connected to the transistor collector, and the board serial input line connected between the ~10k resistor and the transistor collector. The rest of the stuff in my circuit is for chip brownout protection and providing 5.7v instead of 5v to the servos for “snappy” servo operation.
I removed the Rx Part as i dont need that. I’d use the normal botboards serial port to program it, but i need to use the botboards pin 14, which is hardware serial in. To send control bytes to it.
I have capacitors, i have some resisters, and but a very limited selection of transistors. Again i have no clue what kind of diodes i have. I’m tight on cash, so i gutted a burned out PC power supplies, an AM/FM cassette alarm clock, and some PC speakers. I just desoldered anything that looks in working condition. But the diodes, i just can’t read them.
I’m buying a max232 equipped converter, but that wont be for a little bit so i just need to make my own RS-232 Tx to TTL Rx adapter.
Zoomkat thanks for the diagram but i can’t make sense of that.
As I said there are others who know much more about the electronics than I do. If it were me, I would simply purchase it from someone like sparkfun: sparkfun.com/commerce/product_info.php?products_id=449, that is I always worry I will screw up which pin is which on things like transisters
Fallentine, my rs232 to TTL setup is the same as yours with the exception I don’t use the diode (D1) and capacitor (C1) on the tx line and don’t use the 10k resistor (R2) on the transistor base. I just use R1, R4, and Q1. My setup runs at 9600 baud, so I can’t speak to higher rates. Below are the parts I get at the local Radio Shack store.
Thanks zoomkat i’ll probably just make one of those. Since that’s the transistor pack i have.
I’ll try it with one of the diodes i have here, And i only need 9600 baud, 'd probably get by with 1/4 that, i’m only sending simple bytes, digits 0-20.
Edit:
Edit again: removed previous edit, i fixed my problem.
Problem was
if bot >=2 and <= 7 then
Which didnt work
I tried seperating it, which did work, but made it more complex.
i fixed it with
if bot >= and bot <= 7 then
just added a second “bot” variable.
So your post up on BasicMicro as well. I believe you are doing something like:
hserin [dec x]
The question is what is your input string that is being passed from your pc to your bot? Are you passing a an ascii value text string? If so how is it terminated? I believe with the straight dec command it will want some character outside of the value “0” to “9” to terminate it. If it is a fixed length like 2 characters you might try something like:
hserin [dec2 x\2]
This says that you are expecting a minimum of 2 digits and a maximum of 2 digits (ie you are expecting 2 digits).
Now instead if you are passing binary data down from the PC, you should simply put it something like:
hserin [x]
Now it will only assign one incoming byte to the variable X, regardless of how big X is. If you are sending multiple bytes, you may have to play games with HIGHBYTE, LOWBYTE…
As for your IF, not sure what is not working? Is this code being executed at all? If so one branch or the other should do something. But not sure from your description.
I have been trying something similar. I’m using labView to coltrol my rover, but when I’m trying to figure out code I use docklight to send serial data. I had trouble getting the BAP to parse the hex string. Switching to 3 and 4 digit octets that stay in a fixed format is the only thing I have been able to get to work.
Here is my code, maybe it can be useful to you. You should be warned that my math is bassackwards, when I came up with this I had/have no clue about what I was/am doing.
;-------------------------------------------------
; Logitec Dual Action Game Pad
; LabView 8 student ed.
;
; LabView sends concatenated serial a string of 4 3 digit octets for axis
; 2 4 digit octets for buttons and direction pad. Axis values are unsigned from 000 to 401
; (math in LV still needs refinement and trim function added). When sticks are centered, value
; is 200. (The string was orininally hex and was changed because I couldnt figure out hor to
; get the BAP parse and convert the hex strings to signed decimal).
;
;
; hervo uses pin 3
;
; Pin 8 (LED) indicates hserial timeout
; Pin 9 (led) indicates buttons 1 and 2 pressed at same time
;
;
;
;
ENABLEHSERIAL
sethserial h9600,h8databits,hnoparity,h1stopbits
enablehservo
HSERVO [3\0\0]
main
high p8
;variables
id var byte ; string source
lx var long ; left X axis
ly var long ; lefy Y axis
rz var long ; right X axis
rr var long ; right Y axis
btn var long ; buttons
dir var long ; direction pad
servoL var long ; left servo
hserin 500, tmout, [id,dec3 lx,dec3 ly,dec3 rz,dec3 rr,dec4 dir,dec4 btn]
servoL = (lx -200)*30*2
HSERVO [3\servoL\0]
serout s_out, i9600, "left X ", sdec servoL\5, 13]
if btn =12 then
high p9
else
low p9
endif
goto main
tmout
serout s_out, i9600, "connection timeout",13]
HSERVO [3\0\0]
high p8
pause 100
low p8
pause 100
high p8
pause 100
low p8
pause 100
goto main
Oddly enough i prefer to use my home made NPN transistor serial data cable over my $10 RS-232 to TTL converter.
Only problem i have now, is my control program itself, a terminal type program which i use to control my bot.
i can’t make it tick, or repeat signals for as long as i hold down a button.
But that’s ok, i can live without it.
I bound it to one of my pc game controllers, so now all i need is a blue tooth to UART adapter so my robot isn’t wired to my pc.