AccShooter

AccShooter is a remote controlled cannon firing plastic balls. The robot is so unique because of the way it is controlled. The turret uses acceleration sensor (project site) (no buttons !) to aim.

The design was taken from HiTechnic site (link). It is simple but very functional. Building instruction can be downloaded so I won't include it here.

I obviously didn't use HiTechnic acceleration sensor. Instead I used mine (link). I also added my bargraph (project site) to display remaining ammoun of bullets. As you can see I used both of my sensors in this simple project. In this video you can see that both work perfectly. My accelerometer works as good as original from HiTechnic.

Now, the program. I have to point out that I did NOT used or even look at the implementation from HiTechnic website. I like to write my own programs and learn something instead of copying someone else's work. It's your call whether my program is better that theirs.

I obviously post my code below. I will try to comment on it briefly. At first I include the library (link) to interface with the accelerometer. Then I define a couple of constants and the first task. It will monitor the trigger button on the remote and shoot a ball if pressed. After one shot it updates the bargraph on which I display the amount of remaining bullets.

Moving on, there is the main task. In the beginning I initialize all sensors, set the turret's motors to regulated mode, assign priorities to tasks and start the trigger task. In the main loop I read accelerometer readings and calculate pitch and roll angles. Both values are displayed on the screen. Then there is some magic math. I calculate two angles for two motors controlling the turret. In the end I set those motors to desired positions.

I must admit I spent some time trying to find the way to count accelerometer's raw values to motor's angles. The solution I came up with is simple enough for you to figure out from the code (fingers crossed).

Testing took me a couple of minutes. I fine-tuned some numbers. I came across some errors while communicating with the accelerometer but I fixed that quickly.

In the video you can see the final effect. I find it the most satisfactory. Judge it yourself if my program / sensor is better than HiTechnic's.

Video

Gallery

3D Gallery

To watch pictures in this gallery you need special red-cyan anaglyph glasses. They are to buy in many Internet stores for less than 1$.


Program code

//author: Krzysztof Kapusta, All rights reserved #include "accmag.nxc" #define TRIGGER S3 #define ACCSENSOR S2 #define BARGRAPH S1 #define AIM_MOTORS OUT_BC #define AIM_LEFT OUT_B #define AIM_RIGHT OUT_C #define SHOOT_MOTOR OUT_A #define DEG_AIM_MAX 150 byte bar[] = {0x40, 0x7f}; task trigger() { I2CWrite(BARGRAPH, 0, bar); while(1) { if (SensorBoolean(TRIGGER)) { RotateMotorEx(SHOOT_MOTOR, 100, 360, 0, false, true); bar[1] >>= 1; I2CWrite(BARGRAPH, 0, bar); } } } task main() { SetSensorAcceleration(ACCSENSOR); SetSensorTouch(TRIGGER); SetSensorLowspeed(BARGRAPH); PosRegSetMax(AIM_MOTORS, 20, 5); priority main, 50; priority trigger, 2; StartTask(trigger); int prev_p = 0; int prev_r = 0; PosRegEnable(AIM_MOTORS); while(1) { VectorType vec = ReadSensorAccScaledEx(ACCSENSOR, ASCALE_2G); float pitch, roll; pitch = -atan(vec.Y / sqrt(vec.X*vec.X + vec.Z*vec.Z)) * 180 / PI; roll = -atan(vec.Z / sqrt(vec.X*vec.X + vec.Y*vec.Y)) * 180 / PI; ClearScreen(); TextOut(0, LCD_LINE5, FormatNum("PITCH %.2f", pitch)); TextOut(0, LCD_LINE6, FormatNum("ROLL %.2f", roll)); long p = (pitch+20) * DEG_AIM_MAX / 35; long r = roll*3; if (p < 0) p = 0; else if (p > DEG_AIM_MAX) p = DEG_AIM_MAX; if (abs(p-prev_p)+abs(r-prev_r) < 6) continue; long left, right; left = p + r; right = p - r; if (left > DEG_AIM_MAX) { right -= DEG_AIM_MAX - left; left = DEG_AIM_MAX; } else if (right > DEG_AIM_MAX) { left -= DEG_AIM_MAX - right; right = DEG_AIM_MAX; } PosRegSetAngle(AIM_LEFT, left); PosRegSetAngle(AIM_RIGHT, right); prev_r = r; prev_p = p; } }

Show/hide all the code

Comments

Szacunek

Posted by: katon | 2013-11-22

Dobree xD

Posted by: BekuĊ› | 2013-10-27

Post your comment