Commit f6be0e40 authored by Beck-Sisyphus's avatar Beck-Sisyphus
Browse files

Separate previous year's code and merge together with EE team's code

parent 8c63716d
No preview for this file type
No preview for this file type
......@@ -2,13 +2,18 @@
#include <Arduino.h>
#include "megaBoardDefine.h"
#include "communication.h"
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <Wire.h>
void setup() {
Serial.begin(BAUD_RATE);
Serial1.begin(BAUD_RATE);
motor1.attach(motorPin1);
motor2.attach(motorPin2);
leftMotor.attach(motorPin1);
rightMotor.attach(motorPin2);
// set up the arm to steady
for(int i = 0; i < 3; i++) {
......@@ -23,6 +28,32 @@ void loop() {
// getReadingFromCurrentSensor();
armRiseAndBack();
delay(100);
}
int networkStatus = processNetworkData();
if(networkStatus == 1) {
// Refresh timeout
timeLastPacket = millis();
// Process new values;
leftMotor.writeMicroseconds(map(leftPower, 0, 255, MIN_FREQUENCY, MAX_FREQUENCY));
rightMotor.writeMicroseconds(map(rightPower, 0, 255, MIN_FREQUENCY, MAX_FREQUENCY));
for(int i = 0; i < 4; i++) {
armMotor[i].writeMicroseconds(dirToMicroseconds(arm[i]));
}
for(int i = 0; i < 3; i++) {
Serial.print("Hand: ");
Serial.print(i);
Serial.println(hand[i]);
if(hand[i] == 0 || hand[i] == 2) {
if(hand[i] == 0 && handPos[i] < 180) {
handPos[i] += 5;
} else if(handPos[i] > 0) {
handPos[i] -= 5;
}
// handMotor[i].write(handPos[i]);
}
}
}
}
......@@ -11,11 +11,11 @@ void driveMotorFullSpeed(int potValue) {
sendValue = map(potValue, 0, 1023, 1250, 1750);
if (sendValue < 1480 || sendValue > 1520)
{
motor1.writeMicroseconds(sendValue);
motor2.writeMicroseconds(-sendValue);
leftMotor.writeMicroseconds(sendValue);
rightMotor.writeMicroseconds(-sendValue);
} else {
motor1.writeMicroseconds(1500);
motor2.writeMicroseconds(1500);
leftMotor.writeMicroseconds(1500);
rightMotor.writeMicroseconds(1500);
}
// delay(10);
}
// Start from 2013's code made by Talyor Cramer
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <Wire.h>
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// MAC address, IP address, and port for UDP
boolean hasIP = false;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 5005;
const int TIMEOUT = 1000;
// buffers for receiving and sending data
char packetBuffer[96]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
unsigned long timeLastPacket = millis() - TIMEOUT;
// Recieved Data
int leftPower = 128;
int rightPower = 128;
int arm[4] = {
1, 1, 1, 1};
int hand[3] = {
1, 1, 1};
int misc[4] = {
1, 1, 1, 1};
// Changing values
int handPos[3] = {90, 90, 90};
// Sent Data
int pressureValue = 0;
int processNetworkData() {
//Serial.println(freeRam());
// Check size of available packet
int packetSize = Udp.parsePacket();
// Process packet if available
if(packetSize == 5)
{
hasIP=true;
//printPacketInfo(packetBuffer);
//Serial.println("GOOD");
// read the packet into packetBufffer
Udp.read(packetBuffer, 96);
// Process packet into values
leftPower = ((unsigned char)packetBuffer[0]) & 0xFFFF;
rightPower = ((unsigned char)packetBuffer[1]) & 0xFFFF;
readByteAsInt(packetBuffer[2], arm, 4);
readByteAsInt(packetBuffer[3], hand, 3);
readByteAsInt(packetBuffer[4], misc, 4);
//printPacketValues();
return 1;
}
return 0;
}
// Prints data about packet, packet sender
void printPacketInfo(int packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i = 0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
}
// Prints readable versions of packet
void printPacketValues() {
Serial.print("Left: ");
Serial.println(leftPower);
Serial.print("Right: ");
Serial.println(rightPower);
for(int i = 0; i < 4; i++)
{
Serial.print("Arm:");
Serial.println(arm[i]);
}
for(int i = 0; i < 3; i++)
{
Serial.print("Hand:");
Serial.println(hand[i]);
}
for(int i = 0; i < 4; i++)
{
Serial.print("Misc:");
Serial.println(misc[i]);
}
}
void readByteAsInt(byte data, int* buffer, byte bufferSize) {
byte mask = 0x03;
for(byte i = 0; i < bufferSize; i++)
{
*buffer = data & mask;
data >>= 2;
buffer++;
}
}
int dirToMicroseconds(int dir) {
if(dir == 0) {
return MIN_FREQUENCY;
}
if(dir == 2) {
return MAX_FREQUENCY;
}
return NEUTRAL_FREQUENCY;
}
// Handle i2c Event
void receivei2c(int howMany) {
if(howMany == 9) {
char i2cData[8];
Wire.read();
for(int i = 0; i < 8; i++) {
char c = Wire.read();
i2cData[i] = c;
}
if(hasIP) {
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(i2cData);
Udp.endPacket();
}
}
}
......@@ -12,8 +12,7 @@ int sendValue;
// Need PWM input with male pin for motor controller
// connect to RS232
Servo motor1;
Servo motor2;
Servo leftMotor, rightMotor;
const int motorPin1 = 12;
const int motorPin2 = 13;
......@@ -24,6 +23,8 @@ const int TALON_ARM[3] = {2, 3, 4};
const int NEUTRAL_FREQUENCY = 1500;
const int SPEEDUP_FREQUENCY = 1750;
const int SPEEDDOWN_FREWQUENCY = 1250;
const int MIN_FREQUENCY = 1001;
const int MAX_FREQUENCY = 2041;
// Turn table continous rotation servo
const int CONT_SERVO_CENTER 80 //in degrees
const int CONT_SERVO_CENTER = 80; //in degrees
......@@ -186,7 +186,7 @@ void setup() {
// i2c
Wire.begin(4); // Join i2c with address #1
Wire.onReceive(receivei2c);
Wire.onReceive(receivei2c); // Call back
// Start serial
Serial.begin(9600);
......
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment