Basic stamp encoder program


















My BS2 code is based on a very tight loop that merely monitors a single encoder output. When that output is high, the BS2 signals another, much faster Stamp, else the BS2 signals the faster Stamp that the signal is low. It works very well for the full rpm range that I use on my robot, Ugly Buster : 0 - 72 rpm at the gearmotor. The faster Stamp a BS2p40 is the "Master" and receives information from the encoder-Stamps and, based on that information, sends commands to the Stamps that control the gearmotors.

Well, actually the Master sends commands to the HB motor controllers that, in turn, send commands to the gearmotors. A microcontroller, such as the Stamp, does not have an interrupt system and thus is incapable of halting and resuming an operation in mid-stream and performing another, higher priority operation that may arrive at any time.

I go to extremes. For example, if I had an arm joint, a single BS2 would be devoted to monitoring the encoder that implies the joint angle. This method is far different than most use to control their machines. Eventually, I will migrate to an SX microcontroller. I'll find out. Thanks a lot for your help Bill! The motor that I'm using it's about the same rpm's and it has a cpr encoder, I'm only using channel A of the encoder, trying to make it faster.

The only thing I do with the stamp is count the encoder pulses and send it to the pc using serial port USB , everything else is done in the pc. I was thinking about doing the same, switch to the SX, because the stamp it's just too slow for my application, right now I'm trying with a freescale previously motorola MC68HCQ that a profesor had, it's suposed to be able to handle interrupts, so it might be my solution, but if that doesn't work I'll use the SX.

The inverse conversion, from binary to Gray code , is almost trivial. Shift the binary value right by one bit and XOR it with the original binary value:. State machine: Direction of motion decoder. Here's some BS2 code for a direction-of-motion detector where an object moves past detectors in so as to interrupt the beam signals in a pattern: for one direction of motion and for the other direction of motion.

This code also works for linear or rotary encoder strips see the previous article. This is an BS2 implementation in software of a circuit that I originally made in hardware, shown to the right. The original application was to detect the movement of bumblebees in and out of their nest. The inputs of the quad schmitt trigger are normally pulled to a low level, due to a light source falling on the phototransistors. When an object moves completely through the field of view, it blocks first one and then the other light path, and then unblocks them in the same order.

A pulse occurs on one or the other output, depending on which way the object goes through. On an encoder disk, a sequence of opaque and transparent bands is like a sequence of objects moving through the light path, and the circuit generates a sequence of pulses corresponding to the direction of rotation.

Here is the circuit implemented on a BS2. The phototransistors go directly to BS2 inputs, with pull-up resistors. The voltage at the input pins is LOW until an object moves through the light path. The following BS2 routine samples input port inL once, and then makes the determination of direction from that and from an internal "memory" variable. The trick here is that it is all done with static logic. There are no IF statements.

This makes the execution relatively fast. It is important to sample the two phototransistor signals at exactly the same point in time to avoid false results. The Stamp will have to respond fast enough to "see" all the states in the sequence.

The speed limit depends on how much the stamp has to do before it returns to sampling the signal. As shown it can work reliably up to about hertz--each state has to be stable on the inputs for greater than 5 milliseconds. They stay high for one iteration of the loop. The routine is "self-debouncing", in the sense that jitter back and forth generates either no signal, or both signals first one and then the reverse.

It is straightforward to extend this to as many as 8 separate pairs on 16 inputs of a BS2 without much expansion of the code. The logic works all at once, in parallel, on pairs of bits in byte or word-wide variables. To indicate the directional results, the message byte is output on P8-P15, and to the debug screen. The fancy term with shifts right and left in the following program calculates the cross-terms in x0 and x1.

Observe that this "quad" program is hardly any longer than the program that processes one directional signal. Only 4 lines of program code. Game show, state machine. I'm kind of proud of the following, because it reduces the whole program, from detecting the buttons to flashing the lights, to four lines of program code in a loop, with no IFs or branches. It is a "state machine" that takes advantage of the fabulous capabilities of the stamp2 to address chunks of data as elements of an array.

An explanation follows the program. Matrix keypad scanner as state machine. A matrix keypad is arranged with buttons in rows and columns, so that pressing a button makes an electrical connection between a particular row and column. A key matrix keypad typically has connections arranged into 4 columns and 4 rows, as follows:. In this example a keyswitch is shown "X" closed at the intersection of the second row and second column When closed it electrically connects pin P9 to pin P14 on the stamp.

The 10 kohm resistors are pullups for the inputs, to hold them at a high level unless a switch is closed and a column is made output low under program control. The ohm resistors in series with the pins are there for protection. There might be static electricity, or there might be contention a short circuit if Stamp pins on two sides of a closed switch incorrectly are made outputs of opposite polarity.

Pullups can also be used on the port C pins, but it is not absolutely necessary. This determines which key or multiple keys are pressed at the moment. This is usually followed by a decoding step, to take some action like printing a character or dialing a touch tone or starting a machine, that corresponds to the key or key combination or key sequence pressed. The process of scanning the keypad should be thought of as independent from the decoding.

The arduino community is good, and you might be lucky, but the Stamp community is older, larger, and has Parallax with them. So basic stamp is a bit slower. Which one has more memory? Can they store information when there is no power connected?

This sounds a lot like my goal; learn how to make a robot base first, later add camera, hand, etc. I put together the Tamiya tracked vehicle chassis, and the Tamiya twin motor gearbox geared low, with faster slot car motors. I use the Solarbotics L motor driver pcb kit to drive the motors. You need 2A to drive the low voltage motors. There is a lot of assembly in this parts list, but it is inexpensive. The chief virtue of this approach I think is that you only spend a little money at a time.

But I hope this helps you; it sounds like our approach to robotics may be similar. Sort of. So: x faster than a basic stamp. Both have no-power memory that you can save stuff in. A bit as in significantly slower. I know iRobot has wheel modules now… I wonder if these would be a good cheap all-in-one solution? Yay for me. You should seriously consider adding a few potentiometers to your toolbox though. Just a few 10K or K will do it. They are really god for making experiments with analog input, and for controlling things.

These will allow you to increase the amount of inputs and output on the Arduino board. The good news is that most of them are very cheap. That function is what causes various letters, characters and words in your program to appear in different colors and capitalization schemes.

Syntax highlighting makes your programs easier to read, understand, and correct if there are any bugs in them. If the Reset button on your board is pressed or if you disconnect and reconnect your power supply , the BASIC Stamp will re-run the program you loaded into it.

If a new program is loaded into it, the old one is erased, and the new program begins to run. Milliseconds are thousandths of a second are typically abbreviated ms. By removing the apostrophe and re-running the program, you can then test how the program behaves without the PAUSE. It can also happen if the same program is running as the computer boots while it is connected to a serial or USB port.



0コメント

  • 1000 / 1000