<commit_before>package org.usfirst.frc.team1736.lib.CoProcessor;

import java.util.concurrent.locks.ReentrantLock;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

///////////////////////////////////////////////////////////////////////////////
//Copyright (c) FRC Team 1736 2016. See the License file.
//
//Can you use this code? Sure! We're releasing this under GNUV3, which
//basically says you can take, modify, share, publish this as much as you
//want, as long as you don't make it closed source.
//
//If you do find it useful, we'd love to hear about it! Check us out at
//http://robotcasserole.org/ and leave us a message!
///////////////////////////////////////////////////////////////////////////////

/**
* DESCRIPTION: <br>
* Full class to set up a UDP listener, look for UDP packets from a vision coprocessor (such as a Beaglebone Black (BBB)), and interpret 
* the json data into target observations. Note that the JSON object structures and the content of the data is
* tightly tied with the functionality implemented on the coprocessor - this code can likely not be fully understood
* without looking into that code as well! <br>
* <br>
* USAGE:
* <ol>
* <li>Instantiate the reciever with the listen address and port. </li>
* <li>Call the start() method when you are ready to start listening for frames. </li>
* <li>Once per control loop, call the sampleLatestData() method to load data from the latest frame. </li>
* <li>During the control loop, call the other getter functions to extract info about this latest frame. </li>
* </ol>
* 
* 
*/

public class VisionListener {
    UDPReceiver listenClient;
    boolean coprocessorActive;
    
    private String addr;
    private int port;
    JSONParser parser;
    
    //We expect the BBB to send updates at a 30Hz rate (one per frame), so we'll say 35 just for yucks.
    // This constant is used to help the timing of the background update thread
    // so it doesn't chew too many clock cycles just waiting
    private double EXPECTED_MAX_UPDATE_RATE_HZ = 35.0;
    
    //If this much time goes by without getting a packet, we will start to say the processor is no longer turned on.
    private long COPROCESSOR_ACTIIVE_TIMEOUT_MS = 3000;
    
    // Mutithreading academic dissertation time! 
    // The present and previous observations represent a total set of data which must be updated atomically. 
    // We expect them to be updated asynchronously in the background by the listener thread (with updates    
    // driven by udp packet arrivals). While we are in the process of updating them, they are in an _inconsistant_
    // state - that is to say they are not valid for use by other threads. Therefore, during the whole update process,
    // we must LOCK that critical section so other threads which access either observation. 
    private JSONObject currObservation;
    private JSONObject prevObservation;
    private ReentrantLock observationLock;
    private long mostRecentPacketTime;
    
    //This is the json object which the control system considers as "current". Other observations will continue to be gathered in the background,
    //but this object will not be updated until the control system thread calls the sampleLatestData() method.
    private JSONObject userCurrObservation;
    
    /**
     * Constructor for the Vision Coprocessor listener socket. Sets up internal variables to get ready
     * for information to be transmitted from the coprocessor.
     * @param listen_to_addr String of the IP address of the coprocessor (For example, "10.17.36.20")
     * @param listen_on_port integer port number to listen on. Usually between 5800 and 5810 per FMS whitepaper. Must match whatever port the coprocessor is sending information to.
     */
    public VisionListener(String listen_to_addr, int listen_on_port){
        addr = listen_to_addr;
        port = listen_on_port;
        coprocessorActive = false;
        currObservation = new JSONObject();
        prevObservation = new JSONObject();
        userCurrObservation = new JSONObject();
        parser = new JSONParser();
        observationLock = new ReentrantLock();
        
    }
    
