Announcement

Collapse
No announcement yet.

NetworTableInstance doesn't seem to be working properly, advice needed.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • NetworTableInstance doesn't seem to be working properly, advice needed.

    I have this code on the raspberry pi to simply send x=10.0 and y=2.0 over the network to the roborio, which on it's side should send it back to the dashboard.
    Sounds very straight forward, only that it's not working. Here is my rasp code:

    #include "opencv2/imgproc.hpp"
    #include "opencv2/highgui.hpp"
    #include <networktables/NetworkTableInstance.h>
    #include <vision/VisionRunner.h>
    #include "wpi/HelperFunctionsAnwar.h"
    #include "opencv2/core/cvstd.hpp"
    #include <wpi/StringRef.h>
    #include <wpi/json.h>
    #include <wpi/raw_istream.h>
    #include <wpi/raw_ostream.h>
    #include "cameraserver/CameraServer.h"
    #include "time.h"
    class MyPipelines : public frc::VisionPipeline {
    public:
    int val = 0;
    clock_t start;
    double duration;
    std::shared_ptr<NetworkTable> myTable;
    nt::NetworkTableInstance table;
    //NetworkTable *table;

    cs::CvSource mOrigSource;
    MyPipelines()
    {
    wpi:uts() << "starting stream2\n";
    mOrigSource = frc::CameraServer::GetInstance()->PutVideo("stream2", 256, 144);
    /* Your algorithm here */
    start = clock();
    table = nt::NetworkTableInstance::GetDefault();
    table.StartClientTeam(6168);
    myTable = table.GetTable("datatable");
    }
    void Process(cv::Mat& mat) override {
    //Anwar's code starts here:
    wpi:uts() << "Vision processing started..."<<mat.size().height<<"x"<<mat.size().wi dth<<"\n";
    duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
    cv::Mat tempMat = mat.clone(), hsvMat = mat.clone(), mask = mat.clone();// create copy of original image to use for ball detection
    cv::Scalar orangeHsvMin = (0,50,50);//(5, 50, 50);
    cv::Scalar orangeHsvMax = (25,255,255);//(15, 255, 255);
    cv::GaussianBlur(hsvMat, hsvMat,cv::Size(1,1),0);
    cv::cvtColor(hsvMat, hsvMat,cv::COLOR_BGR2HSV);
    for(int y=0;y<hsvMat.rows;y++)
    {
    for(int x=0;x<hsvMat.cols;x++)// inRange alternative.
    {
    // get pixel
    cv::Vec3b color = hsvMat.at<cv::Vec3b>(cv::Point(x,y));
    // ... do something to the color ....
    if(color[0]>=3 && color[0]<=25 && color[1]>=100 && color[1]<=255 && color[2]>=100 && color[2]<=255)
    color = {100,255,255};
    else
    {
    color = {0,0,0};
    }

    // set pixel
    hsvMat.at<cv::Vec3b>(cv::Point(x,y)) = color;
    }
    }
    HelperFunctionsAnwar helpFunction;
    hsvMat = helpFunction.DetectCirclesAndDrawThem(hsvMat, &mat);//use tempMat for detection and draw on mat
    tempMat.deallocate();
    mask.deallocate();
    myTable->PutNumber("X",10.0);
    myTable->PutNumber("Y",2.0);
    //Anwar's code ends here.
    mOrigSource.PutFrame(mat); // <-- here only changed to mat
    }
    };

    and on the the roborio it's something like this:

    public:
    double x=0.0, y=0.0;
    nt::NetworkTableEntry xEntry;
    nt::NetworkTableInstance table;

    void RobotInit(){
    auto inst = nt::NetworkTableInstance::GetDefault();
    inst.SetServerTeam(6168);
    auto table = inst.GetTable("datatable");
    }

    void TeleopPeriodic(){
    xEntry = table.GetEntry("X");
    xEntry.SetDouble(x);
    frc::SmartDashBoard::PutNumber("X=",x);
    }


    Now all get in the dashboard is x=0

    I think reason is datatable has never been created.
    I looked every where for documentation on how to create it, or if the GetTable("datatable") would create it if it doesn't exist, only to no avail.


  • #2
    On the robot code near the bottom you don't want SetDouble. You are setting the value in the table to x (which is zero) and then setting the SmartDashboard value to 0 as well. You want GetDouble.
    FIRST®
    FRC Robotics Engineer

    Comment

    Working...
    X