![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
bio robot
Dependencies: MPU6050-DMP QEI_hw mbed-rpc mbed
Fork of MPU6050_Example by
Comm/Comm.cpp@6:12b6be801ad8, 2015-12-03 (annotated)
- Committer:
- amandaghassaei
- Date:
- Thu Dec 03 05:52:55 2015 +0000
- Revision:
- 6:12b6be801ad8
- Parent:
- 5:c3d671959ef3
- Child:
- 8:1a3a69fecedf
comm stack stop pt for now
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amandaghassaei | 2:17b18ea93551 | 1 | #include "Comm.h" |
amandaghassaei | 2:17b18ea93551 | 2 | |
amandaghassaei | 2:17b18ea93551 | 3 | |
amandaghassaei | 4:ebb882658c50 | 4 | Comm::Comm(CommDelegate *gains, CommDelegate *target, CommDelegate *myMPU6050_1):_pc(USBTX, USBRX), _json(&_pc) |
amandaghassaei | 2:17b18ea93551 | 5 | { |
amandaghassaei | 6:12b6be801ad8 | 6 | _pc.baud(115200); |
amandaghassaei | 2:17b18ea93551 | 7 | _gains = gains; |
amandaghassaei | 2:17b18ea93551 | 8 | _target = target; |
amandaghassaei | 2:17b18ea93551 | 9 | _myMPU6050_1 = myMPU6050_1; |
amandaghassaei | 2:17b18ea93551 | 10 | } |
amandaghassaei | 2:17b18ea93551 | 11 | |
amandaghassaei | 5:c3d671959ef3 | 12 | void Comm::openGripper1(Arguments* input, Reply* output){ |
amandaghassaei | 2:17b18ea93551 | 13 | printGripper1State(true); |
amandaghassaei | 2:17b18ea93551 | 14 | } |
amandaghassaei | 2:17b18ea93551 | 15 | |
amandaghassaei | 5:c3d671959ef3 | 16 | void Comm::closeGripper1(Arguments* input, Reply* output){ |
amandaghassaei | 2:17b18ea93551 | 17 | printGripper1State(false); |
amandaghassaei | 2:17b18ea93551 | 18 | } |
amandaghassaei | 2:17b18ea93551 | 19 | |
amandaghassaei | 2:17b18ea93551 | 20 | void Comm::printGripper1State(bool state){ |
amandaghassaei | 2:17b18ea93551 | 21 | _json.open(); |
amandaghassaei | 2:17b18ea93551 | 22 | if (state) _json.print("gripper1", "true"); |
amandaghassaei | 2:17b18ea93551 | 23 | else _json.print("gripper1", "false"); |
amandaghassaei | 2:17b18ea93551 | 24 | _json.close(); |
amandaghassaei | 2:17b18ea93551 | 25 | } |
amandaghassaei | 2:17b18ea93551 | 26 | |
amandaghassaei | 5:c3d671959ef3 | 27 | void Comm::openGripper2(Arguments* input, Reply* output){ |
amandaghassaei | 2:17b18ea93551 | 28 | printGripper2State(true); |
amandaghassaei | 2:17b18ea93551 | 29 | } |
amandaghassaei | 2:17b18ea93551 | 30 | |
amandaghassaei | 5:c3d671959ef3 | 31 | void Comm::closeGripper2(Arguments* input, Reply* output){ |
amandaghassaei | 2:17b18ea93551 | 32 | printGripper2State(false); |
amandaghassaei | 2:17b18ea93551 | 33 | } |
amandaghassaei | 2:17b18ea93551 | 34 | |
amandaghassaei | 2:17b18ea93551 | 35 | void Comm::printGripper2State(bool state){ |
amandaghassaei | 2:17b18ea93551 | 36 | _json.open(); |
amandaghassaei | 2:17b18ea93551 | 37 | if (state) _json.print("gripper2", "true"); |
amandaghassaei | 2:17b18ea93551 | 38 | else _json.print("gripper2", "false"); |
amandaghassaei | 2:17b18ea93551 | 39 | _json.close(); |
amandaghassaei | 2:17b18ea93551 | 40 | } |
amandaghassaei | 2:17b18ea93551 | 41 | |
amandaghassaei | 5:c3d671959ef3 | 42 | void Comm::setGains(Arguments* input, Reply* output){ |
amandaghassaei | 5:c3d671959ef3 | 43 | if (input->argc < _gains->numGains()){ |
amandaghassaei | 5:c3d671959ef3 | 44 | throwNotEnoughArgsError(); |
amandaghassaei | 5:c3d671959ef3 | 45 | return; |
amandaghassaei | 5:c3d671959ef3 | 46 | } |
amandaghassaei | 5:c3d671959ef3 | 47 | float k1 = input->getArg<float>(); |
amandaghassaei | 5:c3d671959ef3 | 48 | float d1 = input->getArg<float>(); |
amandaghassaei | 5:c3d671959ef3 | 49 | float k2 = input->getArg<float>(); |
amandaghassaei | 5:c3d671959ef3 | 50 | float d2 = input->getArg<float>(); |
amandaghassaei | 2:17b18ea93551 | 51 | _gains->setGains(k1, d1, k2, d2); |
amandaghassaei | 5:c3d671959ef3 | 52 | printGains(); |
amandaghassaei | 2:17b18ea93551 | 53 | } |
amandaghassaei | 2:17b18ea93551 | 54 | |
amandaghassaei | 2:17b18ea93551 | 55 | void Comm::printGains(){ |
amandaghassaei | 2:17b18ea93551 | 56 | _json.open(); |
amandaghassaei | 2:17b18ea93551 | 57 | _json.print("k1", _gains->getK1()); |
amandaghassaei | 2:17b18ea93551 | 58 | _json.sepItem(); |
amandaghassaei | 2:17b18ea93551 | 59 | _json.print("d1", _gains->getD1()); |
amandaghassaei | 2:17b18ea93551 | 60 | _json.sepItem(); |
amandaghassaei | 2:17b18ea93551 | 61 | _json.print("k2", _gains->getK2()); |
amandaghassaei | 2:17b18ea93551 | 62 | _json.sepItem(); |
amandaghassaei | 2:17b18ea93551 | 63 | _json.print("d2", _gains->getD1()); |
amandaghassaei | 2:17b18ea93551 | 64 | _json.close(); |
amandaghassaei | 2:17b18ea93551 | 65 | } |
amandaghassaei | 2:17b18ea93551 | 66 | |
amandaghassaei | 5:c3d671959ef3 | 67 | void Comm::setTarget(Arguments* input, Reply* output){ |
amandaghassaei | 5:c3d671959ef3 | 68 | if (input->argc < 1){ |
amandaghassaei | 5:c3d671959ef3 | 69 | throwNotEnoughArgsError(); |
amandaghassaei | 5:c3d671959ef3 | 70 | return; |
amandaghassaei | 5:c3d671959ef3 | 71 | } |
amandaghassaei | 5:c3d671959ef3 | 72 | int targetPosition = input->getArg<int>(); |
amandaghassaei | 2:17b18ea93551 | 73 | _target->setPosition(targetPosition); |
amandaghassaei | 5:c3d671959ef3 | 74 | printTarget(); |
amandaghassaei | 2:17b18ea93551 | 75 | } |
amandaghassaei | 2:17b18ea93551 | 76 | |
amandaghassaei | 2:17b18ea93551 | 77 | void Comm::printTarget(){ |
amandaghassaei | 2:17b18ea93551 | 78 | _json.open(); |
amandaghassaei | 2:17b18ea93551 | 79 | _json.print("target", _target->getPosition()); |
amandaghassaei | 2:17b18ea93551 | 80 | _json.close(); |
amandaghassaei | 2:17b18ea93551 | 81 | } |
amandaghassaei | 2:17b18ea93551 | 82 | |
amandaghassaei | 2:17b18ea93551 | 83 | void Comm::printPosition(){ |
amandaghassaei | 2:17b18ea93551 | 84 | _json.open(); |
amandaghassaei | 2:17b18ea93551 | 85 | _json.print("th1", _myMPU6050_1->getTheta()); |
amandaghassaei | 2:17b18ea93551 | 86 | _json.sepItem(); |
amandaghassaei | 2:17b18ea93551 | 87 | _json.print("dth1", _myMPU6050_1->getDtheta()); |
amandaghassaei | 2:17b18ea93551 | 88 | _json.close(); |
amandaghassaei | 2:17b18ea93551 | 89 | } |
amandaghassaei | 2:17b18ea93551 | 90 | |
amandaghassaei | 4:ebb882658c50 | 91 | void Comm::check(){ |
amandaghassaei | 5:c3d671959ef3 | 92 | if(!_pc.readable()) return; |
amandaghassaei | 4:ebb882658c50 | 93 | _pc.gets(buf, 256); |
amandaghassaei | 4:ebb882658c50 | 94 | //Call the static call method on the RPC class |
amandaghassaei | 4:ebb882658c50 | 95 | RPC::call(buf, outbuf); |
amandaghassaei | 5:c3d671959ef3 | 96 | // _pc.printf("%s\n", outbuf); |
amandaghassaei | 4:ebb882658c50 | 97 | } |
amandaghassaei | 5:c3d671959ef3 | 98 | |
amandaghassaei | 5:c3d671959ef3 | 99 | void Comm::throwNotEnoughArgsError(){ |
amandaghassaei | 5:c3d671959ef3 | 100 | _json.open(); |
amandaghassaei | 5:c3d671959ef3 | 101 | _json.print("error", "not enough input arguments"); |
amandaghassaei | 5:c3d671959ef3 | 102 | _json.close(); |
amandaghassaei | 5:c3d671959ef3 | 103 | } |