    /**
     * Starts the listener in the background to look for and interpret UDP packets from the coprocessor. <br><br>
     * This method MUST be called after the constructor, but before any attempt is made to use the data from the coprocessor.
     */
    public void start(){
        <commit_msg>work in progress<commit_after>package org.usfirst.frc.team1736.lib.CoProcessor;

import java.util.concurrent.locks.ReentrantLock;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

///////////////////////////////////////////////////////////////////////////////
//Copyright (c) FRC Team 1736 2016. See the License file.
//
//Can you use this code? Sure! We're releasing this under GNUV3, which
//basically says you can take, modify, share, publish this as much as you
//want, as long as you don't make it closed source.
//
//If you do find it useful, we'd love to hear about it! Check us out at
//http://robotcasserole.org/ and leave us a message!
///////////////////////////////////////////////////////////////////////////////

/**
* DESCRIPTION: <br>
* Full class to set up a UDP listener, look for UDP packets from a vision coprocessor (such as a Beaglebone Black (BBB)), and interpret 
* the json data into target observations. Note that the JSON object structures and the content of the data is
* tightly tied with the functionality implemented on the coprocessor - this code can likely not be fully understood
* without looking into that code as well! <br>
* <br>
* USAGE:
* <ol>
* <li>Instantiate the reciever with the listen address and port. </li>
* <li>Call the start() method when you are ready to start listening for frames. </li>
* <li>Once per control loop, call the sampleLatestData() method to load data from the latest frame. </li>
* <li>During the control loop, call the other getter functions to extract info about this latest frame. </li>
* </ol>
* 
* 
*/

public class VisionListener {
    UDPReceiver listenClient;
    boolean coprocessorActive;
    
    private String addr;
    private int port;
    JSONParser parser;
    
    //We expect the BBB to send updates at a 30Hz rate (one per frame), so we'll say 35 just for yucks.
    // This constant is used to help the timing of the background update thread
    // so it doesn't chew too many clock cycles just waiting
    private double EXPECTED_MAX_UPDATE_RATE_HZ = 35.0;
    
    //If this much time goes by without getting a packet, we will start to say the processor is no longer turned on.
    private long COPROCESSOR_ACTIIVE_TIMEOUT_MS = 3000;
    
    // Mutithreading academic dissertation time! 
    // The present and previous observations represent a total set of data which must be updated atomically. 
    // We expect them to be updated asynchronously in the background by the listener thread (with updates    
    // driven by udp packet arrivals). While we are in the process of updating them, they are in an _inconsistant_
    // state - that is to say they are not valid for use by other threads. Therefore, during the whole update process,
    // we must LOCK that critical section so other threads which access either observation. 
    private JSONObject currObservation;
    private JSONObject prevObservation;
    private ReentrantLock observationLock;
    private long mostRecentPacketTime;
    
    //This is the json object which the control system considers as "current". Other observations will continue to be gathered in the background,
    //but this object will not be updated until the control system thread calls the sampleLatestData() method.
    private JSONObject userCurrObservation;
    
    //Values used for axis M1011
    double targetWidthFeet = 1.25;
    int FOVWidthPixel = 640;
    double tangentTheta = 1.0724;
    
    /**
     * Constructor for the Vision Coprocessor listener socket. Sets up internal variables to get ready
     * for information to be transmitted from the coprocessor.
     * @param listen_to_addr String of the IP address of the coprocessor (For example, "10.17.36.20")
     * @param listen_on_port integer port number to listen on. Usually between 5800 and 5810 per FMS whitepaper. Must match whatever port the coprocessor is sending information to.
     */
    public VisionListener(String listen_to_addr, int listen_on_port){
        addr = listen_to_addr;
        port = listen_on_port;
        coprocessorActive = false;
        currObservation = new JSONObject();
        prevObservation = new JSONObject();
        userCurrObservation = new JSONObject();
        parser = new JSONParser();
        observationLock = new ReentrantLock();
        
    }
    
    /**
     * Starts the listener in the background to look for and interpret UDP packets from the coprocessor. <br><br>
     * This method MUST be called after the constructor, but before any attempt is made to use the data from the coprocessor.
     */
    public void start(){
        
{'fraction_non_alphanumeric': 0.09584457636265516, 'fraction_numerical': 0.01025364274150027, 'mean_word_length': 2.8786102971954795, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3441994', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2367, 'n_words': 1152}