Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Hanson Robokind LLC.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.robokind.impl.motion.dynamixel.feedback;
 
 import java.util.Map;
Provides concurrency for reading and updating values used by the DynamixelControlLoop. This will accept new values, both dynamixel feedback values and new goal positions, merging them in a thread-safe manner.

Author(s):
Matthew Stevenson <www.robokind.org>
 
 public class ConcurrentDynamixelCache {
     private Lock myValueLock;
     private boolean myMoveFlag;
     
     public ConcurrentDynamixelCache(){
          = new HashMap<DynamixelServo.IdDynamixelServoCache>();
          = new ReentrantLock();
          = false;
     }
    
    
Locks the cache and returns the MoveParams. releaseMoveParams() must be called when done using the params.

Returns:
 
         .lock();
         return (Collection).values();
     }
     
     synchronized void releaseMoveParams(){
         try{
             mergeFeedback(.values());
             mergeGoals(.values());
             .clear();
             .clear();
         }finally{
             .unlock();
         }
     }
    
    
Attempts to merge the given values into the MoveParam cache. If the params are in use and locked, the values are queued to be merged when unlocked.
 
     synchronized void addFeedbackValues(Collection<FeedbackUpdateValuesvalues){
         if(.tryLock()){
             try{
                 mergeFeedback(values);
             }finally{
                 .unlock();
             }
         }else{
             queueFeedback(values);
         }
     }
     
     synchronized void setGoalPositions(Collection<GoalUpdateValues<DynamixelServo.Id>> goals){
         if(.tryLock()){
             try{
                 mergeGoals(goals);
             }finally{
                 .unlock();
             }
         }else{
             queueGoals(goals);
         }
     }
     
     private void queueFeedback(Collection<FeedbackUpdateValuesvals){
         for(FeedbackUpdateValues val : vals){
             if(val.getCurrentTemperature() == 0 
                    || val.getCurrentVoltage() == 0){
                continue;
            }
            .put(val.getServoId(), val);
        }
    }
    
    private void mergeFeedback(Collection<FeedbackUpdateValuesvals){
        for(FeedbackUpdateValues val : vals){
            DynamixelServo.Id id = val.getServoId();
            DynamixelServoCache cache = .get(id);
            if(cache == null){
                cache = new DynamixelServoCache(id);
                cache.setGoalVals(new GoalUpdateValues(id, -1, 0));
                cache.goalsSent();
                .put(idcache);
            }
            cache.setFeedbackVals(val);
        }
    }
    
    private void queueGoals(Collection<GoalUpdateValues<DynamixelServo.Id>> vals){
        for(GoalUpdateValues<DynamixelServo.Idval : vals){
            .put(val.getServoId(), val);
        }
    }
    
    private void mergeGoals(Collection<GoalUpdateValues<DynamixelServo.Id>> goals){
        for(GoalUpdateValues<DynamixelServo.Idval : goals){
            DynamixelServo.Id id = val.getServoId();
            DynamixelServoCache cache = .get(id);
            if(cache == null){
                cache = new DynamixelServoCache(id);
                cache.setFeedbackVals(
                        new FeedbackUpdateValues(idnew int[5], 0));
                .put(idcache);
            }
            cache.setGoalVals(val);
        }
        if(!goals.isEmpty()){
            setMoveFlag(true);
        }
    }
    
    synchronized boolean getMoveFlag(){
        return ;
    }
    
    synchronized void setMoveFlag(boolean val){
         = val;
    }
    
        return ;
    }
New to GrepCode? Check out our FAQ X