Pen - QuirkyCort/gears GitHub Wiki
Pen
This virtual sensor draws a line in the world to show where the robot has been. Although it is not really a sensor, it still uses a sensor port. It is possible to have more than one pen on a robot; in this case you specify the sensor port numbers so that the pens can do different things (such as drawing in different colors).
Adding a Pen to your Robot
The default Single Sensor Line Follower robot has a pen. The pen is centered between the wheels of the robot. (If you look at the bottom of the robot you will see it - it is a small black pyramid pointing down.)
If you want to add a pen to another robot, you can do this in the Robot Configurator by using the Add button and chosing the Pen component. You may add more than one pen to a robot if you want to track how different parts of the robot move.
Useful locations to put a pen:
-
Pen centered between the two wheels: This is useful for looking at the general motion of the robot, because in this location, the pen does not move when the robot spins in place. (Left wheel at speed x, right wheel at speed -x)
-
Pen located in the same position as the color sensor: This may be useful for looking at the details of line following using a single color sensor. The pen trace will show you where the color sensor was relative to the line.
-
Pen located at the wheel positions: May be useful for looking at how the wheels moved relative to the rest of the robot.
Changing the appearance of the pen trace:
- Pen color: The pen color is set as a combination of red, green, and blue. Each of these values may be between 0 and 1, where 0 is the absence of the color, and 1 is as much of the color as possible. Set all values to 0 to get black, set all values to 1 to get white, or experiment with other combinations.
When using multiple pens, it is useful to use a different color for each pen.
When using a single pen, it can be useful to change the trace color as different sections of the code are running.
- Pen trace width: The pen trace width defaults to 1, but may be set to other values if you want the trace to be thicker or thinner.
Using the Pen (Blocky)
The default Single Sensor Line Follower robot has a pen.
Just add a "Start drawing with pen" block to your blockly code, and a pen trace will be drawn behind the robot as it moves.
The "Start drawing with pen" defaults to port "Auto", which means that if there is a single pen on the robot, the block will chose the correct port for you.
There is a sample program, pen_example.xml, in the samples/blockly directory that shows a robot moving while drawing a pen trace.
Using the Pen (Python)
First, make sure that your robot has a pen. Use the Single Sensor Line Follower robot, or add a pen to a different robot. If you make a new python program when you have a robot with a pen loaded, you should get something like this in the code near where the other sensor objects are created:
pen_in5 = Pen(INPUT_5)
If you are modifying existing code to use a pen, you will have to add this, making sure to use the correct sensor port for your pen.
Once you have a python pen object, start drawing by using the pen.down() method, like this:
pen_in5.down()
There is a sample program, pen_trace.py, in the samples/python directory that shows a robot moving while drawing a pen trace.
Differences from a real sensor
- This sensor doesn't exist for a real robot. You can mount a physical pen or paint brush on a robot, but the virtual pen is more flexible in that you can change the pen color and start and stop drawing while the robot is moving.