Shop Learn P1 Docs P2 Docs
Pixycam2 line tracking — Parallax Forums

Pixycam2 line tracking

SteffeD1SteffeD1 Posts: 14
edited 2022-04-17 16:06 in Robotics

Hi,
Have anyone get Activitybot 360 to perform line follow with Pixycam2?
Are there a method to do odometry while Activitybot driving at speed command?
Happy Easter!

Comments

  • TCIIITCIII Posts: 81
    edited 2022-06-01 18:44

    @SteffeD1,

    I purchased a Pixycam2 v2.1 off of Amazon and started testing it recently.

    Pixymon works well and is quite comprehensive for setting up and programming the Pixycam2.

    I have run ccc 'hello world' on an Arduino Uno R3 after programming the Pixycam to recognize a yellow tennis ball and all went well.

    I have built a custom Arlo based on NikosG's "Artist" robot chassis except I am using the old "Eddie" Motor/Wheel assemblies with the new "Arlo" encoders and have the motors mounted in the middle of a 14" square chassis with caster wheels at each end.

    I was thinking of using the Pixycam2 with the custom "Arlo" chassis. NikosG's "Artist" robot chassis acts just like an ActivityBot and I have ported the ActivityBot software to my custom Arlo robot.

    There is a good Parallax tutorial on using the Pixycam2 with the ActivityBot360, but it involves colored blocks and is written in BlockyProp.

    I was thinking about porting the Arduino C code for line following with the Pixycam2 to my Propeller Activity Board on my custom Arlo chassis.

    Comments?

    Regards,
    TCIII

  • TCIIITCIII Posts: 81
    edited 2022-06-01 20:17

    @SteffeD1,

    I believe that the attached file can be ported to ActivityBot PAB for line following.

    It is written for an Arduino Uno R3 communicating with a L293N motor controller so the following changes must be made for a start:

    1) The Arduino uses SPI while the PAB uses UART to communicate with the Pixycam2.
    2) Convert Arduino motor control to ActivityBot motor control.

    I have also attached some Pixycam C code for the PAB. Unfortunately I can't load libpixy2.side

    Regards,
    TCIII

  • Thank You! I will look into this later. There are also some code in downloads to https://parallax.com/product/pixy2-cmucam-with-cable-mounting-hardware/
    Regards,
    SteffeD1

  • TCIIITCIII Posts: 81
    edited 2022-06-02 14:27

    @SteffeD1,

    Your link is where I got the C code that I posted above.

    It is interesting that Parallax has not provided C code for the Pixycam line following mode.

    I guess that they decided to stick with QTI line following tutorial.

    Regards,
    TCIII

  • TCIIITCIII Posts: 81
    edited 2022-07-01 16:44

    @SteffeD1,

    I have done some serious research concerning generating C code for the Propeller Activity Board (PAB) to do Line Following with the Pixycam2 and have found that it might not be worth the effort.

    I have examined the PAB colored object C code (30028-Pixy2-Demo-C-Code-2-24-2020), written by Jon McPhalen back in 2020, where he created a pixy2.h library and C code, libpixy2.c, to identify colored blocks.

    To be able to switch the Pixy2 from the "CCC" mode to the "Line" mode, to do Line Following, requires a pixy.changeProg ("line") request function as well as a pixy.init() request function whose formats are not available in the Charmed Labs "Pixy2 Serial Protocol - Packet Reference" tutorial. I have requested information concerning the pixy.changeProg ("line") request function as well as a pixy.init() request function on the Pixy Forum, but don't expect a response. Charmed Labs appear to be locked into the Arduino and Raspberry Pi when it comes to C code examples and probably could care less about the Parallax Propeller.

    As an alternative, I have pursued this course of action:

    I have managed to rewrite and compile the Charmed Labs Zumo robot line_zumo_demo.ino program to send the Zumo motor speed control commands, after being suitably adjusted for Arlo, serially to the PAB to control Arlo's motors.

    I have written and compiled a Propeller C program that receives the left and right motor speed values, using serial.h, and uses drive_rampStep(left, right) to control Arlo's motors speed and direction.

    Comments?

    Regards,
    TCIII

  • @SteffeD1,

    I found this very interesting Masters Thesis, written in 2015 before the line following app was integrated into the Pixycam, that used a Zumo robot chassis, Pixycam, and a QTR Front Sensor Assembly. The thesis paper dealt with traffic flow for driverless cars.

    Since the paper was written before the line following app was integrated into the Pixycam, they used the Zumo robot QTR Front Sensor Assembly for line following and the Pixycam for maintaining a suitable distance from the robot that was in front of the test robot chassis.

    Unlike some thesis papers, they included the Arduino C .ino code and a very detailed description of how the code was built which is very impressive.

    Since the Charmed Labs PIDLoop.h library was not available at the time, they developed their own ServoLoop class which only uses P and D components compared to the Charmed Labs PIDLoop.h library which used all three of the PID components.

    The explanation of the code development is very detailed and has given me more insights into the Pixycam line following and object detection/tracking algorithms.

    Comments?

    Regards,
    TCIII

  • ercoerco Posts: 20,195

    Somewhat related: IMO one the most intriguing (and frustrating) promise of driverless cars is the elimination of stoplights at intersections on surface streets. Cars all talk to each other, managing speed, sequence & separation to move nonstop through intersections north/south/east/west. AKA AIM, or Autonomous Intersection Management, see video below at 1:00. The frustrating part is that 100% of the cars must be self driving.

  • TCIIITCIII Posts: 81
    edited 2022-06-20 16:14

    @SteffeD1,

    One of the problems that I have encountered attempting to port the Charmed Labs Zumo Line Following demo Arduino C code to be used by the Propeller Activity Board on my Arlo imitation ActivityBot is that the SimpleIDE compiler seems to be unable to compile header .h files that don't have an accompany .c file.
    The following Charmed Labs Arduino .h files have the C code embedded with each header in the .h file and the Arduino IDE complies these files without error:
    Pixy2.h
    TPixy2.h
    Pixy2CCC.h
    Pixy2Line.h
    PIDLoop.h

    However the SimpleIDE compiler chokes on these header files. So my approach has been to modify the line_zumo_demo.ino code to transmit left and right motor speed commands to the Arlo-ActivityBot over a serial link and use drive_speed(left, right) to let the Pixycam2 control Arlo-ActivityBot to follow a line.

    I have validated the left and right motor speed commands output to the Arlo-ActivityBot when the Pixycam2 is in the line following mode and is looking at a piece of white paper with a 1/4" black line drawn down the center. When I move the Pixycam2 to the right and left of the black line, it transmits the correct motor control speeds (0-64) that will bring the Arlo-ActivityBot back to the center of the line while moving forward. Works great on curves too. When the Pixycam2 comes to an intersection, it slows down to a speed of 24 to see if it should turn right or left depending on a barcode symbol at the intersection.

    It would be nice to avoid having to do line following this way and let the Propeller Activity Board do all of the processing. However, until I can figure out a way to get the SimpleIDE compiler to compile header files with embedded C code, this dual processor configuration for line following will have to suffice.

    Comments?

    Regards,
    TCIII

  • TCIIITCIII Posts: 81
    edited 2022-07-01 19:06

    @SteffeD1,

    I have successfully managed to use a Pixcam2, Arduino Leonardo, and a Propeller Activity Board (PAB) for Arlo line following.

    As I stated above, I would have preferred to be able to create Propeller C code to perform the line following, but there are several header file definitions for line following, that are presently not available on the Charmed Labs software website, that are necessary to perform the line following coding for the Propeller.

    To start with, I modified the Charmed Labs' line_zumo_demo.ino line following code to just transmit left and right motor speed values on a serial link to the PAB:

    #include <SPI.h>
    #include <Pixy2.h>
    #include <PIDLoop.h>
    #include <SoftwareSerial.h>
    
    SoftwareSerial prop(2,3);         //RX, TX
    
    #define ARLO_FAST 64
    #define ARLO_SLOW 24
    #define X_CENTER  (pixy.frameWidth/2)
    
    Pixy2 pixy;
    
     PIDLoop headingLoop(500, 0, 0, false);
    
    
    void setup()
    {
      prop.begin(38400);
      Serial.begin(115200);
      prop.print(0, DEC);                  // Send left motor stop
      prop.print(0, DEC);                  // Send right motor stop
      Serial.print("Starting...\n");
    
      pixy.init();
      // Turn on both lamps, upper and lower for maximum exposure
      pixy.setLamp(1, 1);
      // change to the line_tracking program.  Note, changeProg can use partial strings, so for example,
      // you can change to the line_tracking program by calling changeProg("line") instead of the whole
      // string changeProg("line_tracking")
      pixy.changeProg("line_tracking");
    
      // look straight and down
      //pixy.setServos(500, 1000);
    }  
    
    void loop()
    {
      int8_t res;
      int32_t error;
      int left, right;
      char buf[96];
    
      // Get latest data from Pixy, including main vector, new intersections and new barcodes.
      res = pixy.line.getMainFeatures();
    
      // If error or nothing detected, stop motors
      if (res<=0) 
      {
        Serial.print("stop ");
        Serial.println(res);
        return;
      }
    
    // We found the vector...
      if (res&LINE_VECTOR)
      {
        // Calculate heading error with respect to m_x1, which is the far-end of the vector,
        // the part of the vector we're heading toward.
        error = (int32_t)pixy.line.vectors->m_x1 - (int32_t)X_CENTER;
    
        pixy.line.vectors->print();
    
        // Perform PID calcs on heading error.
        headingLoop.update(error);
    
        // separate heading into left and right wheel velocities.
        left = headingLoop.m_command;
        right = -headingLoop.m_command;
    
        // If vector is heading away from us (arrow pointing up), things are normal.
        if (pixy.line.vectors->m_y0 > pixy.line.vectors->m_y1)
        {
          // ... but slow down a little if intersection is present, so we don't miss it.
          if (pixy.line.vectors->m_flags&LINE_FLAG_INTERSECTION_PRESENT)
          {
            left = ARLO_SLOW;
            right = ARLO_SLOW;
          }
          else // otherwise, pedal to the metal!
          {
            left = ARLO_FAST;
            right = ARLO_FAST;
          }    
        }
        else  // If the vector is pointing down, or down-ish, we need to go backwards to follow.
        {
          left -= ARLO_SLOW;
          right -= ARLO_SLOW;  
        } 
            prop.write(left);
            prop.write(right);
        // Serial.print(left, DEC);                  // For troubleshooting.
        // Serial.print(right, DEC);
      }
    
      // If intersection, do nothing (we've already set the turn), but acknowledge with a beep.
      if (res&LINE_INTERSECTION)
      {
    
        pixy.line.intersections->print();
      }
    }
    
    

    I added the SoftwareSerial.h library to be able to send the left and right motor speed values to the PAB and replaced the Zumo motor control commands with serial write code to send the computed speed values to the PAB. I also reduced the PIDLoop P value to 500 from 5000.

    Here is my Propeller C code to serially receive the left and right motor speed values from the Arduino Leonardo and use those speed values to the control the Arlo Motor/Wheel assemblies:

    #include "simpletools.h"
    #include "arlodrive.h"
    #include "serial.h"
    
    / ------ Global Variables and Objects ------
    int leftSpeed;
    int rightSpeed;
    serial *ser5_4;
    
    // ------ Main Program ------
    int main() {
      ser5_4 = serial_open(5, 4, 0b0000, 38400);
      // Pixy2_Line_Following
    
      while(1) {
        leftSpeed = serial_rxChar(ser5_4);
        rightSpeed = serial_rxChar(ser5_4);
        // print(("Left = "));
        // print("%d\r", leftSpeed);
        // print(("Right = "));
        // print("%d\r", rightSpeed);
        drive_rampStep(leftSpeed, rightSpeed);
      }
    }   
    

    The drive_rampStep(left, right) function is used in place of drive_speed (left, right) because it the best way to control the Arlo Motor/Wheel assemblies speed when using a loop to transmit time varying speed values.

    Comments?

    Regards,
    TCIII

  • Somewhat related: IMO one the most intriguing (and frustrating) promise of driverless cars is the elimination of stoplights at intersections on surface streets. Cars all talk to each other, managing speed, sequence & separation to move nonstop through intersections north/south/east/west. AKA AIM, or Autonomous Intersection Management, see video below at 1:00. The frustrating part is that 100% of the cars must be self driving.

    'Seems to work okay without self-driving cars here:

    -Phil

  • ercoerco Posts: 20,195

    OMG that's a crackup, PhiPi!

  • @SteffeD1,

    Here is link to my experience attempting to use the Propeller Activity Board with the HB25 motor controllers.

    I do not recommend using the HB25 motor controller with the Propeller Activity Board and have switched to a Roboclaw motor controller with excellent results.

    TCIII

Sign In or Register to comment